aopt386.pas 3.8 KB

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