optutils.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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,last : 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,ncon;
  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. if assigned(n.successor) then
  111. write(text(arg^),' Successor: ',nodetype2str[n.successor.nodetype],'(',n.successor.fileinfo.line,',',n.successor.fileinfo.column,')')
  112. else
  113. write(text(arg^),' Successor: nil');
  114. writeln(text(arg^));
  115. end;
  116. result:=fen_false;
  117. end;
  118. procedure PrintDFAInfo(var f : text;p : tnode);
  119. begin
  120. foreachnodestatic(pm_postprocess,p,@PrintNodeDFA,@f);
  121. end;
  122. procedure SetNodeSucessors(p,last : tnode);
  123. var
  124. Continuestack : TFPList;
  125. Breakstack : TFPList;
  126. { sets the successor nodes of a node tree block
  127. returns the first node of the tree if it's a controll flow node }
  128. function DoSet(p : tnode;succ : tnode) : tnode;
  129. var
  130. hp1,hp2 : tnode;
  131. i : longint;
  132. begin
  133. result:=nil;
  134. if p=nil then
  135. exit;
  136. case p.nodetype of
  137. statementn:
  138. begin
  139. hp1:=p;
  140. result:=p;
  141. while assigned(hp1) do
  142. begin
  143. { does another statement follow? }
  144. if assigned(tstatementnode(hp1).next) then
  145. begin
  146. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  147. if assigned(hp2) then
  148. tstatementnode(hp1).successor:=hp2
  149. else
  150. tstatementnode(hp1).successor:=tstatementnode(hp1).next;
  151. end
  152. else
  153. begin
  154. hp2:=DoSet(tstatementnode(hp1).statement,succ);
  155. if assigned(hp2) then
  156. tstatementnode(hp1).successor:=hp2
  157. else
  158. tstatementnode(hp1).successor:=succ;
  159. end;
  160. hp1:=tstatementnode(hp1).next;
  161. end;
  162. end;
  163. blockn:
  164. begin
  165. result:=p;
  166. DoSet(tblocknode(p).statements,succ);
  167. if assigned(tblocknode(p).statements) then
  168. p.successor:=tblocknode(p).statements
  169. else
  170. p.successor:=succ;
  171. end;
  172. forn:
  173. begin
  174. Breakstack.Add(succ);
  175. Continuestack.Add(p);
  176. result:=p;
  177. { the successor of the last node of the for body is the dummy loop iteration node
  178. it allows the dfa to inject needed life information into the loop }
  179. tfornode(p).loopiteration:=cnothingnode.create;
  180. DoSet(tfornode(p).t2,tfornode(p).loopiteration);
  181. p.successor:=succ;
  182. Breakstack.Delete(Breakstack.Count-1);
  183. Continuestack.Delete(Continuestack.Count-1);
  184. end;
  185. breakn:
  186. begin
  187. result:=p;
  188. p.successor:=tnode(Breakstack.Last);
  189. end;
  190. continuen:
  191. begin
  192. result:=p;
  193. p.successor:=tnode(Continuestack.Last);
  194. end;
  195. whilerepeatn:
  196. begin
  197. Breakstack.Add(succ);
  198. Continuestack.Add(p);
  199. result:=p;
  200. { the successor of the last node of the while/repeat body is the while node itself }
  201. DoSet(twhilerepeatnode(p).right,p);
  202. p.successor:=succ;
  203. { special case: we do not do a dyn. dfa, but we should handle endless loops }
  204. if is_constboolnode(twhilerepeatnode(p).left) then
  205. begin
  206. if ((lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  207. getbooleanvalue(twhilerepeatnode(p).left)) or (not(lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  208. not(getbooleanvalue(twhilerepeatnode(p).left))) then
  209. p.successor:=nil;
  210. end;
  211. Breakstack.Delete(Breakstack.Count-1);
  212. Continuestack.Delete(Continuestack.Count-1);
  213. end;
  214. ifn:
  215. begin
  216. result:=p;
  217. DoSet(tifnode(p).right,succ);
  218. DoSet(tifnode(p).t1,succ);
  219. p.successor:=succ;
  220. end;
  221. labeln:
  222. begin
  223. result:=p;
  224. if assigned(tlabelnode(p).left) then
  225. begin
  226. DoSet(tlabelnode(p).left,succ);
  227. p.successor:=tlabelnode(p).left;
  228. end
  229. else
  230. p.successor:=succ;
  231. end;
  232. assignn:
  233. begin
  234. result:=p;
  235. p.successor:=succ;
  236. end;
  237. goton:
  238. begin
  239. result:=p;
  240. if not(assigned(tgotonode(p).labelnode)) then
  241. internalerror(2007050701);
  242. p.successor:=tgotonode(p).labelnode;
  243. end;
  244. exitn:
  245. begin
  246. result:=p;
  247. p.successor:=nil;
  248. end;
  249. casen:
  250. begin
  251. result:=p;
  252. DoSet(tcasenode(p).elseblock,succ);
  253. for i:=0 to tcasenode(p).blocks.count-1 do
  254. DoSet(pcaseblock(tcasenode(p).blocks[i])^.statement,succ);
  255. p.successor:=succ;
  256. end;
  257. calln:
  258. begin
  259. { not sure if this is enough (FK) }
  260. result:=p;
  261. if not(cnf_call_never_returns in tcallnode(p).callnodeflags) then
  262. p.successor:=succ;
  263. end;
  264. inlinen:
  265. begin
  266. { not sure if this is enough (FK) }
  267. result:=p;
  268. p.successor:=succ;
  269. end;
  270. loadn,
  271. tempcreaten,
  272. tempdeleten,
  273. nothingn:
  274. begin
  275. result:=p;
  276. p.successor:=succ;
  277. end;
  278. raisen:
  279. begin
  280. result:=p;
  281. { raise never returns }
  282. p.successor:=nil;
  283. end;
  284. withn,
  285. tryexceptn,
  286. tryfinallyn,
  287. onn:
  288. internalerror(2007050501);
  289. end;
  290. end;
  291. begin
  292. Breakstack:=TFPList.Create;
  293. Continuestack:=TFPList.Create;
  294. DoSet(p,last);
  295. Continuestack.Free;
  296. Breakstack.Free;
  297. end;
  298. var
  299. sum : TDFASet;
  300. function adddef(var n: tnode; arg: pointer): foreachnoderesult;
  301. begin
  302. if assigned(n.optinfo) then
  303. DFASetIncludeSet(sum,n.optinfo^.def);
  304. Result:=fen_false;
  305. end;
  306. procedure CalcDefSum(p : tnode);
  307. begin
  308. p.allocoptinfo;
  309. if not assigned(p.optinfo^.defsum) then
  310. begin
  311. sum:=nil;
  312. foreachnodestatic(pm_postprocess,p,@adddef,nil);
  313. p.optinfo^.defsum:=sum;
  314. end;
  315. end;
  316. function has_life_info(n : tnode) : boolean;
  317. begin
  318. result:=assigned(n) and assigned(n.optinfo) and
  319. assigned(n.optinfo^.life);
  320. end;
  321. end.