optdfa.pas 20 KB

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