optdfa.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. temprefn,
  92. loadn:
  93. begin
  94. pdfainfo(arg)^.map.Add(n);
  95. if nf_modify in n.flags then
  96. begin
  97. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  98. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  99. end
  100. else if nf_write in n.flags then
  101. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  102. else
  103. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  104. {
  105. write('Use Set: ');
  106. PrintDFASet(output,pdfainfo(arg)^.use^);
  107. write(' Def Set: ');
  108. PrintDFASet(output,pdfainfo(arg)^.def^);
  109. writeln;
  110. }
  111. end;
  112. end;
  113. result:=fen_false;
  114. end;
  115. function ResetProcessing(var n: tnode; arg: pointer): foreachnoderesult;
  116. begin
  117. exclude(n.flags,nf_processing);
  118. result:=fen_false;
  119. end;
  120. function ResetDFA(var n: tnode; arg: pointer): foreachnoderesult;
  121. begin
  122. if assigned(n.optinfo) then
  123. begin
  124. with n.optinfo^ do
  125. begin
  126. life:=nil;
  127. def:=nil;
  128. use:=nil;
  129. defsum:=nil;
  130. end;
  131. end;
  132. result:=fen_false;
  133. end;
  134. procedure TDFABuilder.CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  135. var
  136. changed : boolean;
  137. procedure CreateInfo(node : tnode);
  138. { update life entry of a node with l, set changed if this changes
  139. life info for the node
  140. }
  141. procedure updatelifeinfo(n : tnode;l : TDFASet);
  142. var
  143. b : boolean;
  144. begin
  145. b:=DFASetNotEqual(l,n.optinfo^.life);
  146. {
  147. if b then
  148. begin
  149. printnode(output,n);
  150. printdfaset(output,l);
  151. writeln;
  152. printdfaset(output,n.optinfo^.life);
  153. writeln;
  154. end;
  155. }
  156. {$ifdef DEBUG_DFA}
  157. if not(changed) and b then
  158. writeln('Another DFA pass caused by: ',nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,')');
  159. {$endif DEBUG_DFA}
  160. changed:=changed or b;
  161. n.optinfo^.life:=l;
  162. end;
  163. procedure calclife(n : tnode);
  164. var
  165. l : TDFASet;
  166. begin
  167. if assigned(n.successor) then
  168. begin
  169. {
  170. write('Successor Life: ');
  171. printdfaset(output,n.successor.optinfo^.life);
  172. writeln;
  173. write('Def.');
  174. printdfaset(output,n.optinfo^.def);
  175. writeln;
  176. }
  177. { ensure we can access optinfo }
  178. DFASetDiff(l,n.successor.optinfo^.life,n.optinfo^.def);
  179. {
  180. printdfaset(output,l);
  181. writeln;
  182. }
  183. DFASetIncludeSet(l,n.optinfo^.use);
  184. DFASetIncludeSet(l,n.optinfo^.life);
  185. end
  186. else
  187. begin
  188. { last node, not exit or raise node and function? }
  189. if assigned(resultnode) and
  190. not(node.nodetype=exitn) and
  191. not((node.nodetype=calln) and (cnf_call_never_returns in tcallnode(node).callnodeflags)) then
  192. begin
  193. { if yes, result lifes }
  194. DFASetDiff(l,resultnode.optinfo^.life,n.optinfo^.def);
  195. DFASetIncludeSet(l,n.optinfo^.use);
  196. DFASetIncludeSet(l,n.optinfo^.life);
  197. end
  198. else
  199. begin
  200. l:=n.optinfo^.use;
  201. DFASetIncludeSet(l,n.optinfo^.life);
  202. end;
  203. end;
  204. updatelifeinfo(n,l);
  205. end;
  206. var
  207. dfainfo : tdfainfo;
  208. l : TDFASet;
  209. save: TDFASet;
  210. i : longint;
  211. begin
  212. if node=nil then
  213. exit;
  214. { ensure we've already optinfo set }
  215. node.allocoptinfo;
  216. if nf_processing in node.flags then
  217. exit;
  218. include(node.flags,nf_processing);
  219. if not(assigned(node.successor)) and (node<>resultnode) then
  220. node.successor:=resultnode;
  221. if assigned(node.successor) then
  222. CreateInfo(node.successor);
  223. {$ifdef EXTDEBUG_DFA}
  224. writeln('Handling: ',nodetype2str[node.nodetype],'(',node.fileinfo.line,',',node.fileinfo.column,')');
  225. {$endif EXTDEBUG_DFA}
  226. { life:=succesorlive-definition+use }
  227. case node.nodetype of
  228. whilerepeatn:
  229. begin
  230. { analyze the loop condition }
  231. if not(assigned(node.optinfo^.def)) and
  232. not(assigned(node.optinfo^.use)) then
  233. begin
  234. dfainfo.use:[email protected]^.use;
  235. dfainfo.def:[email protected]^.def;
  236. dfainfo.map:=map;
  237. foreachnodestatic(pm_postprocess,twhilerepeatnode(node).left,@AddDefUse,@dfainfo);
  238. end;
  239. { NB: this node should typically have empty def set }
  240. if assigned(node.successor) then
  241. DFASetDiff(l,node.successor.optinfo^.life,node.optinfo^.def)
  242. else if assigned(resultnode) then
  243. DFASetDiff(l,resultnode.optinfo^.life,node.optinfo^.def)
  244. else
  245. l:=nil;
  246. { for repeat..until, node use set in included at the end of loop }
  247. if not (lnf_testatbegin in twhilerepeatnode(node).loopflags) then
  248. DFASetIncludeSet(l,node.optinfo^.use);
  249. DFASetIncludeSet(l,node.optinfo^.life);
  250. save:=node.optinfo^.life;
  251. { to process body correctly, we need life info in place (because
  252. whilerepeatnode is successor of its body). }
  253. node.optinfo^.life:=l;
  254. { now process the body }
  255. CreateInfo(twhilerepeatnode(node).right);
  256. { restore, to prevent infinite recursion via changed flag }
  257. node.optinfo^.life:=save;
  258. { for while loops, node use set is included at the beginning of loop }
  259. l:=twhilerepeatnode(node).right.optinfo^.life;
  260. if lnf_testatbegin in twhilerepeatnode(node).loopflags then
  261. DFASetIncludeSet(l,node.optinfo^.use);
  262. UpdateLifeInfo(node,l);
  263. { ... and a second iteration for fast convergence }
  264. CreateInfo(twhilerepeatnode(node).right);
  265. end;
  266. forn:
  267. begin
  268. {
  269. left: loopvar
  270. right: from
  271. t1: to
  272. t2: body
  273. }
  274. node.allocoptinfo;
  275. if not(assigned(node.optinfo^.def)) and
  276. not(assigned(node.optinfo^.use)) then
  277. begin
  278. dfainfo.use:[email protected]^.use;
  279. dfainfo.def:[email protected]^.def;
  280. dfainfo.map:=map;
  281. foreachnodestatic(pm_postprocess,tfornode(node).left,@AddDefUse,@dfainfo);
  282. foreachnodestatic(pm_postprocess,tfornode(node).right,@AddDefUse,@dfainfo);
  283. foreachnodestatic(pm_postprocess,tfornode(node).t1,@AddDefUse,@dfainfo);
  284. end;
  285. { create life for the body }
  286. CreateInfo(tfornode(node).t2);
  287. { update for node }
  288. { life:=life+body }
  289. l:=copy(node.optinfo^.life);
  290. DFASetIncludeSet(l,tfornode(node).t2.optinfo^.life);
  291. { take care of the sucessor as it's possible that we don't have one execution of the body }
  292. DFASetIncludeSet(l,node.successor.optinfo^.life);
  293. { the counter variable is living as well inside the for loop }
  294. DFASetInclude(l,tfornode(node).left.optinfo^.index);
  295. { force block node life info }
  296. UpdateLifeInfo(tfornode(node).t2,l);
  297. { the counter variable is not living at the entry of the for node }
  298. DFASetExclude(l,tfornode(node).left.optinfo^.index);
  299. { ... but it could be that left/right use it, so do it after
  300. removing def }
  301. DFASetIncludeSet(l,node.optinfo^.use);
  302. UpdateLifeInfo(node,l);
  303. { ... and a second iteration for fast convergence }
  304. CreateInfo(tfornode(node).t2);
  305. end;
  306. temprefn,
  307. loadn,
  308. typeconvn,
  309. assignn:
  310. begin
  311. if not(assigned(node.optinfo^.def)) and
  312. not(assigned(node.optinfo^.use)) then
  313. begin
  314. dfainfo.use:[email protected]^.use;
  315. dfainfo.def:[email protected]^.def;
  316. dfainfo.map:=map;
  317. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  318. end;
  319. calclife(node);
  320. end;
  321. statementn:
  322. begin
  323. { nested statement }
  324. CreateInfo(tstatementnode(node).statement);
  325. { inherit info }
  326. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  327. end;
  328. blockn:
  329. begin
  330. CreateInfo(tblocknode(node).statements);
  331. if assigned(tblocknode(node).statements) then
  332. node.optinfo^.life:=tblocknode(node).statements.optinfo^.life;
  333. end;
  334. ifn:
  335. begin
  336. { get information from cond. expression }
  337. if not(assigned(node.optinfo^.def)) and
  338. not(assigned(node.optinfo^.use)) then
  339. begin
  340. dfainfo.use:[email protected]^.use;
  341. dfainfo.def:[email protected]^.def;
  342. dfainfo.map:=map;
  343. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  344. end;
  345. { create life info for then and else node }
  346. CreateInfo(tifnode(node).right);
  347. CreateInfo(tifnode(node).t1);
  348. { ensure that we don't remove life info }
  349. l:=node.optinfo^.life;
  350. { get life info from then branch }
  351. if assigned(tifnode(node).right) then
  352. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  353. { get life info from else branch }
  354. if assigned(tifnode(node).t1) then
  355. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  356. else
  357. if assigned(node.successor) then
  358. DFASetIncludeSet(l,node.successor.optinfo^.life)
  359. { last node and function? }
  360. else
  361. if assigned(resultnode) then
  362. DFASetIncludeSet(l,resultnode.optinfo^.life);
  363. { add use info from the cond. expression }
  364. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  365. { finally, update the life info of the node }
  366. UpdateLifeInfo(node,l);
  367. end;
  368. casen:
  369. begin
  370. { get information from "case" expression }
  371. if not(assigned(node.optinfo^.def)) and
  372. not(assigned(node.optinfo^.use)) then
  373. begin
  374. dfainfo.use:[email protected]^.use;
  375. dfainfo.def:[email protected]^.def;
  376. dfainfo.map:=map;
  377. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  378. end;
  379. { create life info for block and else nodes }
  380. for i:=0 to tcasenode(node).blocks.count-1 do
  381. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  382. CreateInfo(tcasenode(node).elseblock);
  383. { ensure that we don't remove life info }
  384. l:=node.optinfo^.life;
  385. { get life info from case branches }
  386. for i:=0 to tcasenode(node).blocks.count-1 do
  387. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  388. { get life info from else branch or the succesor }
  389. if assigned(tcasenode(node).elseblock) then
  390. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  391. else
  392. if assigned(node.successor) then
  393. DFASetIncludeSet(l,node.successor.optinfo^.life)
  394. { last node and function? }
  395. else
  396. if assigned(resultnode) then
  397. DFASetIncludeSet(l,resultnode.optinfo^.life);
  398. { add use info from the "case" expression }
  399. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  400. { finally, update the life info of the node }
  401. UpdateLifeInfo(node,l);
  402. end;
  403. exitn:
  404. begin
  405. if not(is_void(current_procinfo.procdef.returndef)) then
  406. begin
  407. if not(assigned(node.optinfo^.def)) and
  408. not(assigned(node.optinfo^.use)) then
  409. begin
  410. if assigned(texitnode(node).left) then
  411. begin
  412. node.optinfo^.def:=resultnode.optinfo^.def;
  413. dfainfo.use:[email protected]^.use;
  414. dfainfo.def:[email protected]^.def;
  415. dfainfo.map:=map;
  416. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  417. calclife(node);
  418. end
  419. else
  420. begin
  421. { get info from faked resultnode }
  422. node.optinfo^.use:=resultnode.optinfo^.use;
  423. node.optinfo^.life:=node.optinfo^.use;
  424. changed:=true;
  425. end;
  426. end;
  427. end;
  428. end;
  429. inlinen,
  430. calln:
  431. begin
  432. if not(assigned(node.optinfo^.def)) and
  433. not(assigned(node.optinfo^.use)) then
  434. begin
  435. dfainfo.use:[email protected]^.use;
  436. dfainfo.def:[email protected]^.def;
  437. dfainfo.map:=map;
  438. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  439. end;
  440. calclife(node);
  441. end;
  442. tempcreaten,
  443. tempdeleten,
  444. nothingn,
  445. continuen,
  446. goton,
  447. breakn,
  448. labeln:
  449. begin
  450. calclife(node);
  451. end;
  452. else
  453. if node<>resultnode then
  454. begin
  455. writeln(nodetype2str[node.nodetype]);
  456. internalerror(2007050502);
  457. end;
  458. end;
  459. // exclude(node.flags,nf_processing);
  460. end;
  461. var
  462. runs : integer;
  463. dfarec : tdfainfo;
  464. begin
  465. runs:=0;
  466. if not(is_void(current_procinfo.procdef.returndef)) then
  467. begin
  468. { create a fake node using the result }
  469. if current_procinfo.procdef.proctypeoption=potype_constructor then
  470. resultnode:=load_self_node
  471. else
  472. resultnode:=load_result_node;
  473. resultnode.allocoptinfo;
  474. dfarec.use:[email protected]^.use;
  475. dfarec.def:[email protected]^.def;
  476. dfarec.map:=map;
  477. AddDefUse(resultnode,@dfarec);
  478. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  479. end
  480. else
  481. resultnode:=cnothingnode.create;
  482. repeat
  483. inc(runs);
  484. changed:=false;
  485. CreateInfo(node);
  486. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  487. {$ifdef DEBUG_DFA}
  488. PrintIndexedNodeSet(output,map);
  489. PrintDFAInfo(output,node);
  490. {$endif DEBUG_DFA}
  491. until not(changed);
  492. {$ifdef DEBUG_DFA}
  493. writeln('DFA solver iterations: ',runs);
  494. {$endif DEBUG_DFA}
  495. end;
  496. { reset all dfa info, this is required before creating dfa info
  497. if the tree has been changed without updating dfa }
  498. procedure TDFABuilder.resetdfainfo(node : tnode);
  499. begin
  500. foreachnodestatic(pm_postprocess,node,@ResetDFA,nil);
  501. end;
  502. procedure TDFABuilder.createdfainfo(node : tnode);
  503. begin
  504. if not(assigned(nodemap)) then
  505. nodemap:=TIndexedNodeSet.Create;
  506. { add controll flow information }
  507. SetNodeSucessors(node,resultnode);
  508. { now, collect life information }
  509. CreateLifeInfo(node,nodemap);
  510. end;
  511. destructor TDFABuilder.Destroy;
  512. begin
  513. Resultnode.free;
  514. nodemap.free;
  515. inherited destroy;
  516. end;
  517. end.