opttail.pas 8.7 KB

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