opttail.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {
  2. Tail recursion optimization
  3. Copyright (c) 2006 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 opttail;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symdef,node;
  22. procedure do_opttail(var n : tnode;p : tprocdef);
  23. implementation
  24. uses
  25. globtype,
  26. symconst,symsym,
  27. defcmp,defutil,
  28. nutils,nbas,nflw,ncal,nld,ncnv,
  29. pass_1,
  30. paramgr;
  31. procedure do_opttail(var n : tnode;p : tprocdef);
  32. var
  33. labelnode : tlabelnode;
  34. function find_and_replace_tailcalls(var n : tnode) : boolean;
  35. var
  36. usedcallnode : tcallnode;
  37. function has_copyback_paras(call: tcallnode): boolean;
  38. var
  39. n: tcallparanode;
  40. begin
  41. n:=tcallparanode(call.left);
  42. result:=false;
  43. while assigned(n) do
  44. begin
  45. if assigned(n.fparacopyback) then
  46. begin
  47. result:=true;
  48. exit;
  49. end;
  50. n:=tcallparanode(n.right);
  51. end;
  52. end;
  53. function is_optimizable_recursivecall(n : tnode) : boolean;
  54. begin
  55. result:=
  56. (n.nodetype=calln) and
  57. (tcallnode(n).procdefinition=p) and
  58. not(assigned(tcallnode(n).methodpointer)) and
  59. not has_copyback_paras(tcallnode(n));
  60. if result then
  61. usedcallnode:=tcallnode(n)
  62. else
  63. { obsolete type cast? }
  64. result:=((n.nodetype=typeconvn) and (ttypeconvnode(n).convtype=tc_equal) and is_optimizable_recursivecall(ttypeconvnode(n).left));
  65. end;
  66. function is_resultassignment(n : tnode) : boolean;
  67. begin
  68. result:=((n.nodetype=loadn) and (tloadnode(n).symtableentry=p.funcretsym)) or
  69. ((n.nodetype=typeconvn) and (ttypeconvnode(n).convtype=tc_equal) and is_resultassignment(ttypeconvnode(n).left));
  70. end;
  71. var
  72. calcnodes,
  73. copynodes,
  74. hp : tnode;
  75. nodes,
  76. calcstatements,
  77. copystatements : tstatementnode;
  78. paranode : tcallparanode;
  79. tempnode : ttempcreatenode;
  80. loadnode : tloadnode;
  81. oldnodetree : tnode;
  82. begin
  83. { no tail call found and replaced so far }
  84. result:=false;
  85. if n=nil then
  86. exit;
  87. usedcallnode:=nil;
  88. case n.nodetype of
  89. statementn:
  90. begin
  91. hp:=n;
  92. { search last node }
  93. while assigned(tstatementnode(hp).right) do
  94. hp:=tstatementnode(hp).right;
  95. result:=find_and_replace_tailcalls(tstatementnode(hp).left);
  96. end;
  97. ifn:
  98. begin
  99. result:=find_and_replace_tailcalls(tifnode(n).right);
  100. { avoid short bool eval here }
  101. result:=find_and_replace_tailcalls(tifnode(n).t1) or result;
  102. end;
  103. calln,
  104. assignn:
  105. begin
  106. if ((n.nodetype=calln) and is_optimizable_recursivecall(n)) or
  107. ((n.nodetype=assignn) and is_resultassignment(tbinarynode(n).left) and
  108. is_optimizable_recursivecall(tbinarynode(n).right)) then
  109. begin
  110. { found one! }
  111. {
  112. writeln('tail recursion optimization for ',p.mangledname);
  113. printnode(output,n);
  114. }
  115. { create assignments for all parameters }
  116. { this is hairy to do because one parameter could be used to calculate another one, so
  117. assign them first to temps and then add them }
  118. calcnodes:=internalstatements(calcstatements);
  119. copynodes:=internalstatements(copystatements);
  120. paranode:=tcallparanode(usedcallnode.left);
  121. while assigned(paranode) do
  122. begin
  123. if assigned(paranode.fparainit) then
  124. begin
  125. addstatement(calcstatements,paranode.fparainit);
  126. paranode.fparainit:=nil;
  127. end;
  128. tempnode:=ctempcreatenode.create(paranode.left.resultdef,paranode.left.resultdef.size,tt_persistent,true);
  129. addstatement(calcstatements,tempnode);
  130. addstatement(calcstatements,
  131. cassignmentnode.create(
  132. ctemprefnode.create(tempnode),
  133. paranode.left
  134. ));
  135. { "cast" away const varspezs }
  136. loadnode:=cloadnode.create(paranode.parasym,paranode.parasym.owner);
  137. include(tloadnode(loadnode).loadnodeflags,loadnf_isinternal_ignoreconst);
  138. addstatement(copystatements,
  139. cassignmentnode.create(
  140. loadnode,
  141. ctemprefnode.create(tempnode)
  142. ));
  143. addstatement(copystatements,ctempdeletenode.create_normal_temp(tempnode));
  144. { reused }
  145. paranode.left:=nil;
  146. paranode:=tcallparanode(paranode.right);
  147. end;
  148. oldnodetree:=n;
  149. n:=internalstatements(nodes);
  150. if assigned(usedcallnode.callinitblock) then
  151. begin
  152. addstatement(nodes,usedcallnode.callinitblock);
  153. usedcallnode.callinitblock:=nil;
  154. end;
  155. addstatement(nodes,calcnodes);
  156. addstatement(nodes,copynodes);
  157. { create goto }
  158. addstatement(nodes,cgotonode.create(labelnode.labsym));
  159. if assigned(usedcallnode.callcleanupblock) then
  160. begin
  161. { callcleanupblock should contain only temp. node clean up }
  162. checktreenodetypes(usedcallnode.callcleanupblock,
  163. [tempdeleten,blockn,statementn,temprefn,nothingn]);
  164. addstatement(nodes,usedcallnode.callcleanupblock);
  165. usedcallnode.callcleanupblock:=nil;
  166. end;
  167. oldnodetree.free;
  168. do_firstpass(n);
  169. result:=true;
  170. end;
  171. end;
  172. blockn:
  173. result:=find_and_replace_tailcalls(tblocknode(n).left);
  174. end;
  175. end;
  176. var
  177. s : tstatementnode;
  178. oldnodes : tnode;
  179. i : longint;
  180. labelsym : tlabelsym;
  181. begin
  182. { check if the parameters actually would support tail recursion elimination }
  183. for i:=0 to p.paras.count-1 do
  184. with tparavarsym(p.paras[i]) do
  185. if (varspez in [vs_out,vs_var,vs_constref]) or
  186. ((varspez=vs_const) and
  187. (paramanager.push_addr_param(varspez,vardef,p.proccalloption)) or
  188. { parameters requiring tables are too complicated to handle
  189. and slow down things anyways so a tail recursion call
  190. makes no sense
  191. }
  192. is_managed_type(vardef)) then
  193. exit;
  194. labelsym:=clabelsym.create('$opttail');
  195. labelnode:=clabelnode.create(cnothingnode.create,labelsym);
  196. if find_and_replace_tailcalls(n) then
  197. begin
  198. oldnodes:=n;
  199. n:=internalstatements(s);
  200. addstatement(s,labelnode);
  201. addstatement(s,oldnodes);
  202. end
  203. else
  204. labelnode.free;
  205. end;
  206. end.