aopt386.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. repeat
  40. lastLoop :=
  41. not(slowopt) or
  42. (not changed and (pass > 2)) or
  43. { prevent endless loops }
  44. (pass = 4);
  45. changed := false;
  46. { Setup labeltable, always necessary }
  47. BlockStart := Tai(AsmL.First);
  48. BlockEnd := DFAPass1(AsmL, BlockStart);
  49. { Blockend now either contains an ait_marker with Kind = AsmBlockStart, }
  50. { or nil }
  51. While Assigned(BlockStart) Do
  52. Begin
  53. if pass = 0 then
  54. PrePeepHoleOpts(AsmL, BlockStart, BlockEnd);
  55. { Peephole optimizations }
  56. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  57. { Only perform them twice in the first pass }
  58. if pass = 0 then
  59. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  60. { Data flow analyzer }
  61. If (cs_fastoptimize in aktglobalswitches) Then
  62. Begin
  63. If DFAPass2(
  64. {$ifdef statedebug}
  65. AsmL,
  66. {$endif statedebug}
  67. BlockStart, BlockEnd) Then
  68. { common subexpression elimination }
  69. changed := CSE(asmL, blockStart, blockEnd, pass) or changed;
  70. End;
  71. { More peephole optimizations }
  72. PeepHoleOptPass2(AsmL, BlockStart, BlockEnd);
  73. if lastLoop then
  74. PostPeepHoleOpts(AsmL, BlockStart, BlockEnd);
  75. { Dispose labeltabel }
  76. ShutDownDFA;
  77. { Continue where we left off, BlockEnd is either the start of an }
  78. { assembler block or nil }
  79. BlockStart := BlockEnd;
  80. While Assigned(BlockStart) And
  81. (BlockStart.typ = ait_Marker) And
  82. (Tai_Marker(BlockStart).Kind = AsmBlockStart) Do
  83. Begin
  84. { We stopped at an assembler block, so skip it }
  85. Repeat
  86. BlockStart := Tai(BlockStart.Next);
  87. Until (BlockStart.Typ = Ait_Marker) And
  88. (Tai_Marker(Blockstart).Kind = AsmBlockEnd);
  89. { Blockstart now contains a Tai_marker(asmblockend) }
  90. If GetNextInstruction(BlockStart, HP) And
  91. ((HP.typ <> ait_Marker) Or
  92. (Tai_Marker(HP).Kind <> AsmBlockStart)) Then
  93. { There is no assembler block anymore after the current one, so }
  94. { optimize the next block of "normal" instructions }
  95. BlockEnd := DFAPass1(AsmL, BlockStart)
  96. { Otherwise, skip the next assembler block }
  97. Else BlockStart := HP;
  98. End;
  99. End;
  100. inc(pass);
  101. until lastLoop;
  102. End;
  103. End.
  104. {
  105. $Log$
  106. Revision 1.7 2002-07-01 18:46:29 peter
  107. * internal linker
  108. * reorganized aasm layer
  109. Revision 1.6 2002/05/18 13:34:21 peter
  110. * readded missing revisions
  111. Revision 1.5 2002/05/16 19:46:50 carl
  112. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  113. + try to fix temp allocation (still in ifdef)
  114. + generic constructor calls
  115. + start of tassembler / tmodulebase class cleanup
  116. }