aopt.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. Begin
  122. GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
  123. FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
  124. end;
  125. p := BlockStart;
  126. While (P <> BlockEnd) Do
  127. Begin
  128. prefetch(pointer(p.Next)^);
  129. Case p.typ Of
  130. ait_Label:
  131. begin
  132. If tai_label(p).labsym.is_used and
  133. (tai_Label(p).labsym.labeltype=alt_jump) then
  134. begin
  135. LabelIdx:=tai_label(p).labsym.labelnr-LowLabel;
  136. if LabelIdx>int64(LabelDif) then
  137. internalerror(200604202);
  138. LabelTable^[LabelIdx].PaiObj := p;
  139. end;
  140. end;
  141. ait_regAlloc:
  142. begin
  143. if tai_regalloc(p).ratype=ra_alloc then
  144. Begin
  145. If Not(RegInUsedRegs(tai_regalloc(p).Reg,Regs)) Then
  146. IncludeRegInUsedRegs(tai_regalloc(p).Reg,Regs)
  147. Else
  148. Begin
  149. hp1 := tai(p.previous);
  150. {$ifdef DEBUG_OPTALLOC}
  151. AsmL.InsertAfter(tai_comment.Create(strpnew('Removed allocation of '+std_regname(tai_regalloc(p).Reg))),p);
  152. {$endif DEBUG_OPTALLOC}
  153. AsmL.remove(p);
  154. p.free;
  155. p := hp1;
  156. { not sure if this is useful, it even skips previous deallocs of the register (FK)
  157. hp1 := p;
  158. hp2 := nil;
  159. While GetLastInstruction(hp1, hp1) And
  160. Not(RegInInstruction(tai_regalloc(p).Reg, hp1)) Do
  161. hp2:=hp1;
  162. If hp2<>nil Then
  163. Begin
  164. hp1:=tai_regalloc.DeAlloc(tai_regalloc(p).Reg,hp2);
  165. InsertLLItem(tai(hp2.previous), hp2, hp1);
  166. End;
  167. }
  168. End;
  169. End
  170. else if tai_regalloc(p).ratype=ra_dealloc then
  171. Begin
  172. hp1 := p;
  173. hp2 := nil;
  174. While Not(assigned(FindRegAlloc(tai_regalloc(p).Reg, tai(hp1.Next)))) And
  175. GetNextInstruction(hp1, hp1) And
  176. InstructionLoadsFromReg(tai_regalloc(p).Reg, hp1) Do
  177. hp2 := hp1;
  178. { move deallocations }
  179. If hp2 <> nil Then
  180. Begin
  181. hp1 := tai(p.previous);
  182. {$ifdef DEBUG_OPTALLOC}
  183. AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
  184. {$endif DEBUG_OPTALLOC}
  185. AsmL.Remove(p);
  186. InsertLLItem(hp2, tai(hp2.Next), p);
  187. { don't remove this deallocation later on when merging dealloc/alloc pairs because
  188. it marks indenpendent use of a register
  189. This could be also achieved by a separate passes for merging first and then later
  190. moving but I did not choose this solution because it takes more time and code (FK) }
  191. tai_regalloc(p).keep:=true;
  192. {$ifdef DEBUG_OPTALLOC}
  193. AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg)+' here')),hp2);
  194. {$endif DEBUG_OPTALLOC}
  195. p := hp1;
  196. End
  197. { merge allocations/deallocations }
  198. else if assigned(findregalloc(tai_regalloc(p).reg, tai(p.next))) and
  199. getnextinstruction(p,hp1) and
  200. (not(RegLoadedWithNewValue(tai_regalloc(p).Reg, hp1)) or InstructionLoadsFromReg(tai_regalloc(p).Reg, hp1)) and
  201. { don't merge deallocations/allocation which mark a new use of register, this
  202. enables more possibilities for the peephole optimizer }
  203. not(tai_regalloc(p).keep) then
  204. begin
  205. hp1 := tai(p.previous);
  206. {$ifdef DEBUG_OPTALLOC}
  207. AsmL.InsertAfter(tai_comment.Create(strpnew('Removed deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
  208. {$endif DEBUG_OPTALLOC}
  209. AsmL.remove(p);
  210. p.free;
  211. p := hp1;
  212. end
  213. else
  214. ExcludeRegFromUsedRegs(tai_regalloc(p).Reg,Regs);
  215. End
  216. End
  217. else
  218. ;
  219. End;
  220. P := tai(p.Next);
  221. While Assigned(p) and
  222. (p <> blockend) and
  223. (p.typ in (SkipInstr - [ait_regalloc])) Do
  224. P := tai(P.Next)
  225. End;
  226. end;
  227. ReleaseUsedRegs(Regs);
  228. End;
  229. procedure tasmoptimizer.clear;
  230. begin
  231. if assigned(LabelInfo^.labeltable) then
  232. begin
  233. freemem(LabelInfo^.labeltable);
  234. LabelInfo^.labeltable := nil;
  235. end;
  236. LabelInfo^.labeldif:=0;
  237. LabelInfo^.lowlabel:=high(longint);
  238. LabelInfo^.highlabel:=0;
  239. end;
  240. procedure tasmoptimizer.pass_1;
  241. begin
  242. findlohilabels;
  243. BuildLabelTableAndFixRegAlloc;
  244. end;
  245. Procedure TAsmOptimizer.Optimize;
  246. Var
  247. HP: tai;
  248. Begin
  249. BlockStart := tai(AsmL.First);
  250. pass_1;
  251. While Assigned(BlockStart) Do
  252. Begin
  253. if (cs_opt_peephole in current_settings.optimizerswitches) then
  254. begin
  255. PrePeepHoleOpts;
  256. PeepHoleOptPass1;
  257. PeepHoleOptPass2;
  258. PostPeepHoleOpts;
  259. end;
  260. { free memory }
  261. clear;
  262. { continue where we left off, BlockEnd is either the start of an }
  263. { assembler block or nil}
  264. BlockStart := BlockEnd;
  265. While Assigned(BlockStart) And
  266. (BlockStart.typ = ait_Marker) And
  267. (tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  268. Begin
  269. { we stopped at an assembler block, so skip it }
  270. While GetNextInstruction(BlockStart, BlockStart) And
  271. ((BlockStart.Typ <> Ait_Marker) Or
  272. (tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
  273. { blockstart now contains a tai_marker(mark_AsmBlockEnd) }
  274. If GetNextInstruction(BlockStart, HP) And
  275. ((HP.typ <> ait_Marker) Or
  276. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  277. { There is no assembler block anymore after the current one, so }
  278. { optimize the next block of "normal" instructions }
  279. pass_1
  280. { Otherwise, skip the next assembler block }
  281. else
  282. blockStart := hp;
  283. End
  284. End;
  285. End;
  286. Destructor TAsmOptimizer.Destroy;
  287. Begin
  288. ReleaseUsedRegs(TmpUsedRegs);
  289. if assigned(LabelInfo^.LabelTable) then
  290. Freemem(LabelInfo^.LabelTable);
  291. Dispose(LabelInfo);
  292. inherited Destroy;
  293. End;
  294. constructor TAsmScheduler.Create(_AsmL: TAsmList);
  295. begin
  296. inherited create(_asml,nil,nil,nil);
  297. end;
  298. procedure TAsmScheduler.SchedulerPass1;
  299. var
  300. p : tai;
  301. begin
  302. p:=BlockStart;
  303. while p<>BlockEnd Do
  304. begin
  305. prefetch(pointer(p.Next)^);
  306. if SchedulerPass1Cpu(p) then
  307. continue;
  308. p:=tai(p.next);
  309. end;
  310. end;
  311. procedure TAsmScheduler.Optimize;
  312. Var
  313. HP: tai;
  314. Begin
  315. BlockStart := tai(AsmL.First);
  316. While Assigned(BlockStart) Do
  317. Begin
  318. { Peephole optimizations }
  319. SchedulerPass1;
  320. { continue where we left off, BlockEnd is either the start of an }
  321. { assembler block or nil}
  322. BlockStart:=BlockEnd;
  323. While Assigned(BlockStart) And
  324. (BlockStart.typ = ait_Marker) And
  325. (tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  326. Begin
  327. { we stopped at an assembler block, so skip it }
  328. While GetNextInstruction(BlockStart, BlockStart) And
  329. ((BlockStart.Typ <> Ait_Marker) Or
  330. (tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
  331. { blockstart now contains a tai_marker(mark_AsmBlockEnd) }
  332. If not(GetNextInstruction(BlockStart, HP) And
  333. ((HP.typ <> ait_Marker) Or
  334. (Tai_Marker(HP).Kind <> mark_AsmBlockStart))) Then
  335. { skip the next assembler block }
  336. blockStart := hp;
  337. End
  338. End;
  339. End;
  340. procedure Optimize(AsmL:TAsmList);
  341. var
  342. p : TAsmOptimizer;
  343. begin
  344. ResumeTimer(ct_aopt);
  345. p:=casmoptimizer.Create(AsmL);
  346. p.Optimize;
  347. {$ifdef DEBUG_INSTRUCTIONREGISTERDEPENDENCIES}
  348. p.Debug_InsertInstrRegisterDependencyInfo;
  349. {$endif DEBUG_INSTRUCTIONREGISTERDEPENDENCIES}
  350. p.free;
  351. StopTimer;
  352. end;
  353. procedure PreRegallocSchedule(AsmL:TAsmList);
  354. var
  355. p : TAsmScheduler;
  356. begin
  357. p:=cpreregallocscheduler.Create(AsmL);
  358. p.Optimize;
  359. p.free
  360. end;
  361. end.