optutils.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. implementation
  35. uses
  36. verbose,
  37. optbase,
  38. nbas,nflw,nutils;
  39. function TIndexedNodeSet.Add(node : tnode) : boolean;
  40. var
  41. i : Integer;
  42. p : tnode;
  43. begin
  44. node.allocoptinfo;
  45. p:=Includes(node);
  46. if assigned(p) then
  47. begin
  48. result:=false;
  49. node.optinfo^.index:=p.optinfo^.index;
  50. end
  51. else
  52. begin
  53. i:=inherited Add(node);
  54. node.optinfo^.index:=i;
  55. result:=true;
  56. end
  57. end;
  58. function TIndexedNodeSet.Includes(node : tnode) : tnode;
  59. var
  60. i : longint;
  61. begin
  62. for i:=0 to Count-1 do
  63. if tnode(List^[i]).isequal(node) then
  64. begin
  65. result:=tnode(List^[i]);
  66. exit;
  67. end;
  68. result:=nil;
  69. end;
  70. function TIndexedNodeSet.Remove(node : tnode) : boolean;
  71. var
  72. p : tnode;
  73. begin
  74. result:=false;
  75. p:=Includes(node);
  76. if assigned(p) then
  77. begin
  78. if inherited Remove(p)<>-1 then
  79. result:=true;
  80. end;
  81. end;
  82. procedure PrintIndexedNodeSet(var f : text;s : TIndexedNodeSet);
  83. var
  84. i : integer;
  85. begin
  86. for i:=0 to s.count-1 do
  87. begin
  88. writeln(f,'=============================== Node ',i,' ===============================');
  89. printnode(f,tnode(s[i]));
  90. writeln(f);
  91. end;
  92. end;
  93. function PrintNodeDFA(var n: tnode; arg: pointer): foreachnoderesult;
  94. begin
  95. if assigned(n.optinfo) and ((n.optinfo^.life<>nil) or (n.optinfo^.use<>nil) or (n.optinfo^.def<>nil)) then
  96. begin
  97. write(text(arg^),nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,') Life: ');
  98. PrintDFASet(text(arg^),n.optinfo^.life);
  99. write(text(arg^),' Def: ');
  100. PrintDFASet(text(arg^),n.optinfo^.def);
  101. write(text(arg^),' Use: ');
  102. PrintDFASet(text(arg^),n.optinfo^.use);
  103. writeln(text(arg^));
  104. end;
  105. result:=fen_false;
  106. end;
  107. procedure PrintDFAInfo(var f : text;p : tnode);
  108. begin
  109. foreachnodestatic(pm_postprocess,p,@PrintNodeDFA,@f);
  110. end;
  111. procedure SetNodeSucessors(p : tnode);
  112. var
  113. Continuestack : TFPList;
  114. Breakstack : TFPList;
  115. { sets the successor nodes of a node tree block
  116. returns the first node of the tree if it's a controll flow node }
  117. function DoSet(p : tnode;succ : tnode) : tnode;
  118. var
  119. hp1,hp2 : tnode;
  120. begin
  121. result:=nil;
  122. if p=nil then
  123. exit;
  124. case p.nodetype of
  125. statementn:
  126. begin
  127. hp1:=p;
  128. result:=p;
  129. while assigned(hp1) do
  130. begin
  131. { does another statement follow? }
  132. if assigned(tstatementnode(hp1).next) then
  133. begin
  134. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  135. if assigned(hp2) then
  136. tstatementnode(hp1).successor:=hp2
  137. else
  138. tstatementnode(hp1).successor:=tstatementnode(hp1).next;
  139. end
  140. else
  141. begin
  142. hp2:=DoSet(tstatementnode(hp1).statement,succ);
  143. if assigned(hp2) then
  144. tstatementnode(hp1).successor:=hp2
  145. else
  146. tstatementnode(hp1).successor:=succ;
  147. end;
  148. hp1:=tstatementnode(hp1).next;
  149. end;
  150. end;
  151. blockn:
  152. begin
  153. result:=p;
  154. DoSet(tblocknode(p).statements,succ);
  155. p.successor:=succ;
  156. end;
  157. forn:
  158. begin
  159. Breakstack.Add(succ);
  160. Continuestack.Add(p);
  161. result:=p;
  162. { the successor of the last node of the for body is the for node itself }
  163. DoSet(tfornode(p).t2,p);
  164. Breakstack.Delete(Breakstack.Count-1);
  165. Continuestack.Delete(Continuestack.Count-1);
  166. end;
  167. breakn:
  168. begin
  169. result:=p;
  170. p.successor:=tnode(Breakstack.Last);
  171. end;
  172. continuen:
  173. begin
  174. result:=p;
  175. p.successor:=tnode(Continuestack.Last);
  176. end;
  177. whilerepeatn:
  178. begin
  179. Breakstack.Add(succ);
  180. Continuestack.Add(p);
  181. result:=p;
  182. { the successor of the last node of the for body is the while node itself }
  183. DoSet(twhilerepeatnode(p).right,p);
  184. p.successor:=succ;
  185. Breakstack.Delete(Breakstack.Count-1);
  186. Continuestack.Delete(Continuestack.Count-1);
  187. end;
  188. ifn:
  189. begin
  190. result:=p;
  191. DoSet(tifnode(p).right,succ);
  192. DoSet(tifnode(p).t1,succ);
  193. p.successor:=succ;
  194. end;
  195. labeln:
  196. begin
  197. result:=p;
  198. if assigned(tlabelnode(p).left) then
  199. begin
  200. DoSet(tlabelnode(p).left,succ);
  201. p.successor:=tlabelnode(p).left;
  202. end
  203. else
  204. p.successor:=succ;
  205. end;
  206. assignn:
  207. begin
  208. result:=p;
  209. p.successor:=succ;
  210. end;
  211. goton:
  212. begin
  213. result:=p;
  214. if not(assigned(tgotonode(p).labelnode)) then
  215. internalerror(2007050701);
  216. p.successor:=tgotonode(p).labelnode;
  217. end;
  218. exitn:
  219. begin
  220. result:=p;
  221. p.successor:=nil;
  222. end;
  223. inlinen,
  224. calln,
  225. withn,
  226. casen,
  227. tryexceptn,
  228. raisen,
  229. tryfinallyn,
  230. onn,
  231. nothingn:
  232. internalerror(2007050501);
  233. end;
  234. end;
  235. begin
  236. Breakstack:=TFPList.Create;
  237. Continuestack:=TFPList.Create;
  238. DoSet(p,nil);
  239. Continuestack.Free;
  240. Breakstack.Free;
  241. end;
  242. end.