optbase.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. {
  2. Basic node optimizer stuff
  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. unit optbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype;
  22. type
  23. { this should maybe replaced by a spare set,
  24. using a dyn. array makes assignments cheap }
  25. tdfaset = array of byte;
  26. PDFASet = ^TDFASet;
  27. toptinfo = record
  28. { index of the current node inside the dfa sets, aword(-1) if no entry }
  29. index : aword;
  30. { dfa }
  31. def : tdfaset;
  32. use : tdfaset;
  33. life : tdfaset;
  34. { all definitions made by this node and its children }
  35. defsum : tdfaset;
  36. { all used nodes by this node and its children }
  37. usesum : tdfaset;
  38. avail : tdfaset;
  39. { estimation, how often the node is executed per subroutine call times 100, calculated by optutils.CalcExecutionWeight }
  40. executionweight : longint;
  41. end;
  42. poptinfo = ^toptinfo;
  43. { basic set operations for dfa sets }
  44. { add e to s }
  45. procedure DFASetInclude(var s : tdfaset;e : integer);
  46. { add s to d }
  47. procedure DFASetIncludeSet(var d : tdfaset;const s : tdfaset);
  48. { remove s to d }
  49. procedure DFASetExcludeSet(var d : tdfaset;const s : tdfaset);
  50. { remove e from s }
  51. procedure DFASetExclude(var s : tdfaset;e : integer);
  52. { test if s contains e }
  53. function DFASetIn(const s : tdfaset;e : integer) : boolean;
  54. { d:=s1+s2; }
  55. procedure DFASetUnion(var d : tdfaset;const s1,s2 : tdfaset);
  56. { d:=s1*s2; }
  57. procedure DFASetIntersect(var d : tdfaset;const s1,s2 : tdfaset);
  58. { d:=s1-s2; }
  59. procedure DFASetDiff(var d : tdfaset;const s1,s2 : tdfaset);
  60. { s1<>s2; }
  61. function DFASetNotEqual(const s1,s2 : tdfaset) : boolean;
  62. { output DFA set }
  63. procedure PrintDFASet(var f : text;s : TDFASet);
  64. implementation
  65. uses
  66. cutils;
  67. procedure DFASetInclude(var s : tdfaset;e : integer);
  68. var
  69. e8 : Integer;
  70. begin
  71. e8:=e div 8;
  72. if e8>high(s) then
  73. SetLength(s,e8+1);
  74. s[e8]:=s[e8] or (1 shl (e mod 8));
  75. end;
  76. procedure DFASetIncludeSet(var d : tdfaset;const s : tdfaset);
  77. var
  78. i : integer;
  79. begin
  80. if length(s)>length(d) then
  81. SetLength(d,length(s));
  82. for i:=0 to high(s) do
  83. d[i]:=d[i] or s[i];
  84. end;
  85. procedure DFASetExcludeSet(var d : tdfaset;const s : tdfaset);
  86. var
  87. i : integer;
  88. begin
  89. if length(s)>length(d) then
  90. SetLength(d,length(s));
  91. for i:=0 to high(s) do
  92. d[i]:=d[i] and not(s[i]);
  93. end;
  94. procedure DFASetExclude(var s : tdfaset;e : integer);
  95. var
  96. e8 : Integer;
  97. begin
  98. e8:=e div 8;
  99. if e8<=high(s) then
  100. s[e8]:=s[e8] and not(1 shl (e mod 8));
  101. end;
  102. function DFASetIn(const s : tdfaset;e : integer) : boolean;
  103. var
  104. e8 : Integer;
  105. begin
  106. e8:=e div 8;
  107. if e8<=high(s) then
  108. result:=(s[e8] and (1 shl (e mod 8)))<>0
  109. else
  110. result:=false;
  111. end;
  112. procedure DFASetUnion(var d : tdfaset;const s1,s2 : tdfaset);
  113. var
  114. i : integer;
  115. begin
  116. SetLength(d,max(Length(s1),Length(s2)));
  117. for i:=0 to min(high(s1),high(s2)) do
  118. d[i]:=s1[i] or s2[i];
  119. if high(s1)<high(s2) then
  120. for i:=high(s1)+1 to high(s2) do
  121. d[i]:=s2[i]
  122. else
  123. for i:=high(s2)+1 to high(s1) do
  124. d[i]:=s1[i];
  125. end;
  126. procedure DFASetIntersect(var d : tdfaset;const s1,s2 : tdfaset);
  127. var
  128. i : integer;
  129. begin
  130. SetLength(d,min(Length(s1),Length(s2)));
  131. for i:=0 to high(d) do
  132. d[i]:=s1[i] and s2[i];
  133. end;
  134. procedure DFASetDiff(var d : tdfaset;const s1,s2 : tdfaset);
  135. var
  136. i : integer;
  137. begin
  138. SetLength(d,length(s1));
  139. for i:=0 to high(d) do
  140. if i>high(s2) then
  141. d[i]:=s1[i]
  142. else
  143. d[i]:=s1[i] and not(s2[i]);
  144. end;
  145. function DFASetNotEqual(const s1,s2 : tdfaset) : boolean;
  146. var
  147. i : integer;
  148. begin
  149. result:=true;
  150. { one set could be larger than the other }
  151. if length(s1)>length(s2) then
  152. begin
  153. for i:=0 to high(s2) do
  154. if s1[i]<>s2[i] then
  155. exit;
  156. { check remaining part being zero }
  157. for i:=length(s2) to high(s1) do
  158. if s1[i]<>0 then
  159. exit;
  160. end
  161. else
  162. begin
  163. for i:=0 to high(s1) do
  164. if s1[i]<>s2[i] then
  165. exit;
  166. { check remaining part being zero }
  167. for i:=length(s1) to high(s2) do
  168. if s2[i]<>0 then
  169. exit;
  170. end;
  171. result:=false;
  172. end;
  173. procedure PrintDFASet(var f : text;s : TDFASet);
  174. var
  175. i : integer;
  176. first : boolean;
  177. begin
  178. first:=true;
  179. for i:=0 to Length(s)*8 do
  180. begin
  181. if DFASetIn(s,i) then
  182. begin
  183. if not(first) then
  184. write(f,',');
  185. write(f,i);
  186. first:=false;
  187. end;
  188. end;
  189. end;
  190. end.