aopt.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit contains the interface routines between the code generator
  5. and the optimizer.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. Unit aopt;
  20. {$i fpcdefs.inc}
  21. Interface
  22. Uses
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. aoptobj;
  25. Type
  26. TAsmOptimizer = class(TAoptObj)
  27. { _AsmL is the PAasmOutpout list that has to be optimized }
  28. Constructor create(_AsmL: TAsmList); virtual;
  29. { call the necessary optimizer procedures }
  30. Procedure Optimize;
  31. Destructor destroy;override;
  32. private
  33. procedure FindLoHiLabels;
  34. Procedure BuildLabelTableAndFixRegAlloc;
  35. procedure clear;
  36. procedure pass_1;
  37. End;
  38. var
  39. casmoptimizer : class of tasmoptimizer;
  40. procedure Optimize(AsmL:TAsmList);
  41. Implementation
  42. uses
  43. globtype, globals,
  44. verbose,
  45. aoptda,aoptcpu,aoptcpud;
  46. Constructor TAsmOptimizer.create(_AsmL: TAsmList);
  47. Begin
  48. inherited create(_asml,nil,nil,nil);
  49. {setup labeltable, always necessary}
  50. New(LabelInfo);
  51. End;
  52. procedure TAsmOptimizer.FindLoHiLabels;
  53. { Walks through the paasmlist to find the lowest and highest label number. }
  54. { Returns the last Pai object of the current block }
  55. Var LabelFound: Boolean;
  56. p, prev: tai;
  57. Begin
  58. LabelInfo^.LowLabel := High(AWord);
  59. LabelInfo^.HighLabel := 0;
  60. LabelInfo^.LabelDif := 0;
  61. LabelInfo^.LabelTable:=nil;
  62. LabelFound := False;
  63. P := BlockStart;
  64. prev := p;
  65. With LabelInfo^ Do
  66. Begin
  67. While Assigned(P) And
  68. ((P.typ <> Ait_Marker) Or
  69. (tai_Marker(P).Kind <> mark_AsmBlockStart)) Do
  70. Begin
  71. If (p.typ = ait_label) and
  72. (tai_Label(p).labsym.labeltype=alt_jump) and
  73. (tai_Label(p).labsym.is_used) Then
  74. Begin
  75. LabelFound := True;
  76. If (tai_Label(p).labsym.labelnr < LowLabel) Then
  77. LowLabel := tai_Label(p).labsym.labelnr;
  78. If (tai_Label(p).labsym.labelnr > HighLabel) Then
  79. HighLabel := tai_Label(p).labsym.labelnr
  80. End;
  81. prev := p;
  82. GetNextInstruction(p, p)
  83. End;
  84. blockend:=p;
  85. If LabelFound
  86. Then LabelDif := HighLabel-LowLabel+1
  87. Else LabelDif := 0
  88. End
  89. End;
  90. Procedure TAsmOptimizer.BuildLabelTableAndFixRegAlloc;
  91. { Builds a table with the locations of the labels in the TAsmList. }
  92. { Also fixes some RegDeallocs like "# %eax released; push (%eax)" }
  93. Var p, hp1, hp2: tai;
  94. UsedRegs: TRegSet;
  95. LabelIdx : longint;
  96. Begin
  97. UsedRegs := [];
  98. With LabelInfo^ Do
  99. If (LabelDif <> 0) Then
  100. Begin
  101. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  102. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  103. p := BlockStart;
  104. While (P <> BlockEnd) Do
  105. Begin
  106. Case p.typ Of
  107. ait_Label:
  108. begin
  109. If tai_label(p).labsym.is_used and
  110. (tai_Label(p).labsym.labeltype=alt_jump) then
  111. begin
  112. LabelIdx:=tai_label(p).labsym.labelnr-LowLabel;
  113. if LabelIdx>LabelDif then
  114. internalerror(200604202);
  115. LabelTable^[LabelIdx].PaiObj := p;
  116. end;
  117. end;
  118. ait_regAlloc:
  119. begin
  120. {!!!!!!!!!
  121. if tai_regalloc(p).ratype=ra_alloc then
  122. Begin
  123. If Not(tai_regalloc(p).Reg in UsedRegs) Then
  124. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  125. Else
  126. Begin
  127. hp1 := p;
  128. hp2 := nil;
  129. While GetLastInstruction(hp1, hp1) And
  130. Not(RegInInstruction(tai_regalloc(p).Reg, hp1)) Do
  131. hp2:=hp1;
  132. If hp2<>nil Then
  133. Begin
  134. hp1:=tai_regalloc.DeAlloc(tai_regalloc(p).Reg,hp2);
  135. InsertLLItem(tai(hp2.previous), hp2, hp1);
  136. End;
  137. End;
  138. End
  139. else
  140. Begin
  141. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  142. hp1 := p;
  143. hp2 := nil;
  144. While Not(FindRegAlloc(tai_regalloc(p).Reg, tai(hp1.Next))) And
  145. GetNextInstruction(hp1, hp1) And
  146. RegInInstruction(tai_regalloc(p).Reg, hp1) Do
  147. hp2 := hp1;
  148. If hp2 <> nil Then
  149. Begin
  150. hp1 := tai(p.previous);
  151. AsmL.Remove(p);
  152. InsertLLItem(hp2, tai(hp2.Next), p);
  153. p := hp1;
  154. End
  155. End
  156. };
  157. End
  158. End;
  159. P := tai(p.Next);
  160. While Assigned(p) and
  161. (p <> blockend) and
  162. (p.typ in (SkipInstr - [ait_regalloc])) Do
  163. P := tai(P.Next)
  164. End;
  165. End
  166. End;
  167. procedure tasmoptimizer.clear;
  168. begin
  169. if assigned(LabelInfo^.labeltable) then
  170. begin
  171. freemem(LabelInfo^.labeltable);
  172. LabelInfo^.labeltable := nil;
  173. end;
  174. LabelInfo^.labeldif:=0;
  175. LabelInfo^.lowlabel:=high(AWord);
  176. LabelInfo^.highlabel:=0;
  177. end;
  178. procedure tasmoptimizer.pass_1;
  179. begin
  180. findlohilabels;
  181. BuildLabelTableAndFixRegAlloc;
  182. end;
  183. Procedure TAsmOptimizer.Optimize;
  184. Var
  185. HP: tai;
  186. pass: longint;
  187. Begin
  188. pass:=0;
  189. BlockStart := tai(AsmL.First);
  190. pass_1;
  191. While Assigned(BlockStart) Do
  192. Begin
  193. if pass = 0 then
  194. PrePeepHoleOpts;
  195. { Peephole optimizations }
  196. PeepHoleOptPass1;
  197. { Only perform them twice in the first pass }
  198. if pass = 0 then
  199. PeepHoleOptPass1;
  200. If (cs_opt_asmcse in current_settings.optimizerswitches) Then
  201. Begin
  202. // DFA:=TAOptDFACpu.Create(AsmL,BlockStart,BlockEnd,LabelInfo);
  203. { data flow analyzer }
  204. // DFA.DoDFA;
  205. { common subexpression elimination }
  206. { CSE;}
  207. End;
  208. { more peephole optimizations }
  209. PeepHoleOptPass2;
  210. { if pass = last_pass then }
  211. PostPeepHoleOpts;
  212. { free memory }
  213. clear;
  214. { continue where we left off, BlockEnd is either the start of an }
  215. { assembler block or nil}
  216. BlockStart := BlockEnd;
  217. While Assigned(BlockStart) And
  218. (BlockStart.typ = ait_Marker) And
  219. (tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  220. Begin
  221. { we stopped at an assembler block, so skip it }
  222. While GetNextInstruction(BlockStart, BlockStart) And
  223. ((BlockStart.Typ <> Ait_Marker) Or
  224. (tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
  225. { blockstart now contains a tai_marker(mark_AsmBlockEnd) }
  226. If GetNextInstruction(BlockStart, HP) And
  227. ((HP.typ <> ait_Marker) Or
  228. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  229. { There is no assembler block anymore after the current one, so }
  230. { optimize the next block of "normal" instructions }
  231. pass_1
  232. { Otherwise, skip the next assembler block }
  233. else
  234. blockStart := hp;
  235. End
  236. End;
  237. End;
  238. Destructor TAsmOptimizer.Destroy;
  239. Begin
  240. Dispose(LabelInfo)
  241. End;
  242. procedure Optimize(AsmL:TAsmList);
  243. var
  244. p : TAsmOptimizer;
  245. begin
  246. p:=casmoptimizer.Create(AsmL);
  247. p.Optimize;
  248. p.free
  249. end;
  250. end.