aoptcpu.pas 11 KB

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