optconstprop.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. {
  2. Constant propagation across statements
  3. Copyright (c) 2005-2012 by Jeppe Johansen and 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 optconstprop;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_CONSTPROP}
  20. interface
  21. uses
  22. node;
  23. { does constant propagation for rootnode
  24. The approach is simple: It search for constant assignment statements. As soon as such
  25. a statement is found, the following statements are search if they contain
  26. a usage of the assigned variable. If this is a the case, the variable is
  27. replaced by the constant. This does not work across points where the
  28. program flow joins so e.g.
  29. if ... then
  30. ...
  31. a:=1;
  32. ...
  33. else
  34. ...
  35. a:=1;
  36. ...
  37. writeln(a);
  38. will not result in any constant propagation.
  39. }
  40. function do_optconstpropagate(var rootnode : tnode) : tnode;
  41. implementation
  42. uses
  43. pass_1,procinfo,compinnr,
  44. symsym, symconst,
  45. nutils, nbas, ncnv, nld, nflw, ncal, ninl;
  46. function check_written(var n: tnode; arg: pointer): foreachnoderesult;
  47. begin
  48. result:=fen_false;
  49. if n.isequal(tnode(arg)) and
  50. ((n.flags*[nf_write,nf_modify])<>[]) then
  51. begin
  52. result:=fen_norecurse_true;
  53. end;
  54. end;
  55. { propagates the constant assignment passed in arg into n }
  56. function replaceBasicAssign(var n: tnode; arg: tnode; var tree_modified: boolean): boolean;
  57. var
  58. st2, oldnode: tnode;
  59. old: pnode;
  60. changed, tree_modified2,tree_modified3: boolean;
  61. written : Boolean;
  62. begin
  63. result:=true;
  64. if n = nil then
  65. exit;
  66. tree_modified:=false;
  67. tree_modified2:=false;
  68. tree_modified3:=false;
  69. { while it might be usefull, to use foreach to iterate all nodes, it is safer to
  70. iterate manually here so we have full controll how all nodes are processed }
  71. { We cannot analyze beyond those nodes, so we terminate to be on the safe side }
  72. if (n.nodetype in [addrn,derefn,asmn,casen,whilerepeatn,labeln,continuen,breakn,
  73. tryexceptn,raisen,tryfinallyn,onn,loadparentfpn,loadvmtaddrn,guidconstn,rttin,addoptn,asn,goton,
  74. objcselectorn,objcprotocoln]) then
  75. exit(false)
  76. else if n.nodetype=assignn then
  77. begin
  78. tree_modified:=false;
  79. { we can propage the constant in both branches because the evaluation order is not defined }
  80. result:=replaceBasicAssign(tassignmentnode(n).right, arg, tree_modified);
  81. { do not use the intuitive way result:=result and replace... because this would prevent
  82. replaceBasicAssign being called if the result is already false }
  83. result:=replaceBasicAssign(tassignmentnode(n).left, arg, tree_modified2) and result;
  84. tree_modified:=tree_modified or tree_modified2;
  85. { but we have to check if left writes to the currently searched variable ... }
  86. written:=foreachnodestatic(pm_postprocess, tassignmentnode(n).left, @check_written, tassignmentnode(arg).left);
  87. { ... if this is the case, we have to stop searching }
  88. result:=result and not(written);
  89. end
  90. else if n.isequal(tassignmentnode(arg).left) and ((n.flags*[nf_write,nf_modify])=[]) then
  91. begin
  92. n.Free;
  93. n:=tassignmentnode(arg).right.getcopy;
  94. inserttypeconv_internal(n, tassignmentnode(arg).left.resultdef);
  95. tree_modified:=true;
  96. end
  97. else if n.nodetype=statementn then
  98. result:=replaceBasicAssign(tstatementnode(n).left, arg, tree_modified)
  99. else if n.nodetype=forn then
  100. begin
  101. result:=replaceBasicAssign(tfornode(n).right, arg, tree_modified);
  102. if result then
  103. replaceBasicAssign(tfornode(n).t1, arg, tree_modified2);
  104. tree_modified:=tree_modified or tree_modified2;
  105. { after a for node we cannot continue with our simple approach }
  106. result:=false;
  107. end
  108. else if n.nodetype=blockn then
  109. begin
  110. changed:=false;
  111. st2:=tstatementnode(tblocknode(n).statements);
  112. old:=@tblocknode(n).statements;
  113. while assigned(st2) do
  114. begin
  115. repeat
  116. oldnode:=st2;
  117. tree_modified2:=false;
  118. if not replaceBasicAssign(st2, arg, tree_modified2) then
  119. begin
  120. old^:=st2;
  121. oldnode:=nil;
  122. changed:=changed or tree_modified2;
  123. result:=false;
  124. break;
  125. end
  126. else
  127. old^:=st2;
  128. changed:=changed or tree_modified2;
  129. until oldnode=st2;
  130. if oldnode = nil then
  131. break;
  132. old:=@tstatementnode(st2).next;
  133. st2:=tstatementnode(st2).next;
  134. end;
  135. tree_modified:=changed;
  136. end
  137. else if n.nodetype=ifn then
  138. begin
  139. result:=replaceBasicAssign(tifnode(n).left, arg, tree_modified);
  140. if result then
  141. begin
  142. if assigned(tifnode(n).t1) then
  143. begin
  144. { we can propagate the constant in both branches of an if statement
  145. because even if the the branch writes to it, the else branch gets the
  146. unmodified value }
  147. result:=replaceBasicAssign(tifnode(n).right, arg, tree_modified2);
  148. { do not use the intuitive way result:=result and replace... because this would prevent
  149. replaceBasicAssign being called if the result is already false }
  150. result:=replaceBasicAssign(tifnode(n).t1, arg, tree_modified3) and result;
  151. tree_modified:=tree_modified or tree_modified2 or tree_modified3;
  152. end
  153. else
  154. begin
  155. result:=replaceBasicAssign(tifnode(n).right, arg, tree_modified2);
  156. tree_modified:=tree_modified or tree_modified2;
  157. end;
  158. end;
  159. end
  160. else if n.nodetype=inlinen then
  161. begin
  162. { constant inc'ed/dec'ed? }
  163. if (tinlinenode(n).inlinenumber=in_dec_x) or (tinlinenode(n).inlinenumber=in_inc_x) then
  164. begin
  165. if tnode(tassignmentnode(arg).left).isequal(tcallparanode(tinlinenode(n).left).left) and
  166. (not(assigned(tcallparanode(tinlinenode(n).left).right)) or
  167. (tcallparanode(tcallparanode(tinlinenode(n).left).right).left.nodetype=ordconstn)) then
  168. begin
  169. { if the node just being searched is inc'ed/dec'ed then replace the inc/dec
  170. by add/sub and force a second replacement pass }
  171. oldnode:=n;
  172. n:=tinlinenode(n).getaddsub_for_incdec;
  173. oldnode.free;
  174. tree_modified:=true;
  175. { do not continue, value changed, if further const. propagations are possible, this is done
  176. by the next pass }
  177. result:=false;
  178. exit;
  179. end;
  180. end
  181. else if might_have_sideeffects(n) then
  182. exit(false);
  183. replaceBasicAssign(tunarynode(n).left, arg, tree_modified);
  184. result:=false;
  185. end
  186. else if n.nodetype=calln then
  187. exit(false)
  188. else if n.InheritsFrom(tbinarynode) then
  189. begin
  190. result:=replaceBasicAssign(tbinarynode(n).left, arg, tree_modified);
  191. if result then
  192. result:=replaceBasicAssign(tbinarynode(n).right, arg, tree_modified2);
  193. tree_modified:=tree_modified or tree_modified2;
  194. end
  195. else if n.InheritsFrom(tunarynode) then
  196. begin
  197. result:=replaceBasicAssign(tunarynode(n).left, arg, tree_modified);
  198. end;
  199. if n.nodetype<>callparan then
  200. begin
  201. if tree_modified then
  202. exclude(n.flags,nf_pass1_done);
  203. do_firstpass(n);
  204. end;
  205. end;
  206. function propagate(var n: tnode; arg: pointer): foreachnoderesult;
  207. var
  208. l,
  209. st, st2, oldnode: tnode;
  210. old: pnode;
  211. a: tassignmentnode;
  212. tree_mod, changed: boolean;
  213. begin
  214. result:=fen_true;
  215. changed:=false;
  216. PBoolean(arg)^:=false;
  217. if not assigned(n) then
  218. exit;
  219. if n.nodetype in [calln] then
  220. exit(fen_norecurse_true);
  221. if n.nodetype=blockn then
  222. begin
  223. st:=tblocknode(n).statements;
  224. while assigned(st) and
  225. (st.nodetype=statementn) and
  226. assigned(tstatementnode(st).statement) do
  227. begin
  228. if tstatementnode(st).statement.nodetype=assignn then
  229. begin
  230. a:=tassignmentnode(tstatementnode(st).statement);
  231. l:=a.left;
  232. if ((((l.nodetype=loadn) and
  233. { its address cannot have escaped the current routine }
  234. not(tabstractvarsym(tloadnode(l).symtableentry).addr_taken)) and
  235. ((
  236. (tloadnode(l).symtableentry.typ=localvarsym) and
  237. (tloadnode(l).symtable=current_procinfo.procdef.localst)
  238. ) or
  239. ((tloadnode(l).symtableentry.typ=paravarsym) and
  240. (tloadnode(l).symtable=current_procinfo.procdef.parast)
  241. ) or
  242. ((tloadnode(l).symtableentry.typ=staticvarsym) and
  243. (tloadnode(l).symtable.symtabletype=staticsymtable)
  244. )
  245. )) or
  246. (l.nodetype = temprefn)) and
  247. (is_constintnode(a.right) or
  248. is_constboolnode(a.right) or
  249. is_constcharnode(a.right) or
  250. is_constenumnode(a.right) or
  251. is_conststringnode(a.right)) then
  252. begin
  253. st2:=tstatementnode(tstatementnode(st).right);
  254. old:=@tstatementnode(st).right;
  255. while assigned(st2) do
  256. begin
  257. repeat
  258. oldnode:=st2;
  259. { Simple assignment of constant found }
  260. tree_mod:=false;
  261. if not replaceBasicAssign(st2, a, tree_mod) then
  262. begin
  263. old^:=st2;
  264. oldnode:=nil;
  265. changed:=changed or tree_mod;
  266. break;
  267. end
  268. else
  269. old^:=st2;
  270. changed:=changed or tree_mod;
  271. until oldnode=st2;
  272. if oldnode = nil then
  273. break;
  274. old:=@tstatementnode(st2).next;
  275. st2:=tstatementnode(st2).next;
  276. end;
  277. end;
  278. end;
  279. st:=tstatementnode(st).next;
  280. end;
  281. end;
  282. PBoolean(arg)^:=changed;
  283. end;
  284. function do_optconstpropagate(var rootnode: tnode): tnode;
  285. var
  286. changed: boolean;
  287. runsimplify : Boolean;
  288. begin
  289. {$ifdef DEBUG_CONSTPROP}
  290. writeln('************************ before constant propagation ***************************');
  291. printnode(rootnode);
  292. {$endif DEBUG_CONSTPROP}
  293. runsimplify:=false;
  294. repeat
  295. changed:=false;
  296. foreachnodestatic(pm_postandagain, rootnode, @propagate, @changed);
  297. runsimplify:=runsimplify or changed;
  298. until changed=false;
  299. if runsimplify then
  300. doinlinesimplify(rootnode);
  301. {$ifdef DEBUG_CONSTPROP}
  302. writeln('************************ after constant propagation ***************************');
  303. printnode(rootnode);
  304. writeln('*******************************************************************************');
  305. {$endif DEBUG_CONSTPROP}
  306. result:=rootnode;
  307. end;
  308. end.