aoptcpu.pas 9.8 KB

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