aopt386.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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,CSOpt386;
  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 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_opt_asmcse in current_settings.optimizerswitches) Then
  62. begin
  63. if dfa.pass_generate_code then
  64. { common subexpression elimination }
  65. changed := CSE(asmL, blockStart, blockEnd, pass) or changed;
  66. end;
  67. { More peephole optimizations }
  68. PeepHoleOptPass2(AsmL, BlockStart, BlockEnd);
  69. if lastLoop then
  70. PostPeepHoleOpts(AsmL, BlockStart, BlockEnd);
  71. { Free memory }
  72. dfa.clear;
  73. { Continue where we left off, BlockEnd is either the start of an }
  74. { assembler block or nil }
  75. BlockStart := BlockEnd;
  76. While Assigned(BlockStart) And
  77. (BlockStart.typ = ait_Marker) And
  78. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  79. Begin
  80. { We stopped at an assembler block, so skip it }
  81. Repeat
  82. BlockStart := Tai(BlockStart.Next);
  83. Until (BlockStart.Typ = Ait_Marker) And
  84. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  85. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  86. If GetNextInstruction(BlockStart, HP) And
  87. ((HP.typ <> ait_Marker) Or
  88. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  89. { There is no assembler block anymore after the current one, so }
  90. { optimize the next block of "normal" instructions }
  91. BlockEnd := dfa.pass_1(blockstart)
  92. { Otherwise, skip the next assembler block }
  93. else
  94. blockStart := hp;
  95. End;
  96. End;
  97. inc(pass);
  98. until lastLoop;
  99. dfa.free;
  100. End;
  101. End.