optdfa.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. end;
  132. procedure TDFABuilder.CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  133. var
  134. changed : boolean;
  135. procedure CreateInfo(node : tnode);
  136. { update life entry of a node with l, set changed if this changes
  137. life info for the node
  138. }
  139. procedure updatelifeinfo(n : tnode;l : TDFASet);
  140. var
  141. b : boolean;
  142. begin
  143. b:=DFASetNotEqual(l,n.optinfo^.life);
  144. {
  145. if b then
  146. begin
  147. printnode(output,n);
  148. printdfaset(output,l);
  149. writeln;
  150. printdfaset(output,n.optinfo^.life);
  151. writeln;
  152. end;
  153. }
  154. {$ifdef DEBUG_DFA}
  155. if not(changed) and b then
  156. writeln('Another DFA pass caused by: ',nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,')');
  157. {$endif DEBUG_DFA}
  158. changed:=changed or b;
  159. node.optinfo^.life:=l;
  160. end;
  161. procedure calclife(n : tnode);
  162. var
  163. l : TDFASet;
  164. begin
  165. if assigned(n.successor) then
  166. begin
  167. {
  168. write('Successor Life: ');
  169. printdfaset(output,n.successor.optinfo^.life);
  170. writeln;
  171. write('Def.');
  172. printdfaset(output,n.optinfo^.def);
  173. writeln;
  174. }
  175. { ensure we can access optinfo }
  176. DFASetDiff(l,n.successor.optinfo^.life,n.optinfo^.def);
  177. {
  178. printdfaset(output,l);
  179. writeln;
  180. }
  181. DFASetIncludeSet(l,n.optinfo^.use);
  182. DFASetIncludeSet(l,n.optinfo^.life);
  183. end
  184. else
  185. begin
  186. { last node, not exit or raise node and function? }
  187. if assigned(resultnode) and
  188. not(node.nodetype in [raisen,exitn]) then
  189. begin
  190. { if yes, result lifes }
  191. DFASetDiff(l,resultnode.optinfo^.life,n.optinfo^.def);
  192. DFASetIncludeSet(l,n.optinfo^.use);
  193. DFASetIncludeSet(l,n.optinfo^.life);
  194. end
  195. else
  196. begin
  197. l:=n.optinfo^.use;
  198. DFASetIncludeSet(l,n.optinfo^.life);
  199. end;
  200. end;
  201. updatelifeinfo(n,l);
  202. end;
  203. var
  204. dfainfo : tdfainfo;
  205. l : TDFASet;
  206. i : longint;
  207. begin
  208. if node=nil then
  209. exit;
  210. { ensure we've already optinfo set }
  211. node.allocoptinfo;
  212. if nf_processing in node.flags then
  213. exit;
  214. include(node.flags,nf_processing);
  215. if assigned(node.successor) then
  216. CreateInfo(node.successor);
  217. {$ifdef EXTDEBUG_DFA}
  218. writeln('Handling: ',nodetype2str[node.nodetype],'(',node.fileinfo.line,',',node.fileinfo.column,')');
  219. {$endif EXTDEBUG_DFA}
  220. { life:=succesorlive-definition+use }
  221. case node.nodetype of
  222. whilerepeatn:
  223. begin
  224. calclife(node);
  225. { take care of repeat until! }
  226. if lnf_testatbegin in twhilerepeatnode(node).loopflags then
  227. begin
  228. node.allocoptinfo;
  229. if not(assigned(node.optinfo^.def)) and
  230. not(assigned(node.optinfo^.use)) then
  231. begin
  232. dfainfo.use:[email protected]^.use;
  233. dfainfo.def:[email protected]^.def;
  234. dfainfo.map:=map;
  235. foreachnodestatic(pm_postprocess,twhilerepeatnode(node).left,@AddDefUse,@dfainfo);
  236. end;
  237. calclife(node);
  238. { now iterate through the loop }
  239. CreateInfo(twhilerepeatnode(node).right);
  240. { update while node }
  241. { life:=life+use+right.life }
  242. l:=node.optinfo^.life;
  243. DFASetIncludeSet(l,node.optinfo^.use);
  244. DFASetIncludeSet(l,twhilerepeatnode(node).right.optinfo^.life);
  245. UpdateLifeInfo(node,l);
  246. { ... and a second iteration for fast convergence }
  247. CreateInfo(twhilerepeatnode(node).right);
  248. end;
  249. end;
  250. forn:
  251. begin
  252. {
  253. left: loopvar
  254. right: from
  255. t1: to
  256. t2: body
  257. }
  258. { take care of the sucessor if it's possible that we don't have one execution of the body }
  259. if not((tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn)) then
  260. calclife(node);
  261. node.allocoptinfo;
  262. if not(assigned(node.optinfo^.def)) and
  263. not(assigned(node.optinfo^.use)) then
  264. begin
  265. dfainfo.use:[email protected]^.use;
  266. dfainfo.def:[email protected]^.def;
  267. dfainfo.map:=map;
  268. foreachnodestatic(pm_postprocess,tfornode(node).left,@AddDefUse,@dfainfo);
  269. foreachnodestatic(pm_postprocess,tfornode(node).right,@AddDefUse,@dfainfo);
  270. foreachnodestatic(pm_postprocess,tfornode(node).t1,@AddDefUse,@dfainfo);
  271. end;
  272. { take care of the sucessor if it's possible that we don't have one execution of the body }
  273. if not((tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn)) then
  274. calclife(node);
  275. { create life for the body }
  276. CreateInfo(tfornode(node).t2);
  277. { update for node }
  278. { life:=life+use+body }
  279. l:=node.optinfo^.life;
  280. DFASetIncludeSet(l,node.optinfo^.use);
  281. DFASetIncludeSet(l,tfornode(node).t2.optinfo^.life);
  282. UpdateLifeInfo(node,l);
  283. { ... and a second iteration for fast convergence }
  284. CreateInfo(tfornode(node).t2);
  285. end;
  286. temprefn,
  287. loadn,
  288. typeconvn,
  289. assignn:
  290. begin
  291. if not(assigned(node.optinfo^.def)) and
  292. not(assigned(node.optinfo^.use)) then
  293. begin
  294. dfainfo.use:[email protected]^.use;
  295. dfainfo.def:[email protected]^.def;
  296. dfainfo.map:=map;
  297. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  298. end;
  299. calclife(node);
  300. end;
  301. statementn:
  302. begin
  303. { nested statement }
  304. CreateInfo(tstatementnode(node).statement);
  305. { inherit info }
  306. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  307. end;
  308. blockn:
  309. begin
  310. CreateInfo(tblocknode(node).statements);
  311. if assigned(tblocknode(node).statements) then
  312. node.optinfo^.life:=tblocknode(node).statements.optinfo^.life;
  313. end;
  314. ifn:
  315. begin
  316. { get information from cond. expression }
  317. if not(assigned(node.optinfo^.def)) and
  318. not(assigned(node.optinfo^.use)) then
  319. begin
  320. dfainfo.use:[email protected]^.use;
  321. dfainfo.def:[email protected]^.def;
  322. dfainfo.map:=map;
  323. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  324. end;
  325. { create life info for then and else node }
  326. CreateInfo(tifnode(node).right);
  327. CreateInfo(tifnode(node).t1);
  328. { ensure that we don't remove life info }
  329. l:=node.optinfo^.life;
  330. { get life info from then branch }
  331. if assigned(tifnode(node).right) then
  332. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  333. { get life info from else branch }
  334. if assigned(tifnode(node).t1) then
  335. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  336. else
  337. if assigned(node.successor) then
  338. DFASetIncludeSet(l,node.successor.optinfo^.life)
  339. { last node and function? }
  340. else
  341. if assigned(resultnode) then
  342. DFASetIncludeSet(l,resultnode.optinfo^.life);
  343. { add use info from the cond. expression }
  344. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  345. { finally, update the life info of the node }
  346. UpdateLifeInfo(node,l);
  347. end;
  348. casen:
  349. begin
  350. { get information from "case" expression }
  351. if not(assigned(node.optinfo^.def)) and
  352. not(assigned(node.optinfo^.use)) then
  353. begin
  354. dfainfo.use:[email protected]^.use;
  355. dfainfo.def:[email protected]^.def;
  356. dfainfo.map:=map;
  357. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  358. end;
  359. { create life info for block and else nodes }
  360. for i:=0 to tcasenode(node).blocks.count-1 do
  361. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  362. CreateInfo(tcasenode(node).elseblock);
  363. { ensure that we don't remove life info }
  364. l:=node.optinfo^.life;
  365. { get life info from case branches }
  366. for i:=0 to tcasenode(node).blocks.count-1 do
  367. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  368. { get life info from else branch or the succesor }
  369. if assigned(tcasenode(node).elseblock) then
  370. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  371. else
  372. if assigned(node.successor) then
  373. DFASetIncludeSet(l,node.successor.optinfo^.life)
  374. { last node and function? }
  375. else
  376. if assigned(resultnode) then
  377. DFASetIncludeSet(l,resultnode.optinfo^.life);
  378. { add use info from the "case" expression }
  379. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  380. { finally, update the life info of the node }
  381. UpdateLifeInfo(node,l);
  382. end;
  383. exitn:
  384. begin
  385. if not(is_void(current_procinfo.procdef.returndef)) and
  386. not(current_procinfo.procdef.proctypeoption=potype_constructor) then
  387. begin
  388. if not(assigned(node.optinfo^.def)) and
  389. not(assigned(node.optinfo^.use)) then
  390. begin
  391. if assigned(texitnode(node).left) then
  392. begin
  393. node.optinfo^.def:=resultnode.optinfo^.def;
  394. dfainfo.use:[email protected]^.use;
  395. dfainfo.def:[email protected]^.def;
  396. dfainfo.map:=map;
  397. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  398. calclife(node);
  399. end
  400. else
  401. begin
  402. { get info from faked resultnode }
  403. node.optinfo^.use:=resultnode.optinfo^.use;
  404. node.optinfo^.life:=node.optinfo^.use;
  405. changed:=true;
  406. end;
  407. end;
  408. end;
  409. end;
  410. raisen:
  411. begin
  412. if not(assigned(node.optinfo^.life)) then
  413. begin
  414. dfainfo.use:[email protected]^.use;
  415. dfainfo.def:[email protected]^.def;
  416. dfainfo.map:=map;
  417. foreachnodestatic(pm_postprocess,traisenode(node).left,@AddDefUse,@dfainfo);
  418. foreachnodestatic(pm_postprocess,traisenode(node).right,@AddDefUse,@dfainfo);
  419. foreachnodestatic(pm_postprocess,traisenode(node).third,@AddDefUse,@dfainfo);
  420. { update node }
  421. l:=node.optinfo^.life;
  422. DFASetIncludeSet(l,node.optinfo^.use);
  423. UpdateLifeInfo(node,l);
  424. printdfainfo(output,node);
  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.