aoptcpu.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 cpubase, aasmtai, aopt, aoptx86;
  22. type
  23. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  24. function PrePeepHoleOptsCpu(var p: tai): boolean; override;
  25. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  26. function PeepHoleOptPass2Cpu(var p: tai): boolean; override;
  27. function PostPeepHoleOptsCpu(var p : tai) : boolean; override;
  28. end;
  29. implementation
  30. uses
  31. globals,
  32. aasmcpu;
  33. function TCpuAsmOptimizer.PrePeepHoleOptsCpu(var p : tai) : boolean;
  34. begin
  35. result := false;
  36. case p.typ of
  37. ait_instruction:
  38. begin
  39. case taicpu(p).opcode of
  40. A_IMUL:
  41. result:=PrePeepholeOptIMUL(p);
  42. A_SAR,A_SHR:
  43. result:=PrePeepholeOptSxx(p);
  44. A_AND:
  45. Result:=PrePeepholeOptAND(p);
  46. else
  47. ;
  48. end;
  49. end;
  50. else
  51. ;
  52. end;
  53. { If this flag is set, something was optimised ahead of p, so move
  54. ahead by 1 instruction but treat as if Result was set to True }
  55. if aoc_ForceNewIteration in OptsToCheck then
  56. begin
  57. Exclude(OptsToCheck, aoc_ForceNewIteration);
  58. if not Result then
  59. begin
  60. if (p.typ in SkipInstr) then
  61. UpdateUsedRegs(p);
  62. p := tai(p.Next);
  63. Result := True;
  64. end;
  65. end;
  66. end;
  67. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  68. begin
  69. result:=False;
  70. case p.typ of
  71. ait_instruction:
  72. begin
  73. case taicpu(p).opcode of
  74. A_ADD:
  75. Result:=OptPass1ADD(p);
  76. A_AND:
  77. Result:=OptPass1AND(p);
  78. A_IMUL:
  79. Result:=OptPass1Imul(p);
  80. A_MOV:
  81. Result:=OptPass1MOV(p);
  82. A_MOVSX,
  83. A_MOVSXD,
  84. A_MOVZX:
  85. Result:=OptPass1Movx(p);
  86. A_MOVDQA,
  87. A_MOVAPD,
  88. A_MOVAPS,
  89. A_MOVUPD,
  90. A_MOVUPS,
  91. A_VMOVAPS,
  92. A_VMOVAPD,
  93. A_VMOVUPS,
  94. A_VMOVUPD:
  95. result:=OptPass1_V_MOVAP(p);
  96. A_VMINSS,
  97. A_VMINSD,
  98. A_VMAXSS,
  99. A_VMAXSD,
  100. A_VSQRTSD,
  101. A_VSQRTSS,
  102. A_VDIVSD,
  103. A_VDIVSS,
  104. A_VSUBSD,
  105. A_VSUBSS,
  106. A_VMULSD,
  107. A_VMULSS,
  108. A_VADDSD,
  109. A_VADDSS,
  110. A_VANDPD,
  111. A_VANDPS,
  112. A_VORPD,
  113. A_VORPS:
  114. result:=OptPass1VOP(p);
  115. A_MULSD,
  116. A_MULSS,
  117. A_ADDSD,
  118. A_ADDSS:
  119. result:=OptPass1OP(p);
  120. A_VMOVSD,
  121. A_VMOVSS,
  122. A_MOVSD,
  123. A_MOVSS:
  124. result:=OptPass1MOVXX(p);
  125. A_LEA:
  126. result:=OptPass1LEA(p);
  127. A_SUB:
  128. result:=OptPass1Sub(p);
  129. A_SHL,A_SAL:
  130. result:=OptPass1SHLSAL(p);
  131. A_SHR:
  132. result:=OptPass1SHR(p);
  133. A_FSTP,A_FISTP:
  134. result:=OptPass1FSTP(p);
  135. A_FLD:
  136. result:=OptPass1FLD(p);
  137. A_CMP:
  138. result:=OptPass1Cmp(p);
  139. A_VPXORD,
  140. A_VPXORQ,
  141. A_VXORPS,
  142. A_VXORPD,
  143. A_VPXOR:
  144. Result:=OptPass1VPXor(p);
  145. A_VMOVDQA,
  146. A_VMOVDQU:
  147. Result:=OptPass1VMOVDQ(p);
  148. A_XORPS,
  149. A_XORPD,
  150. A_PXOR:
  151. Result:=OptPass1PXor(p);
  152. A_TEST:
  153. Result:=OptPass1Test(p);
  154. A_Jcc:
  155. Result:=OptPass1Jcc(p);
  156. A_SHRX,
  157. A_SHLX:
  158. Result:=OptPass1SHXX(p);
  159. A_VCVTSS2SD,
  160. A_CVTSS2SD:
  161. Result:=OptPass1_V_Cvtss2sd(p);
  162. else
  163. ;
  164. end;
  165. end;
  166. else
  167. ;
  168. end;
  169. { If this flag is set, force another run of pass 1 even if p wasn't
  170. changed }
  171. if aoc_ForceNewIteration in OptsToCheck then
  172. begin
  173. Exclude(OptsToCheck, aoc_ForceNewIteration);
  174. if not Result then
  175. begin
  176. if (p.typ in SkipInstr) then
  177. UpdateUsedRegs(p);
  178. p := tai(p.Next);
  179. Result := True;
  180. end;
  181. end;
  182. end;
  183. function TCpuAsmOptimizer.PeepHoleOptPass2Cpu(var p : tai) : boolean;
  184. begin
  185. Result := False;
  186. case p.typ of
  187. ait_instruction:
  188. begin
  189. case taicpu(p).opcode of
  190. A_MOV:
  191. Result:=OptPass2MOV(p);
  192. A_MOVZX:
  193. Result:=OptPass2Movx(p);
  194. A_IMUL:
  195. Result:=OptPass2Imul(p);
  196. A_JMP:
  197. Result:=OptPass2Jmp(p);
  198. A_Jcc:
  199. Result:=OptPass2Jcc(p);
  200. A_Lea:
  201. Result:=OptPass2Lea(p);
  202. A_SUB:
  203. Result:=OptPass2SUB(p);
  204. A_ADD:
  205. Result:=OptPass2ADD(p);
  206. A_SETcc:
  207. result:=OptPass2SETcc(p);
  208. else
  209. ;
  210. end;
  211. end;
  212. else
  213. ;
  214. end;
  215. { If this flag is set, something was optimised ahead of p, so move
  216. ahead by 1 instruction but treat as if Result was set to True }
  217. if aoc_ForceNewIteration in OptsToCheck then
  218. begin
  219. Exclude(OptsToCheck, aoc_ForceNewIteration);
  220. if not Result then
  221. begin
  222. if (p.typ in SkipInstr) then
  223. UpdateUsedRegs(p);
  224. p := tai(p.Next);
  225. Result := True;
  226. end;
  227. end;
  228. end;
  229. function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  230. begin
  231. result := false;
  232. case p.typ of
  233. ait_instruction:
  234. begin
  235. case taicpu(p).opcode of
  236. A_MOV:
  237. Result:=PostPeepholeOptMov(p);
  238. A_AND:
  239. Result:=PostPeepholeOptAnd(p);
  240. A_MOVSX,
  241. A_MOVSXD:
  242. Result:=PostPeepholeOptMOVSX(p);
  243. A_MOVZX:
  244. Result:=PostPeepholeOptMovzx(p);
  245. A_CMP:
  246. Result:=PostPeepholeOptCmp(p);
  247. A_OR,
  248. A_TEST:
  249. Result:=PostPeepholeOptTestOr(p);
  250. A_XOR:
  251. Result:=PostPeepholeOptXor(p);
  252. A_CALL:
  253. Result:=PostPeepholeOptCall(p);
  254. A_LEA:
  255. Result:=PostPeepholeOptLea(p);
  256. A_PUSH:
  257. Result:=PostPeepholeOptPush(p);
  258. A_SHR:
  259. Result:=PostPeepholeOptShr(p);
  260. A_ADD,
  261. A_SUB:
  262. Result:=PostPeepholeOptADDSUB(p);
  263. A_VPXOR:
  264. Result:=PostPeepholeOptVPXOR(p);
  265. else
  266. ;
  267. end;
  268. { Optimise any reference-type operands (if Result is True, the
  269. instruction will be checked on the next iteration) }
  270. if not Result then
  271. OptimizeRefs(taicpu(p));
  272. end;
  273. else
  274. ;
  275. end;
  276. { If this flag is set, something was optimised ahead of p, so move
  277. ahead by 1 instruction but treat as if Result was set to True }
  278. if aoc_ForceNewIteration in OptsToCheck then
  279. begin
  280. Exclude(OptsToCheck, aoc_ForceNewIteration);
  281. if not Result then
  282. begin
  283. if (p.typ in SkipInstr) then
  284. UpdateUsedRegs(p);
  285. p := tai(p.Next);
  286. Result := True;
  287. end;
  288. end;
  289. end;
  290. begin
  291. casmoptimizer := TCpuAsmOptimizer;
  292. end.