optdfa.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. {
  2. DFA
  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. { $define DEBUG_DFA}
  18. { $define EXTDEBUG_DFA}
  19. { this unit implements routines to perform dfa }
  20. unit optdfa;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. node,optutils;
  25. type
  26. TDFABuilder = class
  27. protected
  28. procedure CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  29. public
  30. resultnode : tnode;
  31. nodemap : TIndexedNodeSet;
  32. { reset all dfa info, this is required before creating dfa info
  33. if the tree has been changed without updating dfa }
  34. procedure resetdfainfo(node : tnode);
  35. procedure createdfainfo(node : tnode);
  36. destructor destroy;override;
  37. end;
  38. implementation
  39. uses
  40. globtype,globals,
  41. verbose,
  42. cpuinfo,
  43. symconst,symdef,
  44. defutil,
  45. procinfo,
  46. nutils,
  47. nbas,nflw,ncon,ninl,ncal,nset,
  48. optbase;
  49. (*
  50. function initnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  51. begin
  52. { node worth to add? }
  53. if (node_complexity(n)>1) and (tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable) then
  54. begin
  55. plists(arg)^.nodelist.Add(n);
  56. plists(arg)^.locationlist.Add(@n);
  57. result:=fen_false;
  58. end
  59. else
  60. result:=fen_norecurse_false;
  61. end;
  62. *)
  63. {
  64. x:=f; read: [f]
  65. while x do read: []
  66. a:=b; read: [a,b,d] def: [a] life: read*def=[a]
  67. c:=d; read: [a,d] def: [a,c] life: read*def=[a]
  68. e:=a; read: [a] def: [a,c,e] life: read*def=[a]
  69. function f(b,d,x : type) : type;
  70. begin
  71. while x do alive: b,d,x
  72. begin
  73. a:=b; alive: b,d,x
  74. c:=d; alive: a,d,x
  75. e:=a+c; alive: a,c,x
  76. dec(x); alive: c,e,x
  77. end;
  78. result:=c+e; alive: c,e
  79. end; alive: result
  80. }
  81. type
  82. tdfainfo = record
  83. use : PDFASet;
  84. def : PDFASet;
  85. map : TIndexedNodeSet
  86. end;
  87. pdfainfo = ^tdfainfo;
  88. function AddDefUse(var n: tnode; arg: pointer): foreachnoderesult;
  89. begin
  90. case n.nodetype of
  91. loadn:
  92. begin
  93. pdfainfo(arg)^.map.Add(n);
  94. if nf_modify in n.flags then
  95. begin
  96. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  97. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  98. end
  99. else if nf_write in n.flags then
  100. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  101. else
  102. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  103. {
  104. write('Use Set: ');
  105. PrintDFASet(output,pdfainfo(arg)^.use^);
  106. write(' Def Set: ');
  107. PrintDFASet(output,pdfainfo(arg)^.def^);
  108. writeln;
  109. }
  110. end;
  111. end;
  112. result:=fen_false;
  113. end;
  114. function ResetProcessing(var n: tnode; arg: pointer): foreachnoderesult;
  115. begin
  116. exclude(n.flags,nf_processing);
  117. result:=fen_false;
  118. end;
  119. procedure TDFABuilder.CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  120. var
  121. changed : boolean;
  122. procedure CreateInfo(node : tnode);
  123. { update life entry of a node with l, set changed if this changes
  124. life info for the node
  125. }
  126. procedure updatelifeinfo(n : tnode;l : TDFASet);
  127. var
  128. b : boolean;
  129. begin
  130. b:=DFASetNotEqual(l,n.optinfo^.life);
  131. {
  132. if b then
  133. begin
  134. printnode(output,n);
  135. printdfaset(output,l);
  136. writeln;
  137. printdfaset(output,n.optinfo^.life);
  138. writeln;
  139. end;
  140. }
  141. {$ifdef DEBUG_DFA}
  142. if not(changed) and b then
  143. writeln('Another DFA pass caused by: ',nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,')');
  144. {$endif DEBUG_DFA}
  145. changed:=changed or b;
  146. node.optinfo^.life:=l;
  147. end;
  148. procedure calclife(n : tnode);
  149. var
  150. l : TDFASet;
  151. begin
  152. if assigned(n.successor) then
  153. begin
  154. {
  155. write('Successor Life: ');
  156. printdfaset(output,n.successor.optinfo^.life);
  157. writeln;
  158. write('Def.');
  159. printdfaset(output,n.optinfo^.def);
  160. writeln;
  161. }
  162. { ensure we can access optinfo }
  163. DFASetDiff(l,n.successor.optinfo^.life,n.optinfo^.def);
  164. {
  165. printdfaset(output,l);
  166. writeln;
  167. }
  168. DFASetIncludeSet(l,n.optinfo^.use);
  169. DFASetIncludeSet(l,n.optinfo^.life);
  170. end
  171. else
  172. begin
  173. { last node, not exit or raise node and function? }
  174. if assigned(resultnode) and
  175. not(node.nodetype in [raisen,exitn]) then
  176. begin
  177. { if yes, result lifes }
  178. DFASetDiff(l,resultnode.optinfo^.life,n.optinfo^.def);
  179. DFASetIncludeSet(l,n.optinfo^.use);
  180. DFASetIncludeSet(l,n.optinfo^.life);
  181. end
  182. else
  183. begin
  184. l:=n.optinfo^.use;
  185. DFASetIncludeSet(l,n.optinfo^.life);
  186. end;
  187. end;
  188. updatelifeinfo(n,l);
  189. end;
  190. var
  191. dfainfo : tdfainfo;
  192. l : TDFASet;
  193. i : longint;
  194. begin
  195. if node=nil then
  196. exit;
  197. { ensure we've already optinfo set }
  198. node.allocoptinfo;
  199. if nf_processing in node.flags then
  200. exit;
  201. include(node.flags,nf_processing);
  202. if assigned(node.successor) then
  203. CreateInfo(node.successor);
  204. {$ifdef EXTDEBUG_DFA}
  205. writeln('Handling: ',nodetype2str[node.nodetype],'(',node.fileinfo.line,',',node.fileinfo.column,')');
  206. {$endif EXTDEBUG_DFA}
  207. { life:=succesorlive-definition+use }
  208. case node.nodetype of
  209. whilerepeatn:
  210. begin
  211. calclife(node);
  212. { take care of repeat until! }
  213. if lnf_testatbegin in twhilerepeatnode(node).loopflags then
  214. begin
  215. node.allocoptinfo;
  216. if not(assigned(node.optinfo^.def)) and
  217. not(assigned(node.optinfo^.use)) then
  218. begin
  219. dfainfo.use:[email protected]^.use;
  220. dfainfo.def:[email protected]^.def;
  221. dfainfo.map:=map;
  222. foreachnodestatic(pm_postprocess,twhilerepeatnode(node).left,@AddDefUse,@dfainfo);
  223. end;
  224. calclife(node);
  225. { now iterate through the loop }
  226. CreateInfo(twhilerepeatnode(node).right);
  227. { update while node }
  228. { life:=life+use+right.life }
  229. l:=node.optinfo^.life;
  230. DFASetIncludeSet(l,node.optinfo^.use);
  231. DFASetIncludeSet(l,twhilerepeatnode(node).right.optinfo^.life);
  232. UpdateLifeInfo(node,l);
  233. { ... and a second iteration for fast convergence }
  234. CreateInfo(twhilerepeatnode(node).right);
  235. end;
  236. end;
  237. forn:
  238. begin
  239. {
  240. left: loopvar
  241. right: from
  242. t1: to
  243. t2: body
  244. }
  245. { take care of the sucessor if it's possible that we don't have one execution of the body }
  246. if not((tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn)) then
  247. calclife(node);
  248. node.allocoptinfo;
  249. if not(assigned(node.optinfo^.def)) and
  250. not(assigned(node.optinfo^.use)) then
  251. begin
  252. dfainfo.use:[email protected]^.use;
  253. dfainfo.def:[email protected]^.def;
  254. dfainfo.map:=map;
  255. foreachnodestatic(pm_postprocess,tfornode(node).left,@AddDefUse,@dfainfo);
  256. foreachnodestatic(pm_postprocess,tfornode(node).right,@AddDefUse,@dfainfo);
  257. foreachnodestatic(pm_postprocess,tfornode(node).t1,@AddDefUse,@dfainfo);
  258. end;
  259. { take care of the sucessor if it's possible that we don't have one execution of the body }
  260. if not((tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn)) then
  261. calclife(node);
  262. { create life for the body }
  263. CreateInfo(tfornode(node).t2);
  264. { update for node }
  265. { life:=life+use+body }
  266. l:=node.optinfo^.life;
  267. DFASetIncludeSet(l,node.optinfo^.use);
  268. DFASetIncludeSet(l,tfornode(node).t2.optinfo^.life);
  269. UpdateLifeInfo(node,l);
  270. { ... and a second iteration for fast convergence }
  271. CreateInfo(tfornode(node).t2);
  272. end;
  273. temprefn,
  274. loadn,
  275. typeconvn,
  276. assignn:
  277. begin
  278. if not(assigned(node.optinfo^.def)) and
  279. not(assigned(node.optinfo^.use)) then
  280. begin
  281. dfainfo.use:[email protected]^.use;
  282. dfainfo.def:[email protected]^.def;
  283. dfainfo.map:=map;
  284. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  285. end;
  286. calclife(node);
  287. end;
  288. statementn:
  289. begin
  290. { nested statement }
  291. CreateInfo(tstatementnode(node).statement);
  292. { inherit info }
  293. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  294. end;
  295. blockn:
  296. begin
  297. CreateInfo(tblocknode(node).statements);
  298. if assigned(tblocknode(node).statements) then
  299. node.optinfo^.life:=tblocknode(node).statements.optinfo^.life;
  300. end;
  301. ifn:
  302. begin
  303. { get information from cond. expression }
  304. if not(assigned(node.optinfo^.def)) and
  305. not(assigned(node.optinfo^.use)) then
  306. begin
  307. dfainfo.use:[email protected]^.use;
  308. dfainfo.def:[email protected]^.def;
  309. dfainfo.map:=map;
  310. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  311. end;
  312. { create life info for then and else node }
  313. CreateInfo(tifnode(node).right);
  314. CreateInfo(tifnode(node).t1);
  315. { ensure that we don't remove life info }
  316. l:=node.optinfo^.life;
  317. { get life info from then branch }
  318. if assigned(tifnode(node).right) then
  319. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  320. { get life info from else branch }
  321. if assigned(tifnode(node).t1) then
  322. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  323. else
  324. if assigned(node.successor) then
  325. DFASetIncludeSet(l,node.successor.optinfo^.life)
  326. { last node and function? }
  327. else
  328. if assigned(resultnode) then
  329. DFASetIncludeSet(l,resultnode.optinfo^.life);
  330. { add use info from the cond. expression }
  331. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  332. { finally, update the life info of the node }
  333. UpdateLifeInfo(node,l);
  334. end;
  335. casen:
  336. begin
  337. { get information from "case" expression }
  338. if not(assigned(node.optinfo^.def)) and
  339. not(assigned(node.optinfo^.use)) then
  340. begin
  341. dfainfo.use:[email protected]^.use;
  342. dfainfo.def:[email protected]^.def;
  343. dfainfo.map:=map;
  344. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  345. end;
  346. { create life info for block and else nodes }
  347. for i:=0 to tcasenode(node).blocks.count-1 do
  348. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  349. CreateInfo(tcasenode(node).elseblock);
  350. { ensure that we don't remove life info }
  351. l:=node.optinfo^.life;
  352. { get life info from case branches }
  353. for i:=0 to tcasenode(node).blocks.count-1 do
  354. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  355. { get life info from else branch or the succesor }
  356. if assigned(tcasenode(node).elseblock) then
  357. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  358. else
  359. if assigned(node.successor) then
  360. DFASetIncludeSet(l,node.successor.optinfo^.life)
  361. { last node and function? }
  362. else
  363. if assigned(resultnode) then
  364. DFASetIncludeSet(l,resultnode.optinfo^.life);
  365. { add use info from the "case" expression }
  366. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  367. { finally, update the life info of the node }
  368. UpdateLifeInfo(node,l);
  369. end;
  370. exitn:
  371. begin
  372. if not(is_void(current_procinfo.procdef.returndef)) and
  373. not(current_procinfo.procdef.proctypeoption=potype_constructor) then
  374. begin
  375. if not(assigned(node.optinfo^.def)) and
  376. not(assigned(node.optinfo^.use)) then
  377. begin
  378. if assigned(texitnode(node).left) then
  379. begin
  380. node.optinfo^.def:=resultnode.optinfo^.def;
  381. dfainfo.use:[email protected]^.use;
  382. dfainfo.def:[email protected]^.def;
  383. dfainfo.map:=map;
  384. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  385. calclife(node);
  386. end
  387. else
  388. begin
  389. { get info from faked resultnode }
  390. node.optinfo^.use:=resultnode.optinfo^.use;
  391. node.optinfo^.life:=node.optinfo^.use;
  392. changed:=true;
  393. end;
  394. end;
  395. end;
  396. end;
  397. raisen:
  398. begin
  399. if not(assigned(node.optinfo^.life)) then
  400. begin
  401. dfainfo.use:[email protected]^.use;
  402. dfainfo.def:[email protected]^.def;
  403. dfainfo.map:=map;
  404. foreachnodestatic(pm_postprocess,traisenode(node).left,@AddDefUse,@dfainfo);
  405. foreachnodestatic(pm_postprocess,traisenode(node).right,@AddDefUse,@dfainfo);
  406. foreachnodestatic(pm_postprocess,traisenode(node).third,@AddDefUse,@dfainfo);
  407. { update node }
  408. l:=node.optinfo^.life;
  409. DFASetIncludeSet(l,node.optinfo^.use);
  410. UpdateLifeInfo(node,l);
  411. printdfainfo(output,node);
  412. end;
  413. end;
  414. calln:
  415. begin
  416. if not(assigned(node.optinfo^.def)) and
  417. not(assigned(node.optinfo^.use)) then
  418. begin
  419. dfainfo.use:[email protected]^.use;
  420. dfainfo.def:[email protected]^.def;
  421. dfainfo.map:=map;
  422. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  423. end;
  424. calclife(node);
  425. end;
  426. tempcreaten,
  427. tempdeleten,
  428. inlinen,
  429. nothingn,
  430. continuen,
  431. goton,
  432. breakn,
  433. labeln:
  434. begin
  435. calclife(node);
  436. end;
  437. else
  438. begin
  439. writeln(nodetype2str[node.nodetype]);
  440. internalerror(2007050502);
  441. end;
  442. end;
  443. // exclude(node.flags,nf_processing);
  444. end;
  445. var
  446. runs : integer;
  447. dfarec : tdfainfo;
  448. begin
  449. runs:=0;
  450. if not(is_void(current_procinfo.procdef.returndef)) and
  451. not(current_procinfo.procdef.proctypeoption=potype_constructor) then
  452. begin
  453. { create a fake node using the result }
  454. resultnode:=load_result_node;
  455. resultnode.allocoptinfo;
  456. dfarec.use:[email protected]^.use;
  457. dfarec.def:[email protected]^.def;
  458. dfarec.map:=map;
  459. AddDefUse(resultnode,@dfarec);
  460. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  461. end
  462. else
  463. resultnode:=nil;
  464. repeat
  465. inc(runs);
  466. changed:=false;
  467. CreateInfo(node);
  468. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  469. {$ifdef DEBUG_DFA}
  470. PrintIndexedNodeSet(output,map);
  471. PrintDFAInfo(output,node);
  472. {$endif DEBUG_DFA}
  473. until not(changed);
  474. {$ifdef DEBUG_DFA}
  475. writeln('DFA solver iterations: ',runs);
  476. {$endif DEBUG_DFA}
  477. end;
  478. { reset all dfa info, this is required before creating dfa info
  479. if the tree has been changed without updating dfa }
  480. procedure TDFABuilder.resetdfainfo(node : tnode);
  481. begin
  482. end;
  483. procedure TDFABuilder.createdfainfo(node : tnode);
  484. begin
  485. if not(assigned(nodemap)) then
  486. nodemap:=TIndexedNodeSet.Create;
  487. { add controll flow information }
  488. SetNodeSucessors(node);
  489. { now, collect life information }
  490. CreateLifeInfo(node,nodemap);
  491. end;
  492. destructor TDFABuilder.Destroy;
  493. begin
  494. Resultnode.free;
  495. nodemap.free;
  496. inherited destroy;
  497. end;
  498. end.