2
0

optcse.pas 9.2 KB

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