optcse.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. {
  25. the function creates non optimal code so far:
  26. - call para nodes are cse barriers because they can be reordered and thus the
  27. temp. creation can be done too late
  28. - cse's in chained expressions are not recognized: the common subexpression
  29. in (a1 and b and c) vs. (a2 and b and c) is not recognized because there is no common
  30. subtree b and c
  31. - the cse knows nothing about register pressure. In case of high register pressure, cse might
  32. have a negative impact
  33. - assignment nodes are currently cse borders: things like a[i,j]:=a[i,j]+1; are not improved
  34. - the list of cseinvariant node types and inline numbers is not complete yet
  35. Further, it could be done probably in a faster way though the complexity can't probably not reduced
  36. }
  37. function do_optcse(var rootnode : tnode) : tnode;
  38. implementation
  39. uses
  40. globtype,
  41. cclasses,
  42. verbose,
  43. nutils,
  44. procinfo,
  45. nbas,nld,ninl,ncal,ncnv,nadd,
  46. pass_1,
  47. symconst,symtype,symdef,
  48. defutil,
  49. optbase;
  50. const
  51. cseinvariant : set of tnodetype = [loadn,addn,muln,subn,divn,slashn,modn,andn,orn,xorn,notn,vecn,
  52. derefn,equaln,unequaln,ltn,gtn,lten,gten,typeconvn,subscriptn,
  53. inn,symdifn,shrn,shln,ordconstn,realconstn,unaryminusn,pointerconstn,stringconstn,setconstn,
  54. isn,asn,starstarn,nothingn,temprefn,loadparentfpn {,callparan}];
  55. function searchsubdomain(var n:tnode; arg: pointer) : foreachnoderesult;
  56. begin
  57. if (n.nodetype in cseinvariant) or
  58. ((n.nodetype=inlinen) and
  59. (tinlinenode(n).inlinenumber in [in_assigned_x])
  60. ) then
  61. result:=fen_true
  62. else
  63. begin
  64. pboolean(arg)^:=false;
  65. result:=fen_norecurse_true;
  66. end;
  67. end;
  68. type
  69. tlists = record
  70. nodelist : tfplist;
  71. locationlist : tfplist;
  72. equalto : tfplist;
  73. refs : tfplist;
  74. avail : TDFASet;
  75. end;
  76. plists = ^tlists;
  77. { collectnodes needs the address of itself to call foreachnodestatic,
  78. so we need a wrapper because @<func> inside <func doesn't work }
  79. function collectnodes(var n:tnode; arg: pointer) : foreachnoderesult;forward;
  80. function collectnodes2(var n:tnode; arg: pointer) : foreachnoderesult;
  81. begin
  82. result:=collectnodes(n,arg);
  83. end;
  84. function collectnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  85. var
  86. i,j : longint;
  87. begin
  88. result:=fen_false;
  89. { don't add the tree below an untyped const parameter: there is
  90. no information available that this kind of tree actually needs
  91. to be addresable, this could be improved }
  92. if ((n.nodetype=callparan) and
  93. (tcallparanode(n).left.resultdef.typ=formaldef) and
  94. (tcallparanode(n).parasym.varspez=vs_const)) then
  95. begin
  96. result:=fen_norecurse_false;
  97. exit;
  98. end;
  99. { so far, we can handle only nodes being read }
  100. if (n.flags*[nf_write,nf_modify]=[]) and
  101. { node possible to add? }
  102. assigned(n.resultdef) and
  103. (tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable) and
  104. { is_int/fpuregable allows arrays and records to be in registers, cse cannot handle this }
  105. not(n.resultdef.typ in [arraydef,recorddef]) and
  106. { same for voiddef }
  107. not(is_void(n.resultdef)) and
  108. { adding tempref nodes is worthless but their complexity is probably <= 1 anyways }
  109. not(n.nodetype in [temprefn]) and
  110. { node worth to add?
  111. We consider almost every node because even loading a variables from
  112. a register instead of memory is more beneficial. This behaviour should
  113. not increase register pressure because if a variable is already
  114. in a register, the reg. allocator can merge the nodes. If a variable
  115. is loaded from memory, loading this variable and spilling another register
  116. should not add a speed penalty.
  117. }
  118. {
  119. load nodes are not considered if they load para or local symbols from the
  120. current stack frame, those are in registers anyways if possible
  121. }
  122. (not(n.nodetype=loadn) or
  123. not(tloadnode(n).symtableentry.typ in [paravarsym,localvarsym]) or
  124. (tloadnode(n).symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel)
  125. ) and
  126. {
  127. Const nodes however are only considered if their complexity is >1
  128. This might be the case for the risc architectures if they need
  129. more than one instruction to load this particular value
  130. }
  131. (not(is_constnode(n)) or (node_complexity(n)>1)) then
  132. begin
  133. plists(arg)^.nodelist.Add(n);
  134. plists(arg)^.locationlist.Add(@n);
  135. plists(arg)^.refs.Add(nil);
  136. plists(arg)^.equalto.Add(pointer(-1));
  137. DFASetInclude(plists(arg)^.avail,plists(arg)^.nodelist.count-1);
  138. for i:=0 to plists(arg)^.nodelist.count-2 do
  139. begin
  140. if tnode(plists(arg)^.nodelist[i]).isequal(n) and DFASetIn(plists(arg)^.avail,i) then
  141. begin
  142. { use always the first occurence }
  143. if plists(arg)^.equalto[i]<>pointer(-1) then
  144. plists(arg)^.equalto[plists(arg)^.nodelist.count-1]:=plists(arg)^.equalto[i]
  145. else
  146. plists(arg)^.equalto[plists(arg)^.nodelist.count-1]:=pointer(i);
  147. plists(arg)^.refs[i]:=pointer(plists(arg)^.refs[i])+1;
  148. break;
  149. end;
  150. end;
  151. { boolean and/or require a special handling: after evaluating the and/or node,
  152. the expressions of the right side might not be available due to short boolean
  153. evaluation, so after handling the right side, mark those expressions
  154. as unavailable }
  155. if (n.nodetype in [orn,andn]) and is_boolean(taddnode(n).left.resultdef) then
  156. begin
  157. foreachnodestatic(pm_postprocess,taddnode(n).left,@collectnodes2,arg);
  158. j:=plists(arg)^.nodelist.count;
  159. foreachnodestatic(pm_postprocess,taddnode(n).right,@collectnodes2,arg);
  160. for i:=j to plists(arg)^.nodelist.count-1 do
  161. DFASetExclude(plists(arg)^.avail,i);
  162. result:=fen_norecurse_false;
  163. end;
  164. end;
  165. end;
  166. function searchcsedomain(var n: tnode; arg: pointer) : foreachnoderesult;
  167. var
  168. csedomain : boolean;
  169. lists : tlists;
  170. templist : tfplist;
  171. i : longint;
  172. def : tstoreddef;
  173. nodes : tblocknode;
  174. creates,
  175. statements : tstatementnode;
  176. hp : ttempcreatenode;
  177. begin
  178. result:=fen_false;
  179. if n.nodetype in cseinvariant then
  180. begin
  181. csedomain:=true;
  182. foreachnodestatic(pm_postprocess,n,@searchsubdomain,@csedomain);
  183. { found a cse domain }
  184. if csedomain then
  185. begin
  186. statements:=nil;
  187. result:=fen_norecurse_true;
  188. {$ifdef csedebug}
  189. writeln('============ cse domain ==================');
  190. printnode(output,n);
  191. writeln('Complexity: ',node_complexity(n));
  192. {$endif csedebug}
  193. lists.nodelist:=tfplist.create;
  194. lists.locationlist:=tfplist.create;
  195. lists.equalto:=tfplist.create;
  196. lists.refs:=tfplist.create;
  197. foreachnodestatic(pm_postprocess,n,@collectnodes,@lists);
  198. templist:=tfplist.create;
  199. templist.count:=lists.nodelist.count;
  200. { check all nodes if one is used more than once }
  201. for i:=0 to lists.nodelist.count-1 do
  202. begin
  203. { current node used more than once? }
  204. if assigned(lists.refs[i]) then
  205. begin
  206. if not(assigned(statements)) then
  207. begin
  208. nodes:=internalstatements(statements);
  209. addstatement(statements,internalstatements(creates));
  210. end;
  211. def:=tstoreddef(tnode(lists.nodelist[i]).resultdef);
  212. templist[i]:=ctempcreatenode.create_value(def,def.size,tt_persistent,
  213. def.is_intregable or def.is_fpuregable,tnode(lists.nodelist[i]));
  214. { make debugging easier and set temp. location to the original location }
  215. tnode(templist[i]).fileinfo:=tnode(lists.nodelist[i]).fileinfo;
  216. addstatement(creates,tnode(templist[i]));
  217. { make debugging easier and set temp. location to the original location }
  218. creates.fileinfo:=tnode(lists.nodelist[i]).fileinfo;
  219. hp:=ttempcreatenode(templist[i]);
  220. do_firstpass(tnode(hp));
  221. templist[i]:=hp;
  222. pnode(lists.locationlist[i])^:=ctemprefnode.create(ttempcreatenode(templist[i]));
  223. { make debugging easier and set temp. location to the original location }
  224. pnode(lists.locationlist[i])^.fileinfo:=tnode(lists.nodelist[i]).fileinfo;
  225. do_firstpass(pnode(lists.locationlist[i])^);
  226. {$ifdef csedebug}
  227. printnode(output,statements);
  228. {$endif csedebug}
  229. end
  230. { current node reference to another node? }
  231. else if lists.equalto[i]<>pointer(-1) then
  232. begin
  233. {$if defined(csedebug) or defined(csestats)}
  234. printnode(output,tnode(lists.nodelist[i]));
  235. writeln(i,' equals ',ptrint(lists.equalto[i]));
  236. printnode(output,tnode(lists.nodelist[ptrint(lists.equalto[i])]));
  237. {$endif defined(csedebug) or defined(csestats)}
  238. templist[i]:=templist[ptrint(lists.equalto[i])];
  239. pnode(lists.locationlist[i])^:=ctemprefnode.create(ttempcreatenode(templist[ptrint(lists.equalto[i])]));
  240. { make debugging easier and set temp. location to the original location }
  241. pnode(lists.locationlist[i])^.fileinfo:=tnode(lists.nodelist[i]).fileinfo;
  242. do_firstpass(pnode(lists.locationlist[i])^);
  243. end;
  244. end;
  245. { clean up unused trees }
  246. for i:=0 to lists.nodelist.count-1 do
  247. if lists.equalto[i]<>pointer(-1) then
  248. tnode(lists.nodelist[i]).free;
  249. {$ifdef csedebug}
  250. writeln('nodes: ',lists.nodelist.count);
  251. writeln('==========================================');
  252. {$endif csedebug}
  253. lists.nodelist.free;
  254. lists.locationlist.free;
  255. lists.equalto.free;
  256. lists.refs.free;
  257. templist.free;
  258. if assigned(statements) then
  259. begin
  260. { call para nodes need a special handling because
  261. they can be only children nodes of call nodes
  262. so the initialization code is inserted below the
  263. call para node
  264. }
  265. if n.nodetype=callparan then
  266. begin
  267. addstatement(statements,tcallparanode(n).left);
  268. tcallparanode(n).left:=nodes;
  269. do_firstpass(tcallparanode(n).left);
  270. end
  271. else
  272. begin
  273. addstatement(statements,n);
  274. n:=nodes;
  275. do_firstpass(n);
  276. end;
  277. {$ifdef csedebug}
  278. printnode(output,nodes);
  279. {$endif csedebug}
  280. end;
  281. end
  282. end;
  283. end;
  284. function do_optcse(var rootnode : tnode) : tnode;
  285. begin
  286. foreachnodestatic(pm_postprocess,rootnode,@searchcsedomain,nil);
  287. result:=nil;
  288. end;
  289. end.