aopt386.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  21. Interface
  22. Uses
  23. aasm;
  24. Procedure Optimize(AsmL: PAasmOutput);
  25. Implementation
  26. Uses
  27. globtype,
  28. globals,
  29. DAOpt386,POpt386,CSOpt386;
  30. Procedure Optimize(AsmL: PAasmOutput);
  31. Var
  32. count, max: longint;
  33. BlockStart, BlockEnd, HP: Pai;
  34. Begin
  35. if (cs_slowoptimize in aktglobalswitches) then
  36. { Optimize twice }
  37. max := 2
  38. else max := 1;
  39. for count := 1 to max do
  40. begin
  41. { Setup labeltable, always necessary }
  42. BlockStart := Pai(AsmL^.First);
  43. BlockEnd := DFAPass1(AsmL, BlockStart);
  44. { Blockend now either contains an ait_marker with Kind = AsmBlockStart, }
  45. { or nil }
  46. While Assigned(BlockStart) Do
  47. Begin
  48. { Peephole optimizations }
  49. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  50. { Only perform them twice in the first pass }
  51. if count = 1 then
  52. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  53. { Data flow analyzer }
  54. If (cs_slowoptimize in aktglobalswitches) Then
  55. Begin
  56. If DFAPass2(
  57. {$ifdef statedebug}
  58. AsmL,
  59. {$endif statedebug}
  60. BlockStart, BlockEnd) Then
  61. { common subexpression elimination }
  62. CSE(AsmL, BlockStart, BlockEnd);
  63. End;
  64. { More peephole optimizations }
  65. PeepHoleOptPass2(AsmL, BlockStart, BlockEnd);
  66. { Dispose labeltabel }
  67. ShutDownDFA;
  68. { Continue where we left off, BlockEnd is either the start of an }
  69. { assembler block or nil }
  70. BlockStart := BlockEnd;
  71. While Assigned(BlockStart) And
  72. (BlockStart^.typ = ait_Marker) And
  73. (Pai_Marker(BlockStart)^.Kind = AsmBlockStart) Do
  74. Begin
  75. { We stopped at an assembler block, so skip it }
  76. Repeat
  77. BlockStart := Pai(BlockStart^.Next);
  78. Until (BlockStart^.Typ = Ait_Marker) And
  79. (Pai_Marker(Blockstart)^.Kind = AsmBlockEnd);
  80. { Blockstart now contains a pai_marker(asmblockend) }
  81. If GetNextInstruction(BlockStart, HP) And
  82. ((HP^.typ <> ait_Marker) Or
  83. (Pai_Marker(HP)^.Kind <> AsmBlockStart)) Then
  84. { There is no assembler block anymore after the current one, so }
  85. { optimize the next block of "normal" instructions }
  86. BlockEnd := DFAPass1(AsmL, BlockStart)
  87. { Otherwise, skip the next assembler block }
  88. Else BlockStart := HP;
  89. End
  90. End;
  91. end;
  92. End;
  93. End.
  94. {
  95. $Log$
  96. Revision 1.5 2000-09-24 15:06:11 peter
  97. * use defines.inc
  98. Revision 1.4 2000/08/19 09:10:08 jonas
  99. * for all optimization levels > 1, all passes are done twice (the
  100. result improves the most if -Or is used as well)
  101. Revision 1.3 2000/07/14 05:11:48 michael
  102. + Patch to 1.1
  103. Revision 1.2 2000/07/13 11:32:31 michael
  104. + removed logs
  105. }