aoptcpu.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. begin
  184. UpdateUsedRegs(p);
  185. p := tai(p.Next);
  186. end
  187. else
  188. begin
  189. p := tai(p.Next);
  190. UpdateUsedRegs(p);
  191. end;
  192. Result := True;
  193. end;
  194. end;
  195. end;
  196. function TCpuAsmOptimizer.PeepHoleOptPass2Cpu(var p : tai) : boolean;
  197. begin
  198. Result := False;
  199. case p.typ of
  200. ait_instruction:
  201. begin
  202. case taicpu(p).opcode of
  203. A_MOV:
  204. Result:=OptPass2MOV(p);
  205. A_MOVZX:
  206. Result:=OptPass2Movx(p);
  207. A_IMUL:
  208. Result:=OptPass2Imul(p);
  209. A_JMP:
  210. Result:=OptPass2Jmp(p);
  211. A_Jcc:
  212. Result:=OptPass2Jcc(p);
  213. A_Lea:
  214. Result:=OptPass2Lea(p);
  215. A_SUB:
  216. Result:=OptPass2SUB(p);
  217. A_ADD:
  218. Result:=OptPass2ADD(p);
  219. A_CMOVcc:
  220. Result:=OptPass2CMOVcc(p);
  221. A_SETcc:
  222. result:=OptPass2SETcc(p);
  223. A_CMP:
  224. Result:=OptPass2CMP(p);
  225. A_TEST:
  226. Result:=OptPass2TEST(p);
  227. A_CLC,
  228. A_STC:
  229. Result:=OptPass2STCCLC(p);
  230. else
  231. ;
  232. end;
  233. end;
  234. else
  235. ;
  236. end;
  237. { If this flag is set, force another run of pass 2 even if p wasn't
  238. changed (-O3 only), but otherwise move p ahead by 1 instruction
  239. and treat as if Result was set to True }
  240. if aoc_ForceNewIteration in OptsToCheck then
  241. begin
  242. Exclude(OptsToCheck, aoc_ForceNewIteration);
  243. if not Result then
  244. begin
  245. if (p.typ in SkipInstr) then
  246. begin
  247. UpdateUsedRegs(p);
  248. p := tai(p.Next);
  249. end
  250. else
  251. begin
  252. p := tai(p.Next);
  253. UpdateUsedRegs(p);
  254. end;
  255. Result := True;
  256. end;
  257. end;
  258. end;
  259. function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  260. begin
  261. result := false;
  262. case p.typ of
  263. ait_instruction:
  264. begin
  265. case taicpu(p).opcode of
  266. A_MOV:
  267. Result:=PostPeepholeOptMov(p);
  268. A_AND:
  269. Result:=PostPeepholeOptAnd(p);
  270. A_MOVSX,
  271. A_MOVSXD:
  272. Result:=PostPeepholeOptMOVSX(p);
  273. A_MOVZX:
  274. Result:=PostPeepholeOptMovzx(p);
  275. A_CMP:
  276. Result:=PostPeepholeOptCmp(p);
  277. A_OR,
  278. A_TEST:
  279. Result:=PostPeepholeOptTestOr(p);
  280. A_XOR:
  281. Result:=PostPeepholeOptXor(p);
  282. A_CALL:
  283. Result:=PostPeepholeOptCall(p);
  284. A_LEA:
  285. Result:=PostPeepholeOptLea(p);
  286. A_PUSH:
  287. Result:=PostPeepholeOptPush(p);
  288. A_SHR:
  289. Result:=PostPeepholeOptShr(p);
  290. A_ADD,
  291. A_SUB:
  292. Result:=PostPeepholeOptADDSUB(p);
  293. A_RET:
  294. Result:=PostPeepholeOptRET(p);
  295. A_VPXOR:
  296. Result:=PostPeepholeOptVPXOR(p);
  297. else
  298. ;
  299. end;
  300. { Optimise any reference-type operands (if Result is True, the
  301. instruction will be checked on the next iteration) }
  302. if not Result then
  303. OptimizeRefs(taicpu(p));
  304. end;
  305. else
  306. ;
  307. end;
  308. { If this flag is set, something was optimised ahead of p, so move
  309. ahead by 1 instruction but treat as if Result was set to True }
  310. if aoc_ForceNewIteration in OptsToCheck then
  311. begin
  312. Exclude(OptsToCheck, aoc_ForceNewIteration);
  313. if not Result then
  314. begin
  315. if (p.typ in SkipInstr) then
  316. UpdateUsedRegs(p);
  317. p := tai(p.Next);
  318. Result := True;
  319. end;
  320. end;
  321. end;
  322. begin
  323. casmoptimizer := TCpuAsmOptimizer;
  324. end.