aopt.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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; reintroduce;
  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. TAsmOptimizerClass = class of TAsmOptimizer;
  39. var
  40. casmoptimizer : TAsmOptimizerClass;
  41. cpreregallocscheduler : TAsmOptimizerClass;
  42. procedure Optimize(AsmL:TAsmList);
  43. procedure PreRegallocSchedule(AsmL:TAsmList);
  44. Implementation
  45. uses
  46. cutils,
  47. globtype, globals,
  48. verbose,
  49. cpubase,
  50. cgbase,
  51. aoptda,aoptcpu,aoptcpud;
  52. Constructor TAsmOptimizer.create(_AsmL: TAsmList);
  53. Begin
  54. inherited create(_asml,nil,nil,nil);
  55. {setup labeltable, always necessary}
  56. New(LabelInfo);
  57. End;
  58. procedure TAsmOptimizer.FindLoHiLabels;
  59. { Walks through the paasmlist to find the lowest and highest label number. }
  60. { Returns the last Pai object of the current block }
  61. Var LabelFound: Boolean;
  62. p: tai;
  63. Begin
  64. LabelInfo^.LowLabel := High(longint);
  65. LabelInfo^.HighLabel := 0;
  66. LabelInfo^.LabelDif := 0;
  67. LabelInfo^.LabelTable:=nil;
  68. LabelFound := False;
  69. P := BlockStart;
  70. With LabelInfo^ Do
  71. Begin
  72. While Assigned(P) And
  73. ((P.typ <> Ait_Marker) Or
  74. (tai_Marker(P).Kind <> mark_AsmBlockStart)) Do
  75. Begin
  76. If (p.typ = ait_label) and
  77. (tai_Label(p).labsym.labeltype=alt_jump) and
  78. (tai_Label(p).labsym.is_used) Then
  79. Begin
  80. LabelFound := True;
  81. If (tai_Label(p).labsym.labelnr < LowLabel) Then
  82. LowLabel := tai_Label(p).labsym.labelnr;
  83. If (tai_Label(p).labsym.labelnr > HighLabel) Then
  84. HighLabel := tai_Label(p).labsym.labelnr
  85. End;
  86. GetNextInstruction(p, p)
  87. End;
  88. blockend:=p;
  89. If LabelFound
  90. Then LabelDif := HighLabel-LowLabel+1
  91. Else LabelDif := 0
  92. End
  93. End;
  94. Procedure TAsmOptimizer.BuildLabelTableAndFixRegAlloc;
  95. { Builds a table with the locations of the labels in the TAsmList. }
  96. { Also fixes some RegDeallocs like "# %eax released; push (%eax)" }
  97. Var p,hp1, hp2: tai;
  98. Regs: TAllUsedRegs;
  99. LabelIdx : longint;
  100. Begin
  101. CreateUsedRegs(Regs);
  102. With LabelInfo^ Do
  103. If (LabelDif <> 0) Then
  104. Begin
  105. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  106. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  107. p := BlockStart;
  108. While (P <> BlockEnd) Do
  109. Begin
  110. Case p.typ Of
  111. ait_Label:
  112. begin
  113. If tai_label(p).labsym.is_used and
  114. (tai_Label(p).labsym.labeltype=alt_jump) then
  115. begin
  116. LabelIdx:=tai_label(p).labsym.labelnr-LowLabel;
  117. if LabelIdx>int64(LabelDif) then
  118. internalerror(200604202);
  119. LabelTable^[LabelIdx].PaiObj := p;
  120. end;
  121. end;
  122. ait_regAlloc:
  123. begin
  124. if tai_regalloc(p).ratype=ra_alloc then
  125. Begin
  126. If Not(RegInUsedRegs(tai_regalloc(p).Reg,Regs)) Then
  127. IncludeRegInUsedRegs(tai_regalloc(p).Reg,Regs)
  128. Else
  129. Begin
  130. hp1 := tai(p.previous);
  131. {$ifdef DEBUG_OPTALLOC}
  132. AsmL.InsertAfter(tai_comment.Create(strpnew('Removed allocation of '+std_regname(tai_regalloc(p).Reg))),p);
  133. {$endif DEBUG_OPTALLOC}
  134. AsmL.remove(p);
  135. p.free;
  136. p := hp1;
  137. { not sure if this is useful, it even skips previous deallocs of the register (FK)
  138. hp1 := p;
  139. hp2 := nil;
  140. While GetLastInstruction(hp1, hp1) And
  141. Not(RegInInstruction(tai_regalloc(p).Reg, hp1)) Do
  142. hp2:=hp1;
  143. If hp2<>nil Then
  144. Begin
  145. hp1:=tai_regalloc.DeAlloc(tai_regalloc(p).Reg,hp2);
  146. InsertLLItem(tai(hp2.previous), hp2, hp1);
  147. End;
  148. }
  149. End;
  150. End
  151. else if tai_regalloc(p).ratype=ra_dealloc then
  152. Begin
  153. ExcludeRegFromUsedRegs(tai_regalloc(p).Reg,Regs);
  154. hp1 := p;
  155. hp2 := nil;
  156. While Not(FindRegAlloc(tai_regalloc(p).Reg, tai(hp1.Next))) And
  157. GetNextInstruction(hp1, hp1) And
  158. RegInInstruction(tai_regalloc(p).Reg, hp1) Do
  159. hp2 := hp1;
  160. If hp2 <> nil Then
  161. Begin
  162. hp1 := tai(p.previous);
  163. {$ifdef DEBUG_OPTALLOC}
  164. AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
  165. {$endif DEBUG_OPTALLOC}
  166. AsmL.Remove(p);
  167. InsertLLItem(hp2, tai(hp2.Next), p);
  168. {$ifdef DEBUG_OPTALLOC}
  169. AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg)+' here')),hp2);
  170. {$endif DEBUG_OPTALLOC}
  171. p := hp1;
  172. End
  173. else if findregalloc(tai_regalloc(p).reg, tai(p.next))
  174. and getnextinstruction(p,hp1) then
  175. begin
  176. hp1 := tai(p.previous);
  177. {$ifdef DEBUG_OPTALLOC}
  178. AsmL.InsertAfter(tai_comment.Create(strpnew('Removed deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
  179. {$endif DEBUG_OPTALLOC}
  180. AsmL.remove(p);
  181. p.free;
  182. p := hp1;
  183. // don't include here, since then the allocation will be removed when it's processed
  184. // include(usedregs,supreg);
  185. end;
  186. End
  187. End
  188. End;
  189. P := tai(p.Next);
  190. While Assigned(p) and
  191. (p <> blockend) and
  192. (p.typ in (SkipInstr - [ait_regalloc])) Do
  193. P := tai(P.Next)
  194. End;
  195. End;
  196. ReleaseUsedRegs(Regs);
  197. End;
  198. procedure tasmoptimizer.clear;
  199. begin
  200. if assigned(LabelInfo^.labeltable) then
  201. begin
  202. freemem(LabelInfo^.labeltable);
  203. LabelInfo^.labeltable := nil;
  204. end;
  205. LabelInfo^.labeldif:=0;
  206. LabelInfo^.lowlabel:=high(longint);
  207. LabelInfo^.highlabel:=0;
  208. end;
  209. procedure tasmoptimizer.pass_1;
  210. begin
  211. findlohilabels;
  212. BuildLabelTableAndFixRegAlloc;
  213. end;
  214. Procedure TAsmOptimizer.Optimize;
  215. Var
  216. HP: tai;
  217. pass: longint;
  218. Begin
  219. pass:=0;
  220. BlockStart := tai(AsmL.First);
  221. pass_1;
  222. While Assigned(BlockStart) Do
  223. Begin
  224. if pass = 0 then
  225. PrePeepHoleOpts;
  226. { Peephole optimizations }
  227. PeepHoleOptPass1;
  228. { Only perform them twice in the first pass }
  229. if pass = 0 then
  230. PeepHoleOptPass1;
  231. If (cs_opt_asmcse in current_settings.optimizerswitches) Then
  232. Begin
  233. // DFA:=TAOptDFACpu.Create(AsmL,BlockStart,BlockEnd,LabelInfo);
  234. { data flow analyzer }
  235. // DFA.DoDFA;
  236. { common subexpression elimination }
  237. { CSE;}
  238. End;
  239. { more peephole optimizations }
  240. PeepHoleOptPass2;
  241. { if pass = last_pass then }
  242. PostPeepHoleOpts;
  243. { free memory }
  244. clear;
  245. { continue where we left off, BlockEnd is either the start of an }
  246. { assembler block or nil}
  247. BlockStart := BlockEnd;
  248. While Assigned(BlockStart) And
  249. (BlockStart.typ = ait_Marker) And
  250. (tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  251. Begin
  252. { we stopped at an assembler block, so skip it }
  253. While GetNextInstruction(BlockStart, BlockStart) And
  254. ((BlockStart.Typ <> Ait_Marker) Or
  255. (tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
  256. { blockstart now contains a tai_marker(mark_AsmBlockEnd) }
  257. If GetNextInstruction(BlockStart, HP) And
  258. ((HP.typ <> ait_Marker) Or
  259. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  260. { There is no assembler block anymore after the current one, so }
  261. { optimize the next block of "normal" instructions }
  262. pass_1
  263. { Otherwise, skip the next assembler block }
  264. else
  265. blockStart := hp;
  266. End
  267. End;
  268. End;
  269. Destructor TAsmOptimizer.Destroy;
  270. Begin
  271. Dispose(LabelInfo)
  272. End;
  273. procedure Optimize(AsmL:TAsmList);
  274. var
  275. p : TAsmOptimizer;
  276. begin
  277. p:=casmoptimizer.Create(AsmL);
  278. p.Optimize;
  279. p.free
  280. end;
  281. procedure PreRegallocSchedule(AsmL:TAsmList);
  282. var
  283. p : TAsmOptimizer;
  284. begin
  285. p:=cpreregallocscheduler.Create(AsmL);
  286. p.Optimize;
  287. p.free
  288. end;
  289. end.