aoptcpu.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. {$define DEBUG_AOPTCPU}
  21. Interface
  22. uses
  23. cpubase, aoptobj, aoptcpub, aopt, aoptx86,
  24. aasmtai;
  25. Type
  26. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  27. function PeepHoleOptPass1Cpu(var p : tai) : boolean; override;
  28. function PostPeepHoleOptsCpu(var p : tai) : boolean; override;
  29. procedure PostPeepHoleOpts; override;
  30. End;
  31. Implementation
  32. uses
  33. globals,
  34. verbose,
  35. cpuinfo,
  36. aasmcpu,
  37. aoptutils;
  38. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p : tai) : boolean;
  39. var
  40. hp1 : tai;
  41. hp2 : tai;
  42. begin
  43. result:=false;
  44. case p.typ of
  45. ait_instruction:
  46. begin
  47. case taicpu(p).opcode of
  48. A_MOV:
  49. begin
  50. if MatchInstruction(p,A_MOV,[S_W]) and
  51. MatchOpType(taicpu(p),top_ref,top_reg) and
  52. GetNextInstruction(p, hp1) and
  53. MatchInstruction(hp1,A_MOV,[S_W]) and
  54. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  55. GetNextInstruction(hp1, hp2) and
  56. MatchInstruction(hp2,A_MOV,[S_W]) and
  57. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  58. not(OpsEqual(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^)) and
  59. OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp2).oper[0]^) and
  60. (MatchOperand(taicpu(hp2).oper[1]^,NR_ES) or MatchOperand(taicpu(hp2).oper[1]^,NR_DS) or
  61. ((current_settings.cputype>=cpu_386) and
  62. (MatchOperand(taicpu(hp2).oper[1]^,NR_SS) or
  63. MatchOperand(taicpu(hp2).oper[1]^,NR_FS) or
  64. MatchOperand(taicpu(hp2).oper[1]^,NR_GS)))) and
  65. (taicpu(p).oper[0]^.ref^.base=taicpu(hp1).oper[0]^.ref^.base) and
  66. (taicpu(p).oper[0]^.ref^.index=taicpu(hp1).oper[0]^.ref^.index) and
  67. (taicpu(p).oper[0]^.ref^.segment=taicpu(hp1).oper[0]^.ref^.segment) and
  68. (taicpu(p).oper[0]^.ref^.symbol=taicpu(hp1).oper[0]^.ref^.symbol) and
  69. (taicpu(p).oper[0]^.ref^.relsymbol=taicpu(hp1).oper[0]^.ref^.relsymbol) and
  70. (taicpu(p).oper[0]^.ref^.offset+2=taicpu(hp1).oper[0]^.ref^.offset) 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. if assigned(FindRegDealloc(taicpu(hp2).oper[0]^.reg,tai(hp2.Next))) then
  87. begin
  88. asml.remove(hp1);
  89. hp1.free;
  90. DebugMsg('Peephole optimizer MovMovMov2LXX',p);
  91. end
  92. else
  93. begin
  94. taicpu(hp1).loadreg(0,taicpu(hp2).oper[1]^.reg);
  95. DebugMsg('Peephole optimizer MovMovMov2LXXMov',p);
  96. end;
  97. asml.remove(hp2);
  98. hp2.free;
  99. result:=true;
  100. end
  101. else if MatchInstruction(p,A_MOV,[S_W]) and
  102. MatchOpType(taicpu(p),top_reg,top_reg) and
  103. GetNextInstruction(p, hp1) and
  104. MatchInstruction(hp1,A_PUSH,[S_W]) and
  105. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  106. assigned(FindRegDealloc(taicpu(hp1).oper[0]^.reg,tai(hp1.Next))) then
  107. begin
  108. DebugMsg('Peephole optimizer MovPush2Push',p);
  109. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  110. { take care of the register (de)allocs following p }
  111. UpdateUsedRegs(tai(p.next));
  112. asml.remove(p);
  113. p.free;
  114. p:=hp1;
  115. result:=true;
  116. end;
  117. end;
  118. A_SUB:
  119. result:=OptPass1Sub(p);
  120. end;
  121. end
  122. end;
  123. end;
  124. function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  125. begin
  126. result := false;
  127. case p.typ of
  128. ait_instruction:
  129. begin
  130. case taicpu(p).opcode of
  131. {A_MOV commented out, because it still breaks some i8086 code :( }
  132. {A_MOV:
  133. Result:=PostPeepholeOptMov(p);}
  134. A_CMP:
  135. Result:=PostPeepholeOptCmp(p);
  136. A_OR,
  137. A_TEST:
  138. Result:=PostPeepholeOptTestOr(p);
  139. end;
  140. end;
  141. end;
  142. end;
  143. procedure TCpuAsmOptimizer.PostPeepHoleOpts;
  144. begin
  145. inherited;
  146. OptReferences;
  147. end;
  148. begin
  149. casmoptimizer:=TCpuAsmOptimizer;
  150. end.