aoptcpu.pas 10 KB

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