aoptcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. { If this flag is set, something was optimised ahead of p, so move
  110. ahead by 1 instruction but treat as if Result was set to True }
  111. if aoc_ForceNewIteration in OptsToCheck then
  112. begin
  113. Exclude(OptsToCheck, aoc_ForceNewIteration);
  114. if not Result then
  115. begin
  116. if (p.typ in SkipInstr) then
  117. UpdateUsedRegs(p);
  118. p := tai(p.Next);
  119. Result := True;
  120. end;
  121. end;
  122. end;
  123. function TCPUAsmOPtimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  124. var
  125. hp1 : tai;
  126. begin
  127. result:=False;
  128. case p.Typ Of
  129. ait_instruction:
  130. begin
  131. current_filepos:=taicpu(p).fileinfo;
  132. if InsContainsSegRef(taicpu(p)) then
  133. exit;
  134. case taicpu(p).opcode Of
  135. A_ADD:
  136. Result:=OptPass1ADD(p);
  137. A_AND:
  138. Result:=OptPass1And(p);
  139. A_CMOVcc:
  140. Result:=OptPass1CMOVcc(p);
  141. A_IMUL:
  142. Result:=OptPass1Imul(p);
  143. A_CMP:
  144. Result:=OptPass1Cmp(p);
  145. A_VPXORD,
  146. A_VPXORQ,
  147. A_VXORPS,
  148. A_VXORPD,
  149. A_VPXOR:
  150. Result:=OptPass1VPXor(p);
  151. A_XORPS,
  152. A_XORPD,
  153. A_PXOR:
  154. Result:=OptPass1PXor(p);
  155. A_FLD:
  156. Result:=OptPass1FLD(p);
  157. A_FSTP,A_FISTP:
  158. Result:=OptPass1FSTP(p);
  159. A_LEA:
  160. Result:=OptPass1LEA(p);
  161. A_MOV:
  162. Result:=OptPass1MOV(p);
  163. A_MOVD,
  164. A_VMOVD:
  165. Result:=OptPass1MOVD(p);
  166. A_MOVSX,
  167. A_MOVZX:
  168. Result:=OptPass1Movx(p);
  169. A_TEST:
  170. Result:=OptPass1Test(p);
  171. A_PUSH:
  172. begin
  173. if (taicpu(p).opsize = S_W) and
  174. (taicpu(p).oper[0]^.typ = Top_Const) and
  175. GetNextInstruction(p, hp1) and
  176. (tai(hp1).typ = ait_instruction) and
  177. (taicpu(hp1).opcode = A_PUSH) and
  178. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  179. (taicpu(hp1).opsize = S_W) then
  180. begin
  181. taicpu(p).changeopsize(S_L);
  182. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  183. asml.remove(hp1);
  184. hp1.free;
  185. Result:=true;
  186. end;
  187. end;
  188. A_SHL, A_SAL:
  189. Result:=OptPass1SHLSAL(p);
  190. A_SHR:
  191. Result:=OptPass1SHR(p);
  192. A_SUB:
  193. Result:=OptPass1Sub(p);
  194. A_Jcc:
  195. Result:=OptPass1Jcc(p);
  196. A_NOT:
  197. Result:=OptPass1NOT(p);
  198. A_MOVDQA,
  199. A_MOVAPD,
  200. A_MOVAPS,
  201. A_MOVUPD,
  202. A_MOVUPS,
  203. A_VMOVAPS,
  204. A_VMOVAPD,
  205. A_VMOVUPS,
  206. A_VMOVUPD:
  207. Result:=OptPass1_V_MOVAP(p);
  208. A_VDIVSD,
  209. A_VDIVSS,
  210. A_VSUBSD,
  211. A_VSUBSS,
  212. A_VMULSD,
  213. A_VMULSS,
  214. A_VADDSD,
  215. A_VADDSS,
  216. A_VANDPD,
  217. A_VANDPS,
  218. A_VORPD,
  219. A_VORPS:
  220. Result:=OptPass1VOP(p);
  221. A_MULSD,
  222. A_MULSS,
  223. A_ADDSD,
  224. A_ADDSS:
  225. Result:=OptPass1OP(p);
  226. A_VMOVSD,
  227. A_VMOVSS,
  228. A_MOVSD,
  229. A_MOVSS:
  230. Result:=OptPass1MOVXX(p);
  231. A_SHRX,
  232. A_SHLX:
  233. Result:=OptPass1SHXX(p);
  234. A_VMOVDQA,
  235. A_VMOVDQU:
  236. Result:=OptPass1VMOVDQ(p);
  237. A_VCVTSS2SD,
  238. A_CVTSS2SD:
  239. Result:=OptPass1_V_Cvtss2sd(p);
  240. A_CLC,
  241. A_STC:
  242. Result:=OptPass1STCCLC(p);
  243. else
  244. ;
  245. end;
  246. end;
  247. else
  248. ;
  249. end;
  250. { If this flag is set, force another run of pass 1 even if p wasn't
  251. changed }
  252. if aoc_ForceNewIteration in OptsToCheck then
  253. begin
  254. Exclude(OptsToCheck, aoc_ForceNewIteration);
  255. if not Result then
  256. begin
  257. if (p.typ in SkipInstr) then
  258. begin
  259. UpdateUsedRegs(p);
  260. p := tai(p.Next);
  261. end
  262. else
  263. begin
  264. p := tai(p.Next);
  265. UpdateUsedRegs(p);
  266. end;
  267. Result := True;
  268. end;
  269. end;
  270. end;
  271. function TCPUAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
  272. begin
  273. Result:=false;
  274. case p.Typ Of
  275. Ait_Instruction:
  276. begin
  277. if InsContainsSegRef(taicpu(p)) then
  278. exit;
  279. case taicpu(p).opcode Of
  280. A_ADD:
  281. Result:=OptPass2ADD(p);
  282. A_CMOVcc:
  283. Result:=OptPass2CMOVcc(p);
  284. A_CMP:
  285. Result:=OptPass2CMP(p);
  286. A_TEST:
  287. Result:=OptPass2TEST(p);
  288. A_Jcc:
  289. Result:=OptPass2Jcc(p);
  290. A_Lea:
  291. Result:=OptPass2Lea(p);
  292. A_FSTP,A_FISTP:
  293. Result:=OptPass1FSTP(p);
  294. A_IMUL:
  295. Result:=OptPass2Imul(p);
  296. A_JMP:
  297. Result:=OptPass2Jmp(p);
  298. A_MOV:
  299. Result:=OptPass2MOV(p);
  300. A_MOVZX:
  301. Result:=OptPass2Movx(p);
  302. A_SUB:
  303. Result:=OptPass2SUB(p);
  304. A_SETcc:
  305. Result:=OptPass2SETcc(p);
  306. A_CLC,
  307. A_STC:
  308. Result:=OptPass2STCCLC(p);
  309. else
  310. ;
  311. end;
  312. end;
  313. else
  314. ;
  315. end;
  316. { If this flag is set, force another run of pass 2 even if p wasn't
  317. changed (-O3 only), but otherwise move p ahead by 1 instruction
  318. and treat as if Result was set to True }
  319. if aoc_ForceNewIteration in OptsToCheck then
  320. begin
  321. Exclude(OptsToCheck, aoc_ForceNewIteration);
  322. if not Result then
  323. begin
  324. if (p.typ in SkipInstr) then
  325. begin
  326. UpdateUsedRegs(p);
  327. p := tai(p.Next);
  328. end
  329. else
  330. begin
  331. p := tai(p.Next);
  332. UpdateUsedRegs(p);
  333. end;
  334. Result := True;
  335. end;
  336. end;
  337. end;
  338. function TCPUAsmOptimizer.PostPeepHoleOptsCpu(var p : tai) : boolean;
  339. var
  340. hp1: tai;
  341. begin
  342. Result:=false;
  343. case p.Typ Of
  344. Ait_Instruction:
  345. begin
  346. if InsContainsSegRef(taicpu(p)) then
  347. Exit;
  348. case taicpu(p).opcode Of
  349. A_CALL:
  350. Result:=PostPeepHoleOptCall(p);
  351. A_LEA:
  352. Result:=PostPeepholeOptLea(p);
  353. A_CMP:
  354. Result:=PostPeepholeOptCmp(p);
  355. A_MOV:
  356. Result:=PostPeepholeOptMov(p);
  357. A_MOVZX:
  358. { if register vars are on, it's possible there is code like }
  359. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  360. { so we can't safely replace the movzx then with xor/mov, }
  361. { since that would change the flags (JM) }
  362. if PostPeepholeOptMovzx(p) then
  363. Result := True
  364. else if not(cs_opt_regvar in current_settings.optimizerswitches) then
  365. begin
  366. if (taicpu(p).oper[1]^.typ = top_reg) then
  367. if (taicpu(p).oper[0]^.typ = top_reg)
  368. then
  369. case taicpu(p).opsize of
  370. S_BL:
  371. begin
  372. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  373. not(cs_opt_size in current_settings.optimizerswitches) and
  374. (current_settings.optimizecputype = cpu_Pentium) then
  375. {Change "movzbl %reg1, %reg2" to
  376. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  377. PentiumMMX}
  378. begin
  379. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  380. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  381. InsertLLItem(p.previous, p, hp1);
  382. taicpu(p).opcode := A_MOV;
  383. taicpu(p).changeopsize(S_B);
  384. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  385. Result := True;
  386. end;
  387. end;
  388. else
  389. ;
  390. end
  391. else if (taicpu(p).oper[0]^.typ = top_ref) and
  392. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  393. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  394. not(cs_opt_size in current_settings.optimizerswitches) and
  395. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  396. (current_settings.optimizecputype = cpu_Pentium) and
  397. (taicpu(p).opsize = S_BL) then
  398. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  399. Pentium and PentiumMMX}
  400. begin
  401. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  402. taicpu(p).oper[1]^.reg);
  403. taicpu(p).opcode := A_MOV;
  404. taicpu(p).changeopsize(S_B);
  405. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  406. InsertLLItem(p.previous, p, hp1);
  407. Result := True;
  408. end;
  409. end;
  410. A_TEST, A_OR:
  411. Result:=PostPeepholeOptTestOr(p);
  412. A_AND:
  413. Result:=PostPeepholeOptAnd(p);
  414. A_MOVSX:
  415. Result:=PostPeepholeOptMOVSX(p);
  416. A_SHR:
  417. Result:=PostPeepholeOptShr(p);
  418. A_ADD,
  419. A_SUB:
  420. Result:=PostPeepholeOptADDSUB(p);
  421. A_XOR:
  422. Result:=PostPeepholeOptXor(p);
  423. A_RET:
  424. Result:=PostPeepholeOptRET(p);
  425. A_VPXOR:
  426. Result:=PostPeepholeOptVPXOR(p);
  427. A_SARX,
  428. A_SHLX,
  429. A_SHRX:
  430. Result:=PostPeepholeOptSARXSHLXSHRX(p);
  431. A_RORX:
  432. Result:=PostPeepholeOptRORX(p);
  433. else
  434. ;
  435. end;
  436. { Optimise any reference-type operands (if Result is True, the
  437. instruction will be checked on the next iteration) }
  438. if not Result then
  439. OptimizeRefs(taicpu(p));
  440. end;
  441. else
  442. ;
  443. end;
  444. { If this flag is set, something was optimised ahead of p, so move
  445. ahead by 1 instruction but treat as if Result was set to True }
  446. if aoc_ForceNewIteration in OptsToCheck then
  447. begin
  448. Exclude(OptsToCheck, aoc_ForceNewIteration);
  449. if not Result then
  450. begin
  451. if (p.typ in SkipInstr) then
  452. UpdateUsedRegs(p);
  453. p := tai(p.Next);
  454. Result := True;
  455. end;
  456. end;
  457. end;
  458. begin
  459. casmoptimizer:=TCpuAsmOptimizer;
  460. end.