opttail.pas 7.6 KB

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