optdfa.pas 20 KB

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