aoptcpu.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe
  3. This unit calls the optimization procedures to optimize the assembler
  4. code for sparc
  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 aoptcpu;
  19. {$i fpcdefs.inc}
  20. Interface
  21. uses
  22. cpubase, aoptobj, aoptcpub, aopt, aoptx86,
  23. aasmtai;
  24. Type
  25. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  26. function PeepHoleOptPass1Cpu(var p : tai) : boolean; override;
  27. function PostPeepHoleOptsCpu(var p : tai) : boolean; override;
  28. procedure PostPeepHoleOpts; override;
  29. End;
  30. Implementation
  31. uses
  32. globals,
  33. verbose,
  34. cpuinfo,
  35. aasmcpu,
  36. aoptutils;
  37. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p : tai) : boolean;
  38. var
  39. hp1 : tai;
  40. hp2 : tai;
  41. begin
  42. result:=false;
  43. case p.typ of
  44. ait_instruction:
  45. begin
  46. case taicpu(p).opcode of
  47. A_MOV:
  48. begin
  49. if MatchInstruction(p,A_MOV,[S_W]) and
  50. MatchOpType(taicpu(p),top_ref,top_reg) and
  51. GetNextInstruction(p, hp1) and
  52. MatchInstruction(hp1,A_MOV,[S_W]) and
  53. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  54. GetNextInstruction(hp1, hp2) and
  55. MatchInstruction(hp2,A_MOV,[S_W]) and
  56. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  57. not(OpsEqual(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^)) and
  58. OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp2).oper[0]^) and
  59. (MatchOperand(taicpu(hp2).oper[1]^,NR_ES) or MatchOperand(taicpu(hp2).oper[1]^,NR_DS) or
  60. ((current_settings.cputype>=cpu_386) and
  61. (MatchOperand(taicpu(hp2).oper[1]^,NR_SS) or
  62. MatchOperand(taicpu(hp2).oper[1]^,NR_FS) or
  63. MatchOperand(taicpu(hp2).oper[1]^,NR_GS)))) and
  64. (taicpu(p).oper[0]^.ref^.base=taicpu(hp1).oper[0]^.ref^.base) and
  65. (taicpu(p).oper[0]^.ref^.index=taicpu(hp1).oper[0]^.ref^.index) and
  66. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp1).oper[0]^.ref^.segment) and
  67. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp1).oper[0]^.ref^.symbol) and
  68. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp1).oper[0]^.ref^.relsymbol) and
  69. (taicpu(p).oper[0]^.ref^.offset+2=taicpu(hp1).oper[0]^.ref^.offset) and
  70. assigned(FindRegDealloc(taicpu(hp2).oper[0]^.reg,tai(hp2.Next))) then
  71. begin
  72. case taicpu(hp2).oper[1]^.reg of
  73. NR_DS:
  74. taicpu(p).opcode:=A_LDS;
  75. NR_ES:
  76. taicpu(p).opcode:=A_LES;
  77. NR_SS:
  78. taicpu(p).opcode:=A_LSS;
  79. NR_FS:
  80. taicpu(p).opcode:=A_LFS;
  81. NR_GS:
  82. taicpu(p).opcode:=A_LGS;
  83. else
  84. internalerror(2015092601);
  85. end;
  86. asml.remove(hp1);
  87. hp1.free;
  88. asml.remove(hp2);
  89. hp2.free;
  90. result:=true;
  91. end;
  92. end;
  93. A_SUB:
  94. result:=OptPass1Sub(p);
  95. end;
  96. end
  97. end;
  98. end;
  99. function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  100. begin
  101. result := false;
  102. case p.typ of
  103. ait_instruction:
  104. begin
  105. case taicpu(p).opcode of
  106. {A_MOV commented out, because it still breaks some i8086 code :( }
  107. {A_MOV:
  108. Result:=PostPeepholeOptMov(p);}
  109. A_CMP:
  110. Result:=PostPeepholeOptCmp(p);
  111. A_OR,
  112. A_TEST:
  113. Result:=PostPeepholeOptTestOr(p);
  114. end;
  115. end;
  116. end;
  117. end;
  118. procedure TCpuAsmOptimizer.PostPeepHoleOpts;
  119. begin
  120. inherited;
  121. OptReferences;
  122. end;
  123. begin
  124. casmoptimizer:=TCpuAsmOptimizer;
  125. end.