optcse.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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,andn,orn,xorn,notn,vecn,derefn];
  34. function searchsubdomain(var n:tnode; arg: pointer) : foreachnoderesult;
  35. begin
  36. if not(n.nodetype in cseinvariant) then
  37. begin
  38. pboolean(arg)^:=false;
  39. result:=fen_norecurse_true;
  40. end
  41. else
  42. result:=fen_true;
  43. end;
  44. type
  45. tlists = record
  46. nodelist : tfplist;
  47. locationlist : tfplist;
  48. end;
  49. plists = ^tlists;
  50. pnode = ^tnode;
  51. function collectnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  52. begin
  53. { node worth to add? }
  54. if node_complexity(n)>1 then
  55. begin
  56. plists(arg)^.nodelist.Add(n);
  57. plists(arg)^.locationlist.Add(@n);
  58. result:=fen_false;
  59. end
  60. else
  61. result:=fen_norecurse_false;
  62. end;
  63. function searchcsedomain(var n: tnode; arg: pointer) : foreachnoderesult;
  64. var
  65. csedomain : boolean;
  66. lists : tlists;
  67. templist : tfplist;
  68. i,j : aint;
  69. def : tstoreddef;
  70. nodes : tblocknode;
  71. statements : tstatementnode;
  72. begin
  73. result:=fen_false;
  74. if n.nodetype in cseinvariant then
  75. begin
  76. csedomain:=true;
  77. foreachnodestatic(pm_postprocess,n,@searchsubdomain,@csedomain);
  78. { found a cse domain }
  79. if csedomain then
  80. begin
  81. statements:=nil;
  82. result:=fen_norecurse_true;
  83. {$ifdef csedebug}
  84. writeln('============ cse domain ==================');
  85. printnode(output,n);
  86. {$endif csedebug}
  87. lists.nodelist:=tfplist.create;
  88. lists.locationlist:=tfplist.create;
  89. foreachnodestatic(pm_postprocess,n,@collectnodes,@lists);
  90. templist:=tfplist.create;
  91. templist.count:=lists.nodelist.count;
  92. { this is poorly coded, just comparing every node with all other nodes }
  93. for i:=0 to lists.nodelist.count-1 do
  94. for j:=i+1 to lists.nodelist.count-1 do
  95. begin
  96. if tnode(lists.nodelist[i]).isequal(tnode(lists.nodelist[j])) then
  97. begin
  98. if not(assigned(statements)) then
  99. nodes:=internalstatements(statements);
  100. {$ifdef csedebug}
  101. writeln(' ==== ');
  102. printnode(output,tnode(lists.nodelist[i]));
  103. writeln(' equals ');
  104. printnode(output,tnode(lists.nodelist[j]));
  105. writeln(' ==== ');
  106. {$endif csedebug}
  107. def:=tstoreddef(tnode(lists.nodelist[i]).resultdef);
  108. if assigned(templist[i]) then
  109. begin
  110. templist[j]:=templist[i];
  111. pnode(lists.locationlist[j])^.free;
  112. pnode(lists.locationlist[j])^:=ctemprefnode.create(ttempcreatenode(templist[j]));
  113. do_firstpass(pnode(lists.locationlist[j])^);
  114. end
  115. else
  116. begin
  117. templist[i]:=ctempcreatenode.create(def,def.size,tt_persistent,
  118. def.is_intregable or def.is_fpuregable);
  119. addstatement(statements,tnode(templist[i]));
  120. addstatement(statements,cassignmentnode.create(ctemprefnode.create(ttempcreatenode(templist[i])),
  121. tnode(lists.nodelist[i])));
  122. pnode(lists.locationlist[i])^:=ctemprefnode.create(ttempcreatenode(templist[i]));
  123. do_firstpass(pnode(lists.locationlist[i])^);
  124. templist[j]:=templist[i];
  125. pnode(lists.locationlist[j])^.free;
  126. pnode(lists.locationlist[j])^:=ctemprefnode.create(ttempcreatenode(templist[j]));
  127. do_firstpass(pnode(lists.locationlist[j])^);
  128. {$ifdef csedebug}
  129. printnode(output,statements);
  130. {$endif csedebug}
  131. end;
  132. end;
  133. end;
  134. if assigned(statements) then
  135. begin
  136. addstatement(statements,n);
  137. n:=nodes;
  138. do_firstpass(n);
  139. {$ifdef csedebug}
  140. printnode(output,nodes);
  141. {$endif csedebug}
  142. end;
  143. {$ifdef csedebug}
  144. writeln('nodes: ',lists.nodelist.count);
  145. writeln('==========================================');
  146. {$endif csedebug}
  147. lists.nodelist.free;
  148. lists.locationlist.free;
  149. templist.free;
  150. end
  151. end;
  152. end;
  153. function do_optcse(var rootnode : tnode) : tnode;
  154. begin
  155. foreachnodestatic(pm_postprocess,rootnode,@searchcsedomain,nil);
  156. result:=nil;
  157. (*
  158. { create a linear list of nodes }
  159. { create hash values }
  160. { sort by hash values, taking care of nf_csebarrier and keeping the
  161. original order of the nodes }
  162. { compare nodes with equal hash values }
  163. { search barrier }
  164. for i:=0 to nodelist.length-1 do
  165. begin
  166. { and then search backward so we get always the largest equal trees }
  167. j:=i+1;
  168. { collect equal nodes }
  169. while (j<=nodelist.length-1) and
  170. nodelist[i].docompare(nodelist[j]) do
  171. inc(j);
  172. dec(j);
  173. if j>i then
  174. begin
  175. { cse found }
  176. { create temp. location }
  177. { replace first node by
  178. - temp. creation
  179. - expression calculation
  180. - assignment of expression to temp. }
  181. tempnode:=ctempcreatenode.create(nodelist[i].resultdef,nodelist[i].resultdef.size,tt_persistent,
  182. nodelist[i].resultdef.is_intregable or nodelist[i].resultdef.is_fpuregable);
  183. addstatement(createstatement,tempnode);
  184. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  185. caddrnode.create_internal(para.left)));
  186. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  187. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  188. { replace next nodes by loading the temp. reference }
  189. { replace last node by loading the temp. reference and
  190. delete the temp. }
  191. end;
  192. end;
  193. *)
  194. end;
  195. end.