optcse.pas 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. {
  2. Common subexpression elimination on base blocks
  3. Copyright (c) 2005 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. unit optcse;
  18. {$i fpcdefs.inc}
  19. interface
  20. procedure docse(rootnode : tnode);
  21. implementation
  22. procedure docse(rootnode : tnode);
  23. begin
  24. { create a linear list of nodes }
  25. { create hash values }
  26. { sort by hash values, taking care of nf_csebarrier and keeping the
  27. original order of the nodes }
  28. { compare nodes with equal hash values }
  29. { search barrier }
  30. for i:=0 to nodelist.length-1 do
  31. begin
  32. { and then search backward so we get always the largest equal trees }
  33. j:=i+1;
  34. { collect equal nodes }
  35. while (j<=nodelist.length-1) and
  36. nodelist[i].docompare(nodelist[j]) do
  37. inc(j);
  38. dec(j);
  39. if j>i then
  40. begin
  41. { cse found }
  42. { create temp. location }
  43. { replace first node by
  44. - temp. creation
  45. - expression calculation
  46. - assignment of expression to temp. }
  47. tempnode:=ctempcreatenode.create(nodelist[i].resultdef,nodelist[i].resultdef.size,tt_persistent,
  48. nodelist[i].resultdef.is_intregable or nodelist[i].resultdef.is_fpuregable);
  49. addstatement(createstatement,tempnode);
  50. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  51. caddrnode.create_internal(para.left)));
  52. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  53. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  54. { replace next nodes by loading the temp. reference }
  55. { replace last node by loading the temp. reference and
  56. delete the temp. }
  57. end;
  58. end;
  59. end;
  60. end.