opttail.pas 7.6 KB

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