aoptcpu.pas 13 KB

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