optcse.pas 8.7 KB

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