aopt.pas 14 KB

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