aoptcpu.pas 9.5 KB

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