optdfa.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. { expect a blocknode as body because we need to push the life information
  288. of the counter variable into it
  289. if tfornode(node).t2.nodetype<>blockn then
  290. begin
  291. printnode(tfornode(node).t2);
  292. internalerror(2013110301);
  293. end;}
  294. { first update the body }
  295. l:=copy(tfornode(node).t2.optinfo^.life);
  296. { take care of the sucessor as it's possible that we don't have one execution of the body }
  297. DFASetIncludeSet(l,node.successor.optinfo^.life);
  298. { the counter variable is living as well inside the for loop }
  299. DFASetInclude(l,tfornode(node).left.optinfo^.index);
  300. { force block node life info }
  301. UpdateLifeInfo(tfornode(node).t2,l);
  302. { avoid to modify the life information set above for t2 }
  303. l:=copy(l);
  304. { update l for the for node itself }
  305. DFASetIncludeSet(l,tfornode(node).t2.optinfo^.life);
  306. { the counter variable is not living at the entry of the for node }
  307. DFASetExclude(l,tfornode(node).left.optinfo^.index);
  308. { ... but it could be that left/right use it, so do it after
  309. removing def }
  310. DFASetIncludeSet(l,node.optinfo^.use);
  311. UpdateLifeInfo(node,l);
  312. { ... and a second iteration for fast convergence }
  313. CreateInfo(tfornode(node).t2);
  314. end;
  315. temprefn,
  316. loadn,
  317. typeconvn,
  318. assignn:
  319. begin
  320. if not(assigned(node.optinfo^.def)) and
  321. not(assigned(node.optinfo^.use)) then
  322. begin
  323. dfainfo.use:[email protected]^.use;
  324. dfainfo.def:[email protected]^.def;
  325. dfainfo.map:=map;
  326. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  327. end;
  328. calclife(node);
  329. end;
  330. statementn:
  331. begin
  332. { nested statement }
  333. CreateInfo(tstatementnode(node).statement);
  334. { inherit info }
  335. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  336. end;
  337. blockn:
  338. begin
  339. CreateInfo(tblocknode(node).statements);
  340. { ensure that we don't remove life info }
  341. l:=node.optinfo^.life;
  342. if assigned(node.successor) then
  343. DFASetIncludeSet(l,node.successor.optinfo^.life);
  344. UpdateLifeInfo(node,l);
  345. end;
  346. ifn:
  347. begin
  348. { get information from cond. expression }
  349. if not(assigned(node.optinfo^.def)) and
  350. not(assigned(node.optinfo^.use)) then
  351. begin
  352. dfainfo.use:[email protected]^.use;
  353. dfainfo.def:[email protected]^.def;
  354. dfainfo.map:=map;
  355. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  356. end;
  357. { create life info for then and else node }
  358. CreateInfo(tifnode(node).right);
  359. CreateInfo(tifnode(node).t1);
  360. { ensure that we don't remove life info }
  361. l:=node.optinfo^.life;
  362. { get life info from then branch }
  363. if assigned(tifnode(node).right) then
  364. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  365. { get life info from else branch }
  366. if assigned(tifnode(node).t1) then
  367. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  368. else
  369. if assigned(node.successor) then
  370. DFASetIncludeSet(l,node.successor.optinfo^.life)
  371. { last node and function? }
  372. else
  373. if assigned(resultnode) then
  374. DFASetIncludeSet(l,resultnode.optinfo^.life);
  375. { add use info from the cond. expression }
  376. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  377. { finally, update the life info of the node }
  378. UpdateLifeInfo(node,l);
  379. end;
  380. casen:
  381. begin
  382. { get information from "case" expression }
  383. if not(assigned(node.optinfo^.def)) and
  384. not(assigned(node.optinfo^.use)) then
  385. begin
  386. dfainfo.use:[email protected]^.use;
  387. dfainfo.def:[email protected]^.def;
  388. dfainfo.map:=map;
  389. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  390. end;
  391. { create life info for block and else nodes }
  392. for i:=0 to tcasenode(node).blocks.count-1 do
  393. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  394. CreateInfo(tcasenode(node).elseblock);
  395. { ensure that we don't remove life info }
  396. l:=node.optinfo^.life;
  397. { get life info from case branches }
  398. for i:=0 to tcasenode(node).blocks.count-1 do
  399. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  400. { get life info from else branch or the succesor }
  401. if assigned(tcasenode(node).elseblock) then
  402. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  403. else
  404. if assigned(node.successor) then
  405. DFASetIncludeSet(l,node.successor.optinfo^.life)
  406. { last node and function? }
  407. else
  408. if assigned(resultnode) then
  409. DFASetIncludeSet(l,resultnode.optinfo^.life);
  410. { add use info from the "case" expression }
  411. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  412. { finally, update the life info of the node }
  413. UpdateLifeInfo(node,l);
  414. end;
  415. exitn:
  416. begin
  417. if not(is_void(current_procinfo.procdef.returndef)) then
  418. begin
  419. if not(assigned(node.optinfo^.def)) and
  420. not(assigned(node.optinfo^.use)) then
  421. begin
  422. if assigned(texitnode(node).left) then
  423. begin
  424. node.optinfo^.def:=resultnode.optinfo^.def;
  425. dfainfo.use:[email protected]^.use;
  426. dfainfo.def:[email protected]^.def;
  427. dfainfo.map:=map;
  428. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  429. calclife(node);
  430. end
  431. else
  432. begin
  433. { get info from faked resultnode }
  434. node.optinfo^.use:=resultnode.optinfo^.use;
  435. node.optinfo^.life:=node.optinfo^.use;
  436. changed:=true;
  437. end;
  438. end;
  439. end;
  440. end;
  441. inlinen,
  442. calln:
  443. begin
  444. if not(assigned(node.optinfo^.def)) and
  445. not(assigned(node.optinfo^.use)) then
  446. begin
  447. dfainfo.use:[email protected]^.use;
  448. dfainfo.def:[email protected]^.def;
  449. dfainfo.map:=map;
  450. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  451. end;
  452. calclife(node);
  453. end;
  454. tempcreaten,
  455. tempdeleten,
  456. nothingn,
  457. continuen,
  458. goton,
  459. breakn,
  460. labeln:
  461. begin
  462. calclife(node);
  463. end;
  464. else
  465. if node<>resultnode then
  466. begin
  467. writeln(nodetype2str[node.nodetype]);
  468. internalerror(2007050502);
  469. end;
  470. end;
  471. // exclude(node.flags,nf_processing);
  472. end;
  473. var
  474. runs : integer;
  475. dfarec : tdfainfo;
  476. begin
  477. runs:=0;
  478. if not(is_void(current_procinfo.procdef.returndef)) then
  479. begin
  480. { create a fake node using the result }
  481. if current_procinfo.procdef.proctypeoption=potype_constructor then
  482. resultnode:=load_self_node
  483. else
  484. resultnode:=load_result_node;
  485. resultnode.allocoptinfo;
  486. dfarec.use:[email protected]^.use;
  487. dfarec.def:[email protected]^.def;
  488. dfarec.map:=map;
  489. AddDefUse(resultnode,@dfarec);
  490. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  491. end
  492. else
  493. resultnode:=cnothingnode.create;
  494. repeat
  495. inc(runs);
  496. changed:=false;
  497. CreateInfo(node);
  498. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  499. {$ifdef DEBUG_DFA}
  500. PrintIndexedNodeSet(output,map);
  501. PrintDFAInfo(output,node);
  502. {$endif DEBUG_DFA}
  503. until not(changed);
  504. {$ifdef DEBUG_DFA}
  505. writeln('DFA solver iterations: ',runs);
  506. {$endif DEBUG_DFA}
  507. end;
  508. { reset all dfa info, this is required before creating dfa info
  509. if the tree has been changed without updating dfa }
  510. procedure TDFABuilder.resetdfainfo(node : tnode);
  511. begin
  512. foreachnodestatic(pm_postprocess,node,@ResetDFA,nil);
  513. end;
  514. procedure TDFABuilder.createdfainfo(node : tnode);
  515. begin
  516. if not(assigned(nodemap)) then
  517. nodemap:=TIndexedNodeSet.Create;
  518. { add controll flow information }
  519. SetNodeSucessors(node,resultnode);
  520. { now, collect life information }
  521. CreateLifeInfo(node,nodemap);
  522. end;
  523. destructor TDFABuilder.Destroy;
  524. begin
  525. Resultnode.free;
  526. nodemap.free;
  527. inherited destroy;
  528. end;
  529. end.