optdfa.pas 20 KB

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