optloop.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. interface
  20. uses
  21. node;
  22. function unroll_loop(node : tnode) : tnode;
  23. function OptimizeInductionVariables(node : tnode) : boolean;
  24. implementation
  25. uses
  26. cclasses,
  27. globtype,globals,constexp,
  28. symdef,symsym,
  29. cpuinfo,
  30. nutils,
  31. nadd,nbas,nflw,ncon,ninl,ncal,nld,
  32. pass_1,
  33. optbase,optutils,
  34. procinfo;
  35. var
  36. nodecount : aword;
  37. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  38. begin
  39. inc(nodecount);
  40. result:=fen_false;
  41. end;
  42. { rough estimation how large the tree "node" is }
  43. function countnodes(node : tnode) : aword;
  44. begin
  45. nodecount:=0;
  46. foreachnodestatic(node,@donodecount,nil);
  47. result:=nodecount;
  48. end;
  49. function number_unrolls(node : tnode) : cardinal;
  50. begin
  51. {$ifdef i386}
  52. { multiply by 2 for CPUs with a long pipeline }
  53. if current_settings.cputype in [cpu_Pentium4] then
  54. number_unrolls:=60 div countnodes(node)
  55. else
  56. {$endif i386}
  57. number_unrolls:=30 div countnodes(node);
  58. if number_unrolls=0 then
  59. number_unrolls:=1;
  60. end;
  61. function unroll_loop(node : tnode) : tnode;
  62. var
  63. unrolls,i : cardinal;
  64. counts : qword;
  65. unrollstatement,newforstatement : tstatementnode;
  66. unrollblock : tblocknode;
  67. begin
  68. result:=nil;
  69. if (cs_opt_size in current_settings.optimizerswitches) then
  70. exit;
  71. if not(node.nodetype in [forn]) then
  72. exit;
  73. unrolls:=number_unrolls(tfornode(node).t2);
  74. if unrolls>1 then
  75. begin
  76. { number of executions known? }
  77. if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
  78. begin
  79. if lnf_backward in tfornode(node).loopflags then
  80. counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
  81. else
  82. counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
  83. { don't unroll more than we need }
  84. if unrolls>counts then
  85. unrolls:=counts;
  86. { create block statement }
  87. unrollblock:=internalstatements(unrollstatement);
  88. { let's unroll (and rock of course) }
  89. for i:=1 to unrolls do
  90. begin
  91. { create and insert copy of the statement block }
  92. addstatement(unrollstatement,tfornode(node).t2.getcopy);
  93. { set and insert entry label? }
  94. if (counts mod unrolls<>0) and
  95. ((counts mod unrolls)=unrolls-i) then
  96. begin
  97. tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create,tlabelsym.create('$optunrol'));
  98. addstatement(unrollstatement,tfornode(node).entrylabel);
  99. end;
  100. { for itself increases at the last iteration }
  101. if i<unrolls then
  102. begin
  103. { insert incrementation of counter var }
  104. addstatement(unrollstatement,
  105. geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
  106. end;
  107. end;
  108. { can we get rid of the for statement? }
  109. if unrolls=counts then
  110. begin
  111. { create block statement }
  112. result:=internalstatements(newforstatement);
  113. { initial assignment }
  114. addstatement(newforstatement,cassignmentnode.create(
  115. tfornode(node).left.getcopy,tfornode(node).right.getcopy));
  116. addstatement(newforstatement,unrollblock);
  117. end;
  118. end
  119. else
  120. begin
  121. { unrolling is a little bit more tricky if we don't know the
  122. loop count at compile time, but the solution is to use a jump table
  123. which is indexed by "loop count mod unrolls" at run time and which
  124. jumps then at the appropriate place inside the loop. Because
  125. a module division is expensive, we can use only unroll counts dividable
  126. by 2 }
  127. case unrolls of
  128. 1..2:
  129. ;
  130. 3:
  131. unrolls:=2;
  132. 4..7:
  133. unrolls:=4;
  134. { unrolls>4 already make no sense imo, but who knows (FK) }
  135. 8..15:
  136. unrolls:=8;
  137. 16..31:
  138. unrolls:=16;
  139. 32..63:
  140. unrolls:=32;
  141. 64..$7fff:
  142. unrolls:=64;
  143. else
  144. exit;
  145. end;
  146. { we don't handle this yet }
  147. exit;
  148. end;
  149. if not(assigned(result)) then
  150. begin
  151. tfornode(node).t2.free;
  152. tfornode(node).t2:=unrollblock;
  153. end;
  154. end;
  155. end;
  156. var
  157. initcode,
  158. calccode,
  159. deletecode : tblocknode;
  160. initcodestatements,
  161. calccodestatements,
  162. deletecodestatements: tstatementnode;
  163. templist : tfplist;
  164. inductionexprs : tfplist;
  165. changedforloop,
  166. containsnestedforloop : boolean;
  167. function is_loop_invariant(loop : tnode;expr : tnode) : boolean;
  168. begin
  169. result:=is_constnode(expr);
  170. case expr.nodetype of
  171. loadn:
  172. begin
  173. if (pi_dfaavailable in current_procinfo.flags) and
  174. assigned(loop.optinfo) and
  175. assigned(expr.optinfo) then
  176. result:=not(DFASetIn(loop.optinfo^.defsum,expr.optinfo^.index));
  177. end;
  178. end;
  179. end;
  180. { checks if the strength of n can be recuded, arg is the tforloop being considered }
  181. function dostrengthreductiontest(var n: tnode; arg: pointer): foreachnoderesult;
  182. function findpreviousstrengthreduction : boolean;
  183. var
  184. i : longint;
  185. begin
  186. result:=false;
  187. for i:=0 to inductionexprs.count-1 do
  188. begin
  189. { do we already maintain one expression? }
  190. if tnode(inductionexprs[i]).isequal(n) then
  191. begin
  192. n.free;
  193. n:=ttemprefnode.create(ttempcreatenode(templist[i]));
  194. result:=true;
  195. exit;
  196. end;
  197. end;
  198. end;
  199. procedure CreateNodes;
  200. begin
  201. if not assigned(initcode) then
  202. begin
  203. initcode:=internalstatements(initcodestatements);
  204. calccode:=internalstatements(calccodestatements);
  205. deletecode:=internalstatements(deletecodestatements);
  206. end;
  207. end;
  208. var
  209. tempnode : ttempcreatenode;
  210. begin
  211. result:=fen_false;
  212. case n.nodetype of
  213. forn:
  214. { inform for loop search routine, that it needs to search more deeply }
  215. containsnestedforloop:=true;
  216. muln:
  217. begin
  218. if (taddnode(n).right.nodetype=loadn) and
  219. taddnode(n).right.isequal(tfornode(arg).left) and
  220. { plain read of the loop variable? }
  221. not(nf_write in taddnode(n).right.flags) and
  222. not(nf_modify in taddnode(n).right.flags) and
  223. is_loop_invariant(tfornode(arg),taddnode(n).left) and
  224. { for now, we can handle only constant lower borders }
  225. is_constnode(tfornode(arg).right) then
  226. taddnode(n).swapleftright;
  227. if (taddnode(n).left.nodetype=loadn) and
  228. taddnode(n).left.isequal(tfornode(arg).left) and
  229. { plain read of the loop variable? }
  230. not(nf_write in taddnode(n).left.flags) and
  231. not(nf_modify in taddnode(n).left.flags) and
  232. is_loop_invariant(tfornode(arg),taddnode(n).right) and
  233. { for now, we can handle only constant lower borders }
  234. is_constnode(tfornode(arg).right) then
  235. begin
  236. changedforloop:=true;
  237. { did we use the same expression before already? }
  238. if not(findpreviousstrengthreduction) then
  239. begin
  240. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,
  241. tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable);
  242. templist.Add(tempnode);
  243. inductionexprs.Add(n);
  244. CreateNodes;
  245. if lnf_backward in tfornode(arg).loopflags then
  246. addstatement(calccodestatements,
  247. geninlinenode(in_dec_x,false,
  248. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))))
  249. else
  250. addstatement(calccodestatements,
  251. geninlinenode(in_inc_x,false,
  252. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))));
  253. addstatement(initcodestatements,tempnode);
  254. addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
  255. caddnode.create(muln,
  256. caddnode.create(subn,tfornode(arg).right.getcopy,cordconstnode.create(1,tfornode(arg).right.resultdef,false)),
  257. taddnode(n).right.getcopy)
  258. )
  259. );
  260. { finally replace the node by a temp. ref }
  261. n:=ctemprefnode.create(tempnode);
  262. { ... and add a temp. release node }
  263. addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
  264. end;
  265. { set types }
  266. do_firstpass(n);
  267. result:=fen_norecurse_false;
  268. end;
  269. end;
  270. end;
  271. end;
  272. function OptimizeInductionVariablesSingleForLoop(node : tnode) : tnode;
  273. var
  274. loopcode,
  275. newcode : tblocknode;
  276. loopcodestatements,
  277. newcodestatements : tstatementnode;
  278. fornode : tfornode;
  279. begin
  280. result:=nil;
  281. if node.nodetype<>forn then
  282. exit;
  283. templist:=TFPList.Create;
  284. inductionexprs:=TFPList.Create;
  285. initcode:=nil;
  286. calccode:=nil;
  287. deletecode:=nil;
  288. initcodestatements:=nil;
  289. calccodestatements:=nil;
  290. deletecodestatements:=nil;
  291. { find all expressions being candidates for strength reduction
  292. and replace them }
  293. foreachnodestatic(pm_postprocess,node,@dostrengthreductiontest,node);
  294. { clue everything together }
  295. if assigned(initcode) then
  296. begin
  297. do_firstpass(initcode);
  298. do_firstpass(calccode);
  299. do_firstpass(deletecode);
  300. { create a new for node, the old one will be released by the compiler }
  301. with tfornode(node) do
  302. begin
  303. fornode:=cfornode.create(left,right,t1,t2,lnf_backward in loopflags);
  304. left:=nil;
  305. right:=nil;
  306. t1:=nil;
  307. t2:=nil;
  308. end;
  309. node:=fornode;
  310. loopcode:=internalstatements(loopcodestatements);
  311. addstatement(loopcodestatements,calccode);
  312. addstatement(loopcodestatements,tfornode(node).t2);
  313. tfornode(node).t2:=loopcode;
  314. do_firstpass(node);
  315. result:=internalstatements(newcodestatements);
  316. addstatement(newcodestatements,initcode);
  317. addstatement(newcodestatements,node);
  318. addstatement(newcodestatements,deletecode);
  319. end;
  320. templist.Free;
  321. inductionexprs.Free;
  322. end;
  323. function iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
  324. var
  325. hp : tnode;
  326. begin
  327. Result:=fen_false;
  328. if n.nodetype=forn then
  329. begin
  330. { do we have DFA available? }
  331. if pi_dfaavailable in current_procinfo.flags then
  332. begin
  333. CalcDefSum(n);
  334. end;
  335. containsnestedforloop:=false;
  336. hp:=OptimizeInductionVariablesSingleForLoop(n);
  337. if assigned(hp) then
  338. begin
  339. n.Free;
  340. n:=hp;
  341. end;
  342. { can we avoid further searching? }
  343. if not(containsnestedforloop) then
  344. Result:=fen_norecurse_false;
  345. end;
  346. end;
  347. function OptimizeInductionVariables(node : tnode) : boolean;
  348. begin
  349. changedforloop:=false;
  350. foreachnodestatic(pm_postprocess,node,@iterforloops,nil);
  351. Result:=changedforloop;
  352. end;
  353. end.