optcse.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. {
  2. Common subexpression elimination on base blocks
  3. Copyright (c) 2005 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 optcse;
  18. {$i fpcdefs.inc}
  19. { $define csedebug}
  20. interface
  21. uses
  22. node;
  23. function do_optcse(var rootnode : tnode) : tnode;
  24. implementation
  25. uses
  26. globtype,
  27. cclasses,
  28. nutils,
  29. nbas,nld,
  30. pass_1,
  31. symtype,symdef;
  32. const
  33. cseinvariant : set of tnodetype = [loadn,addn,muln,subn,divn,slashn,modn,andn,orn,xorn,notn,vecn,
  34. derefn,equaln,unequaln,ltn,gtn,lten,gten,typeconvn,subscriptn,
  35. inn,symdifn,shrn,shln,ordconstn,realconstn,unaryminusn,pointerconstn,stringconstn,setconstn,
  36. isn,asn,starstarn,nothingn];
  37. function searchsubdomain(var n:tnode; arg: pointer) : foreachnoderesult;
  38. begin
  39. if not(n.nodetype in cseinvariant) then
  40. begin
  41. pboolean(arg)^:=false;
  42. result:=fen_norecurse_true;
  43. end
  44. else
  45. result:=fen_true;
  46. end;
  47. type
  48. tlists = record
  49. nodelist : tfplist;
  50. locationlist : tfplist;
  51. end;
  52. plists = ^tlists;
  53. function collectnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  54. begin
  55. { node worth to add? }
  56. if (node_complexity(n)>1) and (tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable) then
  57. begin
  58. plists(arg)^.nodelist.Add(n);
  59. plists(arg)^.locationlist.Add(@n);
  60. result:=fen_false;
  61. end
  62. else
  63. result:=fen_norecurse_false;
  64. end;
  65. function searchcsedomain(var n: tnode; arg: pointer) : foreachnoderesult;
  66. var
  67. csedomain : boolean;
  68. lists : tlists;
  69. templist : tfplist;
  70. i,j : longint;
  71. def : tstoreddef;
  72. nodes : tblocknode;
  73. creates,
  74. statements : tstatementnode;
  75. hp : ttempcreatenode;
  76. begin
  77. result:=fen_false;
  78. if n.nodetype in cseinvariant then
  79. begin
  80. csedomain:=true;
  81. foreachnodestatic(pm_postprocess,n,@searchsubdomain,@csedomain);
  82. { found a cse domain }
  83. if csedomain then
  84. begin
  85. statements:=nil;
  86. result:=fen_norecurse_true;
  87. {$ifdef csedebug}
  88. writeln('============ cse domain ==================');
  89. printnode(output,n);
  90. {$endif csedebug}
  91. lists.nodelist:=tfplist.create;
  92. lists.locationlist:=tfplist.create;
  93. foreachnodestatic(pm_postprocess,n,@collectnodes,@lists);
  94. templist:=tfplist.create;
  95. templist.count:=lists.nodelist.count;
  96. { this is poorly coded, just comparing every node with all other nodes }
  97. for i:=0 to lists.nodelist.count-1 do
  98. for j:=i+1 to lists.nodelist.count-1 do
  99. begin
  100. if tnode(lists.nodelist[i]).isequal(tnode(lists.nodelist[j])) then
  101. begin
  102. if not(assigned(statements)) then
  103. begin
  104. nodes:=internalstatements(statements);
  105. addstatement(statements,internalstatements(creates));
  106. end;
  107. {$ifdef csedebug}
  108. writeln(' ==== ');
  109. printnode(output,tnode(lists.nodelist[i]));
  110. writeln(' equals ');
  111. printnode(output,tnode(lists.nodelist[j]));
  112. writeln(' ==== ');
  113. {$endif csedebug}
  114. def:=tstoreddef(tnode(lists.nodelist[i]).resultdef);
  115. if assigned(templist[i]) then
  116. begin
  117. templist[j]:=templist[i];
  118. pnode(lists.locationlist[j])^.free;
  119. pnode(lists.locationlist[j])^:=ctemprefnode.create(ttempcreatenode(templist[j]));
  120. do_firstpass(pnode(lists.locationlist[j])^);
  121. end
  122. else
  123. begin
  124. templist[i]:=ctempcreatenode.create(def,def.size,tt_persistent,
  125. def.is_intregable or def.is_fpuregable);
  126. addstatement(creates,tnode(templist[i]));
  127. { properties can't be passed by var }
  128. hp:=ttempcreatenode(templist[i]);
  129. do_firstpass(tnode(hp));
  130. addstatement(statements,cassignmentnode.create(ctemprefnode.create(ttempcreatenode(templist[i])),
  131. tnode(lists.nodelist[i])));
  132. pnode(lists.locationlist[i])^:=ctemprefnode.create(ttempcreatenode(templist[i]));
  133. do_firstpass(pnode(lists.locationlist[i])^);
  134. templist[j]:=templist[i];
  135. pnode(lists.locationlist[j])^.free;
  136. pnode(lists.locationlist[j])^:=ctemprefnode.create(ttempcreatenode(templist[j]));
  137. do_firstpass(pnode(lists.locationlist[j])^);
  138. {$ifdef csedebug}
  139. printnode(output,statements);
  140. {$endif csedebug}
  141. end;
  142. end;
  143. end;
  144. if assigned(statements) then
  145. begin
  146. addstatement(statements,n);
  147. n:=nodes;
  148. do_firstpass(n);
  149. {$ifdef csedebug}
  150. printnode(output,nodes);
  151. {$endif csedebug}
  152. end;
  153. {$ifdef csedebug}
  154. writeln('nodes: ',lists.nodelist.count);
  155. writeln('==========================================');
  156. {$endif csedebug}
  157. lists.nodelist.free;
  158. lists.locationlist.free;
  159. templist.free;
  160. end
  161. end;
  162. end;
  163. function do_optcse(var rootnode : tnode) : tnode;
  164. begin
  165. foreachnodestatic(pm_postprocess,rootnode,@searchcsedomain,nil);
  166. result:=nil;
  167. (*
  168. { create a linear list of nodes }
  169. { create hash values }
  170. { sort by hash values, taking care of nf_csebarrier and keeping the
  171. original order of the nodes }
  172. { compare nodes with equal hash values }
  173. { search barrier }
  174. for i:=0 to nodelist.length-1 do
  175. begin
  176. { and then search backward so we get always the largest equal trees }
  177. j:=i+1;
  178. { collect equal nodes }
  179. while (j<=nodelist.length-1) and
  180. nodelist[i].isequal(nodelist[j]) do
  181. inc(j);
  182. dec(j);
  183. if j>i then
  184. begin
  185. { cse found }
  186. { create temp. location }
  187. { replace first node by
  188. - temp. creation
  189. - expression calculation
  190. - assignment of expression to temp. }
  191. tempnode:=ctempcreatenode.create(nodelist[i].resultdef,nodelist[i].resultdef.size,tt_persistent,
  192. nodelist[i].resultdef.is_intregable or nodelist[i].resultdef.is_fpuregable);
  193. addstatement(createstatement,tempnode);
  194. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  195. caddrnode.create_internal(para.left)));
  196. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  197. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  198. { replace next nodes by loading the temp. reference }
  199. { replace last node by loading the temp. reference and
  200. delete the temp. }
  201. end;
  202. end;
  203. *)
  204. end;
  205. end.