optutils.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. globtype;
  24. type
  25. { this implementation should be really improved,
  26. its purpose is to find equal nodes }
  27. TIndexedNodeSet = class(TFPList)
  28. function Add(node : tnode) : boolean;
  29. function Includes(node : tnode) : tnode;
  30. function Remove(node : tnode) : boolean;
  31. end;
  32. procedure SetNodeSucessors(p,last : tnode);
  33. procedure PrintDFAInfo(var f : text;p : tnode);
  34. procedure PrintIndexedNodeSet(var f : text;s : TIndexedNodeSet);
  35. { determines the optinfo.defsum field for the given node
  36. this field contains a sum of all expressions defined by
  37. all child expressions reachable through p
  38. }
  39. procedure CalcDefSum(p : tnode);
  40. { calculates/estimates the field execution weight of optinfo }
  41. procedure CalcExecutionWeights(p : tnode;Initial : longint = 100);
  42. { determines the optinfo.defsum field for the given node
  43. this field contains a sum of all expressions defined by
  44. all child expressions reachable through p
  45. }
  46. procedure CalcUseSum(p : tnode);
  47. { returns true, if n is a valid node and has life info }
  48. function has_life_info(n : tnode) : boolean;
  49. implementation
  50. uses
  51. cutils,
  52. verbose,
  53. optbase,
  54. ncal,nbas,nflw,nutils,nset,ncon;
  55. function TIndexedNodeSet.Add(node : tnode) : boolean;
  56. var
  57. i : Integer;
  58. p : tnode;
  59. begin
  60. node.allocoptinfo;
  61. p:=Includes(node);
  62. if assigned(p) then
  63. begin
  64. result:=false;
  65. node.optinfo^.index:=p.optinfo^.index;
  66. end
  67. else
  68. begin
  69. i:=inherited Add(node);
  70. node.optinfo^.index:=i;
  71. result:=true;
  72. end
  73. end;
  74. function TIndexedNodeSet.Includes(node : tnode) : tnode;
  75. var
  76. i : longint;
  77. begin
  78. for i:=0 to Count-1 do
  79. if tnode(List^[i]).isequal(node) then
  80. begin
  81. result:=tnode(List^[i]);
  82. exit;
  83. end;
  84. result:=nil;
  85. end;
  86. function TIndexedNodeSet.Remove(node : tnode) : boolean;
  87. var
  88. p : tnode;
  89. begin
  90. result:=false;
  91. p:=Includes(node);
  92. if assigned(p) then
  93. begin
  94. if inherited Remove(p)<>-1 then
  95. result:=true;
  96. end;
  97. end;
  98. procedure PrintIndexedNodeSet(var f : text;s : TIndexedNodeSet);
  99. var
  100. i : integer;
  101. begin
  102. for i:=0 to s.count-1 do
  103. begin
  104. writeln(f,'=============================== Node ',i,' ===============================');
  105. printnode(f,tnode(s[i]));
  106. writeln(f);
  107. end;
  108. end;
  109. function PrintNodeDFA(var n: tnode; arg: pointer): foreachnoderesult;
  110. begin
  111. if assigned(n.optinfo) and ((n.optinfo^.life<>nil) or (n.optinfo^.use<>nil) or (n.optinfo^.def<>nil)) then
  112. begin
  113. write(text(arg^),nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,') Life: ');
  114. PrintDFASet(text(arg^),n.optinfo^.life);
  115. write(text(arg^),' Def: ');
  116. PrintDFASet(text(arg^),n.optinfo^.def);
  117. write(text(arg^),' Use: ');
  118. PrintDFASet(text(arg^),n.optinfo^.use);
  119. if assigned(n.successor) then
  120. write(text(arg^),' Successor: ',nodetype2str[n.successor.nodetype],'(',n.successor.fileinfo.line,',',n.successor.fileinfo.column,')')
  121. else
  122. write(text(arg^),' Successor: nil');
  123. write(text(arg^),' DefSum: ');
  124. PrintDFASet(text(arg^),n.optinfo^.defsum);
  125. writeln(text(arg^));
  126. end;
  127. result:=fen_false;
  128. end;
  129. procedure PrintDFAInfo(var f : text;p : tnode);
  130. begin
  131. foreachnodestatic(pm_postprocess,p,@PrintNodeDFA,@f);
  132. end;
  133. procedure SetNodeSucessors(p,last : tnode);
  134. var
  135. Continuestack : TFPList;
  136. Breakstack : TFPList;
  137. { sets the successor nodes of a node tree block
  138. returns the first node of the tree if it's a controll flow node }
  139. function DoSet(p : tnode;succ : tnode) : tnode;
  140. var
  141. hp1,hp2 : tnode;
  142. i : longint;
  143. begin
  144. result:=nil;
  145. if p=nil then
  146. exit;
  147. case p.nodetype of
  148. statementn:
  149. begin
  150. hp1:=p;
  151. result:=p;
  152. while assigned(hp1) do
  153. begin
  154. { does another statement follow? }
  155. if assigned(tstatementnode(hp1).next) then
  156. begin
  157. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  158. if assigned(hp2) then
  159. tstatementnode(hp1).successor:=hp2
  160. else
  161. tstatementnode(hp1).successor:=tstatementnode(hp1).next;
  162. end
  163. else
  164. begin
  165. hp2:=DoSet(tstatementnode(hp1).statement,succ);
  166. if assigned(hp2) then
  167. tstatementnode(hp1).successor:=hp2
  168. else
  169. tstatementnode(hp1).successor:=succ;
  170. end;
  171. hp1:=tstatementnode(hp1).next;
  172. end;
  173. end;
  174. blockn:
  175. begin
  176. result:=p;
  177. DoSet(tblocknode(p).statements,succ);
  178. if assigned(tblocknode(p).statements) then
  179. p.successor:=tblocknode(p).statements
  180. else
  181. p.successor:=succ;
  182. end;
  183. forn:
  184. begin
  185. Breakstack.Add(succ);
  186. Continuestack.Add(p);
  187. result:=p;
  188. { the successor of the last node of the for body is the dummy loop iteration node
  189. it allows the dfa to inject needed life information into the loop }
  190. tfornode(p).loopiteration:=cnothingnode.create;
  191. DoSet(tfornode(p).t2,tfornode(p).loopiteration);
  192. p.successor:=succ;
  193. Breakstack.Delete(Breakstack.Count-1);
  194. Continuestack.Delete(Continuestack.Count-1);
  195. end;
  196. breakn:
  197. begin
  198. result:=p;
  199. p.successor:=tnode(Breakstack.Last);
  200. end;
  201. continuen:
  202. begin
  203. result:=p;
  204. p.successor:=tnode(Continuestack.Last);
  205. end;
  206. whilerepeatn:
  207. begin
  208. Breakstack.Add(succ);
  209. Continuestack.Add(p);
  210. result:=p;
  211. { the successor of the last node of the while/repeat body is the while node itself }
  212. DoSet(twhilerepeatnode(p).right,p);
  213. p.successor:=succ;
  214. { special case: we do not do a dyn. dfa, but we should handle endless loops }
  215. if is_constboolnode(twhilerepeatnode(p).left) then
  216. begin
  217. if ((lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  218. getbooleanvalue(twhilerepeatnode(p).left)) or (not(lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  219. not(getbooleanvalue(twhilerepeatnode(p).left))) then
  220. p.successor:=nil;
  221. end;
  222. Breakstack.Delete(Breakstack.Count-1);
  223. Continuestack.Delete(Continuestack.Count-1);
  224. end;
  225. ifn:
  226. begin
  227. result:=p;
  228. DoSet(tifnode(p).right,succ);
  229. DoSet(tifnode(p).t1,succ);
  230. p.successor:=succ;
  231. end;
  232. labeln:
  233. begin
  234. result:=p;
  235. if assigned(tlabelnode(p).left) then
  236. begin
  237. DoSet(tlabelnode(p).left,succ);
  238. p.successor:=tlabelnode(p).left;
  239. end
  240. else
  241. p.successor:=succ;
  242. end;
  243. assignn:
  244. begin
  245. result:=p;
  246. p.successor:=succ;
  247. end;
  248. goton:
  249. begin
  250. result:=p;
  251. if not(assigned(tgotonode(p).labelnode)) then
  252. internalerror(2007050701);
  253. p.successor:=tgotonode(p).labelnode;
  254. end;
  255. exitn:
  256. begin
  257. result:=p;
  258. p.successor:=nil;
  259. end;
  260. casen:
  261. begin
  262. result:=p;
  263. DoSet(tcasenode(p).elseblock,succ);
  264. for i:=0 to tcasenode(p).blocks.count-1 do
  265. DoSet(pcaseblock(tcasenode(p).blocks[i])^.statement,succ);
  266. p.successor:=succ;
  267. end;
  268. calln:
  269. begin
  270. { not sure if this is enough (FK) }
  271. result:=p;
  272. if not(cnf_call_never_returns in tcallnode(p).callnodeflags) then
  273. p.successor:=succ;
  274. end;
  275. inlinen:
  276. begin
  277. { not sure if this is enough (FK) }
  278. result:=p;
  279. p.successor:=succ;
  280. end;
  281. loadn,
  282. tempcreaten,
  283. tempdeleten,
  284. nothingn:
  285. begin
  286. result:=p;
  287. p.successor:=succ;
  288. end;
  289. raisen:
  290. begin
  291. result:=p;
  292. { raise never returns }
  293. p.successor:=nil;
  294. end;
  295. tryexceptn,
  296. tryfinallyn,
  297. onn:
  298. internalerror(2007050501);
  299. else
  300. ;
  301. end;
  302. end;
  303. begin
  304. Breakstack:=TFPList.Create;
  305. Continuestack:=TFPList.Create;
  306. DoSet(p,last);
  307. Continuestack.Free;
  308. Breakstack.Free;
  309. end;
  310. var
  311. defsum : TDFASet;
  312. function adddef(var n: tnode; arg: pointer): foreachnoderesult;
  313. begin
  314. if assigned(n.optinfo) then
  315. begin
  316. DFASetIncludeSet(defsum,n.optinfo^.def);
  317. { for nodes itself do not necessarily expose the definition of the counter as
  318. the counter might be undefined after the for loop, so include here the counter
  319. explicitly }
  320. if (n.nodetype=forn) and assigned(tfornode(n).left.optinfo) then
  321. DFASetInclude(defsum,tfornode(n).left.optinfo^.index);
  322. end;
  323. Result:=fen_false;
  324. end;
  325. procedure CalcDefSum(p : tnode);
  326. begin
  327. p.allocoptinfo;
  328. if not assigned(p.optinfo^.defsum) then
  329. begin
  330. defsum:=nil;
  331. foreachnodestatic(pm_postprocess,p,@adddef,nil);
  332. p.optinfo^.defsum:=defsum;
  333. end;
  334. end;
  335. var
  336. usesum : TDFASet;
  337. function SetExecutionWeight(var n: tnode; arg: pointer): foreachnoderesult;
  338. var
  339. Weight, CaseWeight : longint;
  340. i : Integer;
  341. begin
  342. Result:=fen_false;
  343. n.allocoptinfo;
  344. Weight:=max(plongint(arg)^,1);
  345. case n.nodetype of
  346. casen:
  347. begin
  348. CalcExecutionWeights(tcasenode(n).left,Weight);
  349. CaseWeight:=max(Weight div tcasenode(n).labelcnt,1);
  350. for i:=0 to tcasenode(n).blocks.count-1 do
  351. CalcExecutionWeights(pcaseblock(tcasenode(n).blocks[i])^.statement,CaseWeight);
  352. CalcExecutionWeights(tcasenode(n).elseblock,CaseWeight);
  353. Result:=fen_norecurse_false;
  354. end;
  355. whilerepeatn:
  356. begin
  357. CalcExecutionWeights(twhilerepeatnode(n).right,Weight*8);
  358. CalcExecutionWeights(twhilerepeatnode(n).left,Weight*8);
  359. Result:=fen_norecurse_false;
  360. end;
  361. ifn:
  362. begin
  363. CalcExecutionWeights(tifnode(n).left,Weight);
  364. CalcExecutionWeights(tifnode(n).right,Weight div 2);
  365. CalcExecutionWeights(tifnode(n).t1,Weight div 2);
  366. Result:=fen_norecurse_false;
  367. end;
  368. else
  369. ;
  370. end;
  371. n.optinfo^.executionweight:=Weight;
  372. end;
  373. procedure CalcExecutionWeights(p : tnode;Initial : longint = 100);
  374. begin
  375. if assigned(p) then
  376. foreachnodestatic(pm_postprocess,p,@SetExecutionWeight,Pointer(@Initial));
  377. end;
  378. function adduse(var n: tnode; arg: pointer): foreachnoderesult;
  379. begin
  380. if assigned(n.optinfo) then
  381. DFASetIncludeSet(usesum,n.optinfo^.use);
  382. Result:=fen_false;
  383. end;
  384. procedure CalcUseSum(p : tnode);
  385. begin
  386. p.allocoptinfo;
  387. if not assigned(p.optinfo^.usesum) then
  388. begin
  389. usesum:=nil;
  390. foreachnodestatic(pm_postprocess,p,@adduse,nil);
  391. p.optinfo^.usesum:=usesum;
  392. end;
  393. end;
  394. function has_life_info(n : tnode) : boolean;
  395. begin
  396. result:=assigned(n) and assigned(n.optinfo) and
  397. assigned(n.optinfo^.life);
  398. end;
  399. end.