optdfa.pas 21 KB

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