optutils.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. Exitsuccessor: TNode;
  138. { sets the successor nodes of a node tree block
  139. returns the first node of the tree if it's a controll flow node }
  140. function DoSet(p : tnode;succ : tnode) : tnode;
  141. var
  142. hp1,hp2, oldexitsuccessor: tnode;
  143. i : longint;
  144. begin
  145. result:=nil;
  146. if p=nil then
  147. exit;
  148. case p.nodetype of
  149. statementn:
  150. begin
  151. hp1:=p;
  152. result:=p;
  153. while assigned(hp1) do
  154. begin
  155. { does another statement follow? }
  156. if assigned(tstatementnode(hp1).next) then
  157. begin
  158. hp2:=DoSet(tstatementnode(hp1).statement,tstatementnode(hp1).next);
  159. if assigned(hp2) then
  160. tstatementnode(hp1).successor:=hp2
  161. else
  162. tstatementnode(hp1).successor:=tstatementnode(hp1).next;
  163. end
  164. else
  165. begin
  166. hp2:=DoSet(tstatementnode(hp1).statement,succ);
  167. if assigned(hp2) then
  168. tstatementnode(hp1).successor:=hp2
  169. else
  170. tstatementnode(hp1).successor:=succ;
  171. end;
  172. hp1:=tstatementnode(hp1).next;
  173. end;
  174. end;
  175. blockn:
  176. begin
  177. result:=p;
  178. oldexitsuccessor:=Exitsuccessor;
  179. if nf_block_with_exit in p.flags then
  180. Exitsuccessor:=succ;
  181. DoSet(tblocknode(p).statements,succ);
  182. if assigned(tblocknode(p).statements) then
  183. p.successor:=tblocknode(p).statements
  184. else
  185. p.successor:=succ;
  186. Exitsuccessor:=oldexitsuccessor;
  187. end;
  188. forn:
  189. begin
  190. Breakstack.Add(succ);
  191. Continuestack.Add(p);
  192. result:=p;
  193. { the successor of the last node of the for body is the dummy loop iteration node
  194. it allows the dfa to inject needed life information into the loop }
  195. tfornode(p).loopiteration:=cnothingnode.create;
  196. DoSet(tfornode(p).t2,tfornode(p).loopiteration);
  197. p.successor:=succ;
  198. Breakstack.Delete(Breakstack.Count-1);
  199. Continuestack.Delete(Continuestack.Count-1);
  200. end;
  201. breakn:
  202. begin
  203. result:=p;
  204. p.successor:=tnode(Breakstack.Last);
  205. end;
  206. continuen:
  207. begin
  208. result:=p;
  209. p.successor:=tnode(Continuestack.Last);
  210. end;
  211. whilerepeatn:
  212. begin
  213. Breakstack.Add(succ);
  214. Continuestack.Add(p);
  215. result:=p;
  216. { the successor of the last node of the while/repeat body is the while node itself }
  217. DoSet(twhilerepeatnode(p).right,p);
  218. p.successor:=succ;
  219. { special case: we do not do a dyn. dfa, but we should handle endless loops }
  220. if is_constboolnode(twhilerepeatnode(p).left) then
  221. begin
  222. if ((lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  223. getbooleanvalue(twhilerepeatnode(p).left)) or (not(lnf_testatbegin in twhilerepeatnode(p).loopflags) and
  224. not(getbooleanvalue(twhilerepeatnode(p).left))) then
  225. p.successor:=nil;
  226. end;
  227. Breakstack.Delete(Breakstack.Count-1);
  228. Continuestack.Delete(Continuestack.Count-1);
  229. end;
  230. ifn:
  231. begin
  232. result:=p;
  233. DoSet(tifnode(p).right,succ);
  234. DoSet(tifnode(p).t1,succ);
  235. p.successor:=succ;
  236. end;
  237. labeln:
  238. begin
  239. result:=p;
  240. if assigned(tlabelnode(p).left) then
  241. begin
  242. DoSet(tlabelnode(p).left,succ);
  243. p.successor:=tlabelnode(p).left;
  244. end
  245. else
  246. p.successor:=succ;
  247. end;
  248. assignn:
  249. begin
  250. result:=p;
  251. p.successor:=succ;
  252. end;
  253. goton:
  254. begin
  255. result:=p;
  256. if not(assigned(tgotonode(p).labelnode)) then
  257. internalerror(2007050701);
  258. p.successor:=tgotonode(p).labelnode;
  259. end;
  260. exitn:
  261. begin
  262. result:=p;
  263. p.successor:=Exitsuccessor;
  264. end;
  265. casen:
  266. begin
  267. result:=p;
  268. DoSet(tcasenode(p).elseblock,succ);
  269. for i:=0 to tcasenode(p).blocks.count-1 do
  270. DoSet(pcaseblock(tcasenode(p).blocks[i])^.statement,succ);
  271. p.successor:=succ;
  272. end;
  273. calln:
  274. begin
  275. { not sure if this is enough (FK) }
  276. result:=p;
  277. if cnf_call_never_returns in tcallnode(p).callnodeflags then
  278. p.successor:=nil
  279. else
  280. p.successor:=succ;
  281. end;
  282. inlinen:
  283. begin
  284. { not sure if this is enough (FK) }
  285. result:=p;
  286. p.successor:=succ;
  287. end;
  288. loadn,
  289. tempcreaten,
  290. tempdeleten,
  291. nothingn:
  292. begin
  293. result:=p;
  294. p.successor:=succ;
  295. end;
  296. raisen:
  297. begin
  298. result:=p;
  299. { raise never returns }
  300. p.successor:=nil;
  301. end;
  302. tryexceptn,
  303. tryfinallyn,
  304. onn:
  305. internalerror(2007050501);
  306. else
  307. ;
  308. end;
  309. end;
  310. begin
  311. Breakstack:=TFPList.Create;
  312. Continuestack:=TFPList.Create;
  313. Exitsuccessor:=nil;
  314. DoSet(p,last);
  315. Continuestack.Free;
  316. Breakstack.Free;
  317. end;
  318. var
  319. defsum : TDFASet;
  320. function adddef(var n: tnode; arg: pointer): foreachnoderesult;
  321. begin
  322. if assigned(n.optinfo) then
  323. begin
  324. DFASetIncludeSet(defsum,n.optinfo^.def);
  325. { for nodes itself do not necessarily expose the definition of the counter as
  326. the counter might be undefined after the for loop, so include here the counter
  327. explicitly }
  328. if (n.nodetype=forn) and assigned(tfornode(n).left.optinfo) then
  329. DFASetInclude(defsum,tfornode(n).left.optinfo^.index);
  330. end;
  331. Result:=fen_false;
  332. end;
  333. procedure CalcDefSum(p : tnode);
  334. begin
  335. p.allocoptinfo;
  336. if not assigned(p.optinfo^.defsum) then
  337. begin
  338. defsum:=nil;
  339. foreachnodestatic(pm_postprocess,p,@adddef,nil);
  340. p.optinfo^.defsum:=defsum;
  341. end;
  342. end;
  343. var
  344. usesum : TDFASet;
  345. function SetExecutionWeight(var n: tnode; arg: pointer): foreachnoderesult;
  346. var
  347. Weight, CaseWeight : longint;
  348. i : Integer;
  349. begin
  350. Result:=fen_false;
  351. n.allocoptinfo;
  352. Weight:=max(plongint(arg)^,1);
  353. case n.nodetype of
  354. casen:
  355. begin
  356. CalcExecutionWeights(tcasenode(n).left,Weight);
  357. CaseWeight:=max(Weight div tcasenode(n).labelcnt,1);
  358. for i:=0 to tcasenode(n).blocks.count-1 do
  359. CalcExecutionWeights(pcaseblock(tcasenode(n).blocks[i])^.statement,CaseWeight);
  360. CalcExecutionWeights(tcasenode(n).elseblock,CaseWeight);
  361. Result:=fen_norecurse_false;
  362. end;
  363. whilerepeatn:
  364. begin
  365. CalcExecutionWeights(twhilerepeatnode(n).right,Weight*8);
  366. CalcExecutionWeights(twhilerepeatnode(n).left,Weight*8);
  367. Result:=fen_norecurse_false;
  368. end;
  369. ifn:
  370. begin
  371. CalcExecutionWeights(tifnode(n).left,Weight);
  372. CalcExecutionWeights(tifnode(n).right,Weight div 2);
  373. CalcExecutionWeights(tifnode(n).t1,Weight div 2);
  374. Result:=fen_norecurse_false;
  375. end;
  376. else
  377. ;
  378. end;
  379. n.optinfo^.executionweight:=Weight;
  380. end;
  381. procedure CalcExecutionWeights(p : tnode;Initial : longint = 100);
  382. begin
  383. if assigned(p) then
  384. foreachnodestatic(pm_postprocess,p,@SetExecutionWeight,Pointer(@Initial));
  385. end;
  386. function adduse(var n: tnode; arg: pointer): foreachnoderesult;
  387. begin
  388. if assigned(n.optinfo) then
  389. DFASetIncludeSet(usesum,n.optinfo^.use);
  390. Result:=fen_false;
  391. end;
  392. procedure CalcUseSum(p : tnode);
  393. begin
  394. p.allocoptinfo;
  395. if not assigned(p.optinfo^.usesum) then
  396. begin
  397. usesum:=nil;
  398. foreachnodestatic(pm_postprocess,p,@adduse,nil);
  399. p.optinfo^.usesum:=usesum;
  400. end;
  401. end;
  402. function has_life_info(n : tnode) : boolean;
  403. begin
  404. result:=assigned(n) and assigned(n.optinfo) and
  405. assigned(n.optinfo^.life);
  406. end;
  407. end.