aopt386.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe
  4. This unit calls the optimization procedures to optimize the assembler
  5. code for i386+
  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 aopt386;
  20. {$i fpcdefs.inc}
  21. Interface
  22. Uses
  23. aasmbase,aasmtai,aasmcpu;
  24. Procedure Optimize(AsmL: TAasmOutput);
  25. Implementation
  26. Uses
  27. globtype,
  28. globals,
  29. DAOpt386,POpt386,CSOpt386;
  30. Procedure Optimize(AsmL: TAAsmOutput);
  31. Var
  32. BlockStart, BlockEnd, HP: Tai;
  33. pass: longint;
  34. slowopt, changed, lastLoop: boolean;
  35. Begin
  36. slowopt := (cs_slowoptimize in aktglobalswitches);
  37. pass := 0;
  38. changed := false;
  39. dfa := TDFAObj.create(asml);
  40. repeat
  41. lastLoop :=
  42. not(slowopt) or
  43. (not changed and (pass > 2)) or
  44. { prevent endless loops }
  45. (pass = 4);
  46. changed := false;
  47. { Setup labeltable, always necessary }
  48. blockstart := tai(asml.first);
  49. blockend := dfa.pass_1(blockstart);
  50. { Blockend now either contains an ait_marker with Kind = AsmBlockStart, }
  51. { or nil }
  52. While Assigned(BlockStart) Do
  53. Begin
  54. if pass = 0 then
  55. PrePeepHoleOpts(AsmL, BlockStart, BlockEnd);
  56. { Peephole optimizations }
  57. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  58. { Only perform them twice in the first pass }
  59. if pass = 0 then
  60. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  61. { Data flow analyzer }
  62. If (cs_fastoptimize in aktglobalswitches) Then
  63. begin
  64. if dfa.pass_2 then
  65. { common subexpression elimination }
  66. changed := CSE(asmL, blockStart, blockEnd, pass) or changed;
  67. end;
  68. { More peephole optimizations }
  69. PeepHoleOptPass2(AsmL, BlockStart, BlockEnd);
  70. if lastLoop then
  71. PostPeepHoleOpts(AsmL, BlockStart, BlockEnd);
  72. { Free memory }
  73. dfa.clear;
  74. { Continue where we left off, BlockEnd is either the start of an }
  75. { assembler block or nil }
  76. BlockStart := BlockEnd;
  77. While Assigned(BlockStart) And
  78. (BlockStart.typ = ait_Marker) And
  79. (Tai_Marker(BlockStart).Kind = AsmBlockStart) Do
  80. Begin
  81. { We stopped at an assembler block, so skip it }
  82. Repeat
  83. BlockStart := Tai(BlockStart.Next);
  84. Until (BlockStart.Typ = Ait_Marker) And
  85. (Tai_Marker(Blockstart).Kind = AsmBlockEnd);
  86. { Blockstart now contains a Tai_marker(asmblockend) }
  87. If GetNextInstruction(BlockStart, HP) And
  88. ((HP.typ <> ait_Marker) Or
  89. (Tai_Marker(HP).Kind <> AsmBlockStart)) Then
  90. { There is no assembler block anymore after the current one, so }
  91. { optimize the next block of "normal" instructions }
  92. BlockEnd := dfa.pass_1(blockstart)
  93. { Otherwise, skip the next assembler block }
  94. else
  95. blockStart := hp;
  96. End;
  97. End;
  98. inc(pass);
  99. until lastLoop;
  100. dfa.free;
  101. End;
  102. End.
  103. {
  104. $Log$
  105. Revision 1.9 2003-06-08 18:48:03 jonas
  106. * first small steps towards an oop optimizer
  107. Revision 1.8 2003/02/26 21:15:43 daniel
  108. * Fixed the optimizer
  109. Revision 1.7 2002/07/01 18:46:29 peter
  110. * internal linker
  111. * reorganized aasm layer
  112. Revision 1.6 2002/05/18 13:34:21 peter
  113. * readded missing revisions
  114. Revision 1.5 2002/05/16 19:46:50 carl
  115. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  116. + try to fix temp allocation (still in ifdef)
  117. + generic constructor calls
  118. + start of tassembler / tmodulebase class cleanup
  119. }