2
0

aopt386.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe
  3. This unit calls the optimization procedures to optimize the assembler
  4. code for i386+
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aopt386;
  19. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. aasmbase,aasmtai,aasmdata,aasmcpu;
  23. Procedure Optimize(AsmL: TAsmList);
  24. Implementation
  25. Uses
  26. globtype,
  27. globals,
  28. DAOpt386,POpt386;
  29. Procedure Optimize(AsmL: TAsmList);
  30. Var
  31. BlockStart, BlockEnd, HP: Tai;
  32. pass: longint;
  33. slowopt, changed, lastLoop: boolean;
  34. Begin
  35. slowopt := (cs_opt_level3 in current_settings.optimizerswitches);
  36. pass := 0;
  37. changed := false;
  38. dfa := TDFAObj.create(asml);
  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 := dfa.pass_1(blockstart);
  49. { Blockend now either contains an ait_marker with Kind = mark_AsmBlockStart, }
  50. { or nil }
  51. While Assigned(BlockStart) Do
  52. Begin
  53. if (cs_opt_peephole in current_settings.optimizerswitches) then
  54. begin
  55. if (pass = 0) then
  56. PrePeepHoleOpts(AsmL, BlockStart, BlockEnd);
  57. { Peephole optimizations }
  58. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  59. { Only perform them twice in the first pass }
  60. if pass = 0 then
  61. PeepHoleOptPass1(AsmL, BlockStart, BlockEnd);
  62. end;
  63. { More peephole optimizations }
  64. if (cs_opt_peephole in current_settings.optimizerswitches) then
  65. begin
  66. PeepHoleOptPass2(AsmL, BlockStart, BlockEnd);
  67. if lastLoop then
  68. PostPeepHoleOpts(AsmL, BlockStart, BlockEnd);
  69. end;
  70. { Free memory }
  71. dfa.clear;
  72. { Continue where we left off, BlockEnd is either the start of an }
  73. { assembler block or nil }
  74. BlockStart := BlockEnd;
  75. While Assigned(BlockStart) And
  76. (BlockStart.typ = ait_Marker) And
  77. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  78. Begin
  79. { We stopped at an assembler block, so skip it }
  80. Repeat
  81. BlockStart := Tai(BlockStart.Next);
  82. Until (BlockStart.Typ = Ait_Marker) And
  83. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  84. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  85. If GetNextInstruction(BlockStart, HP) And
  86. ((HP.typ <> ait_Marker) Or
  87. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  88. { There is no assembler block anymore after the current one, so }
  89. { optimize the next block of "normal" instructions }
  90. BlockEnd := dfa.pass_1(blockstart)
  91. { Otherwise, skip the next assembler block }
  92. else
  93. blockStart := hp;
  94. End;
  95. End;
  96. inc(pass);
  97. until lastLoop;
  98. dfa.free;
  99. End;
  100. End.