aoptcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer for i386
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aoptcpu;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_AOPTCPU}
  20. Interface
  21. uses
  22. cgbase,
  23. cpubase, aopt, aoptx86,
  24. Aasmbase,aasmtai,aasmdata;
  25. Type
  26. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  27. function PrePeepHoleOptsCpu(var p: tai): boolean; override;
  28. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  29. function PeepHoleOptPass2Cpu(var p: tai): boolean; override;
  30. function PostPeepHoleOptsCpu(var p : tai) : boolean; override;
  31. end;
  32. Var
  33. AsmOptimizer : TCpuAsmOptimizer;
  34. Implementation
  35. uses
  36. verbose,globtype,globals,
  37. cpuinfo,
  38. aasmcpu,
  39. aoptutils,
  40. aasmcfi,
  41. procinfo,
  42. cgutils,
  43. { units we should get rid off: }
  44. symsym,symconst;
  45. { Checks if the register is a 32 bit general purpose register }
  46. function isgp32reg(reg: TRegister): boolean;
  47. begin
  48. {$push}{$warnings off}
  49. isgp32reg:=(getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)>=RS_EAX) and (getsupreg(reg)<=RS_EBX);
  50. {$pop}
  51. end;
  52. { returns true if p contains a memory operand with a segment set }
  53. function InsContainsSegRef(p: taicpu): boolean;
  54. var
  55. i: longint;
  56. begin
  57. result:=true;
  58. for i:=0 to p.opercnt-1 do
  59. if (p.oper[i]^.typ=top_ref) and
  60. (p.oper[i]^.ref^.segment<>NR_NO) then
  61. exit;
  62. result:=false;
  63. end;
  64. function TCPUAsmOPtimizer.PrePeepHoleOptsCpu(var p: tai): boolean;
  65. begin
  66. repeat
  67. Result:=False;
  68. case p.typ of
  69. ait_instruction:
  70. begin
  71. if InsContainsSegRef(taicpu(p)) then
  72. begin
  73. p := tai(p.next);
  74. { Nothing's actually changed, so no need to set Result to True,
  75. but try again to see if an instruction immediately follows }
  76. Continue;
  77. end;
  78. case taicpu(p).opcode Of
  79. A_IMUL:
  80. Result:=PrePeepholeOptIMUL(p);
  81. A_SAR,A_SHR:
  82. Result:=PrePeepholeOptSxx(p);
  83. A_AND:
  84. Result:=PrePeepholeOptAND(p);
  85. A_XOR:
  86. begin
  87. if (taicpu(p).oper[0]^.typ = top_reg) and
  88. (taicpu(p).oper[1]^.typ = top_reg) and
  89. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  90. { temporarily change this to 'mov reg,0' to make it easier }
  91. { for the CSE. Will be changed back in pass 2 }
  92. begin
  93. taicpu(p).opcode := A_MOV;
  94. taicpu(p).loadConst(0,0);
  95. Result:=true;
  96. end;
  97. end;
  98. else
  99. { Do nothing };
  100. end;
  101. end;
  102. else
  103. { Do nothing };
  104. end;
  105. Break;
  106. until False;
  107. end;
  108. function TCPUAsmOPtimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  109. var
  110. hp1 : tai;
  111. begin
  112. result:=False;
  113. case p.Typ Of
  114. ait_instruction:
  115. begin
  116. current_filepos:=taicpu(p).fileinfo;
  117. if InsContainsSegRef(taicpu(p)) then
  118. exit;
  119. case taicpu(p).opcode Of
  120. A_ADD:
  121. Result:=OptPass1ADD(p);
  122. A_AND:
  123. Result:=OptPass1And(p);
  124. A_IMUL:
  125. Result:=OptPass1Imul(p);
  126. A_CMP:
  127. Result:=OptPass1Cmp(p);
  128. A_VPXORD,
  129. A_VPXORQ,
  130. A_VXORPS,
  131. A_VXORPD,
  132. A_VPXOR:
  133. Result:=OptPass1VPXor(p);
  134. A_XORPS,
  135. A_XORPD,
  136. A_PXOR:
  137. Result:=OptPass1PXor(p);
  138. A_FLD:
  139. Result:=OptPass1FLD(p);
  140. A_FSTP,A_FISTP:
  141. Result:=OptPass1FSTP(p);
  142. A_LEA:
  143. Result:=OptPass1LEA(p);
  144. A_MOV:
  145. Result:=OptPass1MOV(p);
  146. A_MOVSX,
  147. A_MOVZX :
  148. Result:=OptPass1Movx(p);
  149. A_TEST:
  150. Result:=OptPass1Test(p);
  151. A_PUSH:
  152. begin
  153. if (taicpu(p).opsize = S_W) and
  154. (taicpu(p).oper[0]^.typ = Top_Const) and
  155. GetNextInstruction(p, hp1) and
  156. (tai(hp1).typ = ait_instruction) and
  157. (taicpu(hp1).opcode = A_PUSH) and
  158. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  159. (taicpu(hp1).opsize = S_W) then
  160. begin
  161. taicpu(p).changeopsize(S_L);
  162. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  163. asml.remove(hp1);
  164. hp1.free;
  165. Result:=true;
  166. end;
  167. end;
  168. A_SHL, A_SAL:
  169. Result:=OptPass1SHLSAL(p);
  170. A_SUB:
  171. Result:=OptPass1Sub(p);
  172. A_Jcc:
  173. Result:=OptPass1Jcc(p);
  174. A_MOVAPD,
  175. A_MOVAPS,
  176. A_MOVUPD,
  177. A_MOVUPS,
  178. A_VMOVAPS,
  179. A_VMOVAPD,
  180. A_VMOVUPS,
  181. A_VMOVUPD:
  182. Result:=OptPass1_V_MOVAP(p);
  183. A_VDIVSD,
  184. A_VDIVSS,
  185. A_VSUBSD,
  186. A_VSUBSS,
  187. A_VMULSD,
  188. A_VMULSS,
  189. A_VADDSD,
  190. A_VADDSS,
  191. A_VANDPD,
  192. A_VANDPS,
  193. A_VORPD,
  194. A_VORPS:
  195. Result:=OptPass1VOP(p);
  196. A_MULSD,
  197. A_MULSS,
  198. A_ADDSD,
  199. A_ADDSS:
  200. Result:=OptPass1OP(p);
  201. A_VMOVSD,
  202. A_VMOVSS,
  203. A_MOVSD,
  204. A_MOVSS:
  205. Result:=OptPass1MOVXX(p);
  206. A_SHRX,
  207. A_SHLX:
  208. Result:=OptPass1SHXX(p);
  209. A_VCVTSS2SD,
  210. A_CVTSS2SD:
  211. Result:=OptPass1_V_Cvtss2sd(p);
  212. else
  213. ;
  214. end;
  215. end;
  216. else
  217. ;
  218. end;
  219. end;
  220. function TCPUAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
  221. begin
  222. Result:=false;
  223. case p.Typ Of
  224. Ait_Instruction:
  225. begin
  226. if InsContainsSegRef(taicpu(p)) then
  227. exit;
  228. case taicpu(p).opcode Of
  229. A_ADD:
  230. Result:=OptPass2ADD(p);
  231. A_Jcc:
  232. Result:=OptPass2Jcc(p);
  233. A_Lea:
  234. Result:=OptPass2Lea(p);
  235. A_FSTP,A_FISTP:
  236. Result:=OptPass1FSTP(p);
  237. A_IMUL:
  238. Result:=OptPass2Imul(p);
  239. A_JMP:
  240. Result:=OptPass2Jmp(p);
  241. A_MOV:
  242. Result:=OptPass2MOV(p);
  243. A_MOVZX:
  244. Result:=OptPass2Movx(p);
  245. A_SUB:
  246. Result:=OptPass2SUB(p);
  247. A_SETcc:
  248. Result:=OptPass2SETcc(p);
  249. else
  250. ;
  251. end;
  252. end;
  253. else
  254. ;
  255. end;
  256. end;
  257. function TCPUAsmOptimizer.PostPeepHoleOptsCpu(var p : tai) : boolean;
  258. var
  259. hp1: tai;
  260. begin
  261. Result:=false;
  262. case p.Typ Of
  263. Ait_Instruction:
  264. begin
  265. if InsContainsSegRef(taicpu(p)) then
  266. Exit;
  267. case taicpu(p).opcode Of
  268. A_CALL:
  269. Result:=PostPeepHoleOptCall(p);
  270. A_LEA:
  271. Result:=PostPeepholeOptLea(p);
  272. A_CMP:
  273. Result:=PostPeepholeOptCmp(p);
  274. A_MOV:
  275. Result:=PostPeepholeOptMov(p);
  276. A_MOVZX:
  277. { if register vars are on, it's possible there is code like }
  278. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  279. { so we can't safely replace the movzx then with xor/mov, }
  280. { since that would change the flags (JM) }
  281. if PostPeepholeOptMovzx(p) then
  282. Result := True
  283. else if not(cs_opt_regvar in current_settings.optimizerswitches) then
  284. begin
  285. if (taicpu(p).oper[1]^.typ = top_reg) then
  286. if (taicpu(p).oper[0]^.typ = top_reg)
  287. then
  288. case taicpu(p).opsize of
  289. S_BL:
  290. begin
  291. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  292. not(cs_opt_size in current_settings.optimizerswitches) and
  293. (current_settings.optimizecputype = cpu_Pentium) then
  294. {Change "movzbl %reg1, %reg2" to
  295. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  296. PentiumMMX}
  297. begin
  298. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  299. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  300. InsertLLItem(p.previous, p, hp1);
  301. taicpu(p).opcode := A_MOV;
  302. taicpu(p).changeopsize(S_B);
  303. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  304. Result := True;
  305. end;
  306. end;
  307. else
  308. ;
  309. end
  310. else if (taicpu(p).oper[0]^.typ = top_ref) and
  311. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  312. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  313. not(cs_opt_size in current_settings.optimizerswitches) and
  314. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  315. (current_settings.optimizecputype = cpu_Pentium) and
  316. (taicpu(p).opsize = S_BL) then
  317. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  318. Pentium and PentiumMMX}
  319. begin
  320. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  321. taicpu(p).oper[1]^.reg);
  322. taicpu(p).opcode := A_MOV;
  323. taicpu(p).changeopsize(S_B);
  324. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  325. InsertLLItem(p.previous, p, hp1);
  326. Result := True;
  327. end;
  328. end;
  329. A_TEST, A_OR:
  330. Result:=PostPeepholeOptTestOr(p);
  331. A_AND:
  332. Result:=PostPeepholeOptAnd(p);
  333. A_MOVSX:
  334. Result:=PostPeepholeOptMOVSX(p);
  335. A_SHR:
  336. Result:=PostPeepholeOptShr(p);
  337. else
  338. ;
  339. end;
  340. { Optimise any reference-type operands (if Result is True, the
  341. instruction will be checked on the next iteration) }
  342. if not Result then
  343. OptimizeRefs(taicpu(p));
  344. end;
  345. else
  346. ;
  347. end;
  348. end;
  349. begin
  350. casmoptimizer:=TCpuAsmOptimizer;
  351. end.