opttail.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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)) or
  107. ((paranode.parasym.varspez=vs_value) and
  108. is_open_array(paranode.parasym.vardef));
  109. if useaddr then
  110. begin
  111. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  112. addstatement(calcstatements,tempnode);
  113. addstatement(calcstatements,
  114. cassignmentnode.create(
  115. ctemprefnode.create(tempnode),
  116. caddrnode.create_internal(paranode.left)
  117. ));
  118. end
  119. else
  120. begin
  121. tempnode:=ctempcreatenode.create(paranode.left.resultdef,paranode.left.resultdef.size,tt_persistent,true);
  122. addstatement(calcstatements,tempnode);
  123. addstatement(calcstatements,
  124. cassignmentnode.create_internal(
  125. ctemprefnode.create(tempnode),
  126. paranode.left
  127. ));
  128. end;
  129. { "cast" away const varspezs }
  130. loadnode:=cloadnode.create(paranode.parasym,paranode.parasym.owner);
  131. include(tloadnode(loadnode).loadnodeflags,loadnf_isinternal_ignoreconst);
  132. { load the address of the symbol instead of symbol }
  133. if useaddr then
  134. include(tloadnode(loadnode).loadnodeflags,loadnf_load_addr);
  135. addstatement(copystatements,
  136. cassignmentnode.create_internal(
  137. loadnode,
  138. ctemprefnode.create(tempnode)
  139. ));
  140. addstatement(copystatements,ctempdeletenode.create_normal_temp(tempnode));
  141. { reused }
  142. paranode.left:=nil;
  143. paranode:=tcallparanode(paranode.right);
  144. end;
  145. oldnodetree:=n;
  146. n:=internalstatements(nodes);
  147. if assigned(usedcallnode.callinitblock) then
  148. begin
  149. addstatement(nodes,usedcallnode.callinitblock);
  150. usedcallnode.callinitblock:=nil;
  151. end;
  152. addstatement(nodes,calcnodes);
  153. addstatement(nodes,copynodes);
  154. { create goto }
  155. addstatement(nodes,cgotonode.create(labelnode.labsym));
  156. if assigned(usedcallnode.callcleanupblock) then
  157. begin
  158. { callcleanupblock should contain only temp. node clean up }
  159. checktreenodetypes(usedcallnode.callcleanupblock,
  160. [tempdeleten,blockn,statementn,temprefn,nothingn]);
  161. addstatement(nodes,usedcallnode.callcleanupblock);
  162. usedcallnode.callcleanupblock:=nil;
  163. end;
  164. oldnodetree.free;
  165. do_firstpass(n);
  166. result:=true;
  167. end;
  168. end;
  169. blockn:
  170. result:=find_and_replace_tailcalls(tblocknode(n).left);
  171. else
  172. ;
  173. end;
  174. end;
  175. var
  176. s : tstatementnode;
  177. oldnodes : tnode;
  178. i : longint;
  179. labelsym : tlabelsym;
  180. begin
  181. { check if the parameters actually would support tail recursion elimination }
  182. for i:=0 to p.paras.count-1 do
  183. with tparavarsym(p.paras[i]) do
  184. if (varspez=vs_out) or
  185. { parameters requiring tables are too complicated to handle
  186. and slow down things anyways so a tail recursion call
  187. makes no sense
  188. }
  189. is_managed_type(vardef) then
  190. exit;
  191. labelsym:=clabelsym.create('$opttail');
  192. labelnode:=clabelnode.create(cnothingnode.create,labelsym);
  193. if find_and_replace_tailcalls(n) then
  194. begin
  195. oldnodes:=n;
  196. n:=internalstatements(s);
  197. addstatement(s,labelnode);
  198. addstatement(s,oldnodes);
  199. end
  200. else
  201. labelnode.free;
  202. end;
  203. end.