aoptcpu.pas 9.7 KB

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