optutils.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. {
  2. Helper routines for the optimizer
  3. Copyright (c) 2007 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 optutils;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. node;
  23. type
  24. { this implementation should be really improved,
  25. its purpose is to find equal nodes }
  26. TIndexedNodeSet = class(TFPList)
  27. function Add(node : tnode) : boolean;
  28. function Includes(node : tnode) : tnode;
  29. function Remove(node : tnode) : boolean;
  30. end;
  31. procedure SetNodeSucessors(p : tnode);
  32. procedure PrintDFAInfo(var f : text;p : tnode);
  33. procedure PrintIndexedNodeSet(var f : text;s : TIndexedNodeSet);
  34. { determines the optinfo.defsum field for the given node
  35. this field contains a sum of all expressions defined by
  36. all child expressions reachable through p
  37. }
  38. procedure CalcDefSum(p : tnode);
  39. { returns true, if n is a valid node and has life info }
  40. function has_life_info(n : tnode) : boolean;
  41. implementation
  42. uses
  43. verbose,
  44. optbase,
  45. ncal,nbas,nflw,nutils,nset;
  46. function TIndexedNodeSet.Add(node : tnode) : boolean;
  47. var
  48. i : Integer;
  49. p : tnode;
  50. begin
  51. node.allocoptinfo;
  52. p:=Includes(node);
  53. if assigned(p) then
  54. begin
  55. result:=false;
  56. node.optinfo^.index:=p.optinfo^.index;
  57. end
  58. else
  59. begin
  60. i:=inherited Add(node);
  61. node.optinfo^.index:=i;
  62. result:=true;
  63. end
  64. end;
  65. function TIndexedNodeSet.Includes(node : tnode) : tnode;
  66. var
  67. i : longint;
  68. begin
  69. for i:=0 to Count-1 do
  70. if tnode(List^[i]).isequal(node) then
  71. begin
  72. result:=tnode(List^[i]);
  73. exit;
  74. end;
  75. result:=nil;
  76. end;
  77. function TIndexedNodeSet.Remove(node : tnode) : boolean;
  78. var
  79. p : tnode;
  80. begin
  81. result:=false;
  82. p:=Includes(node);
  83. if assigned(p) then
  84. begin
  85. if inherited Remove(p)<>-1 then
  86. result:=true;
  87. end;
  88. end;
  89. procedure PrintIndexedNodeSet(var f : text;s : TIndexedNodeSet);
  90. var
  91. i : integer;
  92. begin
  93. for i:=0 to s.count-1 do
  94. begin
  95. writeln(f,'=============================== Node ',i,' ===============================');
  96. printnode(f,tnode(s[i]));
  97. writeln(f);
  98. end;
  99. end;
  100. function PrintNodeDFA(var n: tnode; arg: pointer): foreachnoderesult;
  101. begin
  102. if assigned(n.optinfo) and ((n.optinfo^.life<>nil) or (n.optinfo^.use<>nil) or (n.optinfo^.def<>nil)) then
  103. begin
  104. write(text(arg^),nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,') Life: ');
  105. PrintDFASet(text(arg^),n.optinfo^.life);
  106. write(text(arg^),' Def: ');
  107. PrintDFASet(text(arg^),n.optinfo^.def);
  108. write(text(arg^),' Use: ');
  109. PrintDFASet(text(arg^),n.optinfo^.use);
  110. writeln(text(arg^));
  111. end;
  112. result:=fen_false;
  113. end;
  114. procedure PrintDFAInfo(var f : text;p : tnode);
  115. begin
  116. foreachnodestatic(pm_postprocess,p,@PrintNodeDFA,@f);
  117. end;
  118. procedure SetNodeSucessors(p : tnode);
  119. var
  120. Continuestack : TFPList;
  121. Breakstack : TFPList;
  122. { sets the successor nodes of a node tree block
  123. returns the first node of the tree if it's a controll flow node }
  124. function DoSet(p : tnode;succ : tnode) : tnode;
  125. var
  126. hp1,hp2 : tnode;
  127. i : longint;
  128. begin
  129. result:=nil;
  130. if p=nil then
  131. exit;
  132. case p.nodetype of
  133. statementn:
  134. begin
  135. hp1:=p;
  136. result:=p;
  137. while assigned(hp1) do
  138. begin
  139. { does another statement follow? }
  140. if assigned(tstatementnode(hp1).next) then
  141. begin
  142. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  143. if assigned(hp2) then
  144. tstatementnode(hp1).successor:=hp2
  145. else
  146. tstatementnode(hp1).successor:=tstatementnode(hp1).next;
  147. end
  148. else
  149. begin
  150. hp2:=DoSet(tstatementnode(hp1).statement,succ);
  151. if assigned(hp2) then
  152. tstatementnode(hp1).successor:=hp2
  153. else
  154. tstatementnode(hp1).successor:=succ;
  155. end;
  156. hp1:=tstatementnode(hp1).next;
  157. end;
  158. end;
  159. blockn:
  160. begin
  161. result:=p;
  162. DoSet(tblocknode(p).statements,succ);
  163. p.successor:=succ;
  164. end;
  165. forn:
  166. begin
  167. Breakstack.Add(succ);
  168. Continuestack.Add(p);
  169. result:=p;
  170. { the successor of the last node of the for body is the for node itself }
  171. DoSet(tfornode(p).t2,p);
  172. p.successor:=succ;
  173. Breakstack.Delete(Breakstack.Count-1);
  174. Continuestack.Delete(Continuestack.Count-1);
  175. p.successor:=succ;
  176. end;
  177. breakn:
  178. begin
  179. result:=p;
  180. p.successor:=tnode(Breakstack.Last);
  181. end;
  182. continuen:
  183. begin
  184. result:=p;
  185. p.successor:=tnode(Continuestack.Last);
  186. end;
  187. whilerepeatn:
  188. begin
  189. Breakstack.Add(succ);
  190. Continuestack.Add(p);
  191. result:=p;
  192. { the successor of the last node of the while/repeat body is the while node itself }
  193. DoSet(twhilerepeatnode(p).right,p);
  194. p.successor:=succ;
  195. Breakstack.Delete(Breakstack.Count-1);
  196. Continuestack.Delete(Continuestack.Count-1);
  197. end;
  198. ifn:
  199. begin
  200. result:=p;
  201. DoSet(tifnode(p).right,succ);
  202. DoSet(tifnode(p).t1,succ);
  203. p.successor:=succ;
  204. end;
  205. labeln:
  206. begin
  207. result:=p;
  208. if assigned(tlabelnode(p).left) then
  209. begin
  210. DoSet(tlabelnode(p).left,succ);
  211. p.successor:=tlabelnode(p).left;
  212. end
  213. else
  214. p.successor:=succ;
  215. end;
  216. assignn:
  217. begin
  218. result:=p;
  219. p.successor:=succ;
  220. end;
  221. goton:
  222. begin
  223. result:=p;
  224. if not(assigned(tgotonode(p).labelnode)) then
  225. internalerror(2007050701);
  226. p.successor:=tgotonode(p).labelnode;
  227. end;
  228. exitn:
  229. begin
  230. result:=p;
  231. p.successor:=nil;
  232. end;
  233. casen:
  234. begin
  235. result:=p;
  236. DoSet(tcasenode(p).elseblock,succ);
  237. for i:=0 to tcasenode(p).blocks.count-1 do
  238. DoSet(pcaseblock(tcasenode(p).blocks[i])^.statement,succ);
  239. p.successor:=succ;
  240. end;
  241. calln:
  242. begin
  243. { not sure if this is enough (FK) }
  244. result:=p;
  245. if not(cnf_call_never_returns in tcallnode(p).callnodeflags) then
  246. p.successor:=succ;
  247. end;
  248. inlinen:
  249. begin
  250. { not sure if this is enough (FK) }
  251. result:=p;
  252. p.successor:=succ;
  253. end;
  254. tempcreaten,
  255. tempdeleten,
  256. nothingn:
  257. begin
  258. result:=p;
  259. p.successor:=succ;
  260. end;
  261. raisen:
  262. begin
  263. result:=p;
  264. { raise never returns }
  265. p.successor:=nil;
  266. end;
  267. withn,
  268. tryexceptn,
  269. tryfinallyn,
  270. onn:
  271. internalerror(2007050501);
  272. end;
  273. end;
  274. begin
  275. Breakstack:=TFPList.Create;
  276. Continuestack:=TFPList.Create;
  277. DoSet(p,nil);
  278. Continuestack.Free;
  279. Breakstack.Free;
  280. end;
  281. var
  282. sum : TDFASet;
  283. function adddef(var n: tnode; arg: pointer): foreachnoderesult;
  284. begin
  285. if assigned(n.optinfo) then
  286. DFASetIncludeSet(sum,n.optinfo^.def);
  287. Result:=fen_false;
  288. end;
  289. procedure CalcDefSum(p : tnode);
  290. begin
  291. p.allocoptinfo;
  292. if not assigned(p.optinfo^.defsum) then
  293. begin
  294. sum:=nil;
  295. foreachnodestatic(pm_postprocess,p,@adddef,nil);
  296. p.optinfo^.defsum:=sum;
  297. end;
  298. end;
  299. function has_life_info(n : tnode) : boolean;
  300. begin
  301. result:=assigned(n) and assigned(n.optinfo) and
  302. assigned(n.optinfo^.life);
  303. end;
  304. end.