aopt.pas 14 KB

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