aoptcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. procedure PostPeepHoleOpts; override;
  32. end;
  33. Var
  34. AsmOptimizer : TCpuAsmOptimizer;
  35. Implementation
  36. uses
  37. verbose,globtype,globals,
  38. cpuinfo,
  39. aasmcpu,
  40. aoptutils,
  41. aasmcfi,
  42. procinfo,
  43. cgutils,
  44. { units we should get rid off: }
  45. symsym,symconst;
  46. { Checks if the register is a 32 bit general purpose register }
  47. function isgp32reg(reg: TRegister): boolean;
  48. begin
  49. {$push}{$warnings off}
  50. isgp32reg:=(getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)>=RS_EAX) and (getsupreg(reg)<=RS_EBX);
  51. {$pop}
  52. end;
  53. { returns true if p contains a memory operand with a segment set }
  54. function InsContainsSegRef(p: taicpu): boolean;
  55. var
  56. i: longint;
  57. begin
  58. result:=true;
  59. for i:=0 to p.opercnt-1 do
  60. if (p.oper[i]^.typ=top_ref) and
  61. (p.oper[i]^.ref^.segment<>NR_NO) then
  62. exit;
  63. result:=false;
  64. end;
  65. function TCPUAsmOPtimizer.PrePeepHoleOptsCpu(var p: tai): boolean;
  66. begin
  67. repeat
  68. Result:=False;
  69. case p.typ of
  70. ait_instruction:
  71. begin
  72. if InsContainsSegRef(taicpu(p)) then
  73. begin
  74. p := tai(p.next);
  75. { Nothing's actually changed, so no need to set Result to True,
  76. but try again to see if an instruction immediately follows }
  77. Continue;
  78. end;
  79. case taicpu(p).opcode Of
  80. A_IMUL:
  81. Result:=PrePeepholeOptIMUL(p);
  82. A_SAR,A_SHR:
  83. Result:=PrePeepholeOptSxx(p);
  84. A_XOR:
  85. begin
  86. if (taicpu(p).oper[0]^.typ = top_reg) and
  87. (taicpu(p).oper[1]^.typ = top_reg) and
  88. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  89. { temporarily change this to 'mov reg,0' to make it easier }
  90. { for the CSE. Will be changed back in pass 2 }
  91. begin
  92. taicpu(p).opcode := A_MOV;
  93. taicpu(p).loadConst(0,0);
  94. Result:=true;
  95. end;
  96. end;
  97. else
  98. { Do nothing };
  99. end;
  100. end;
  101. else
  102. { Do nothing };
  103. end;
  104. Break;
  105. until False;
  106. end;
  107. function TCPUAsmOPtimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  108. var
  109. hp1,hp2 : tai;
  110. hp3,hp4: tai;
  111. v:aint;
  112. begin
  113. result:=False;
  114. case p.Typ Of
  115. ait_instruction:
  116. begin
  117. current_filepos:=taicpu(p).fileinfo;
  118. if InsContainsSegRef(taicpu(p)) then
  119. begin
  120. p:=tai(p.next);
  121. Result:=true;
  122. exit;
  123. end;
  124. case taicpu(p).opcode Of
  125. A_AND:
  126. Result:=OptPass1And(p);
  127. A_CMP:
  128. Result:=OptPass1Cmp(p);
  129. A_FLD:
  130. Result:=OptPass1FLD(p);
  131. A_FSTP,A_FISTP:
  132. Result:=OptPass1FSTP(p);
  133. A_LEA:
  134. Result:=OptPass1LEA(p);
  135. A_MOV:
  136. Result:=OptPass1MOV(p);
  137. A_MOVSX,
  138. A_MOVZX :
  139. Result:=OptPass1Movx(p);
  140. (* should not be generated anymore by the current code generator
  141. A_POP:
  142. begin
  143. if target_info.system=system_i386_go32v2 then
  144. begin
  145. { Transform a series of pop/pop/pop/push/push/push to }
  146. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  147. { because I'm not sure whether they can cope with }
  148. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  149. { such a problem when using esp as frame pointer (JM) }
  150. if (taicpu(p).oper[0]^.typ = top_reg) then
  151. begin
  152. hp1 := p;
  153. hp2 := p;
  154. l := 0;
  155. while getNextInstruction(hp1,hp1) and
  156. (hp1.typ = ait_instruction) and
  157. (taicpu(hp1).opcode = A_POP) and
  158. (taicpu(hp1).oper[0]^.typ = top_reg) do
  159. begin
  160. hp2 := hp1;
  161. inc(l,4);
  162. end;
  163. getLastInstruction(p,hp3);
  164. l1 := 0;
  165. while (hp2 <> hp3) and
  166. assigned(hp1) and
  167. (hp1.typ = ait_instruction) and
  168. (taicpu(hp1).opcode = A_PUSH) and
  169. (taicpu(hp1).oper[0]^.typ = top_reg) and
  170. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  171. begin
  172. { change it to a two op operation }
  173. taicpu(hp2).oper[1]^.typ:=top_none;
  174. taicpu(hp2).ops:=2;
  175. taicpu(hp2).opcode := A_MOV;
  176. taicpu(hp2).loadoper(1,taicpu(hp1).oper[0]^);
  177. reference_reset(tmpref);
  178. tmpRef.base.enum:=R_INTREGISTER;
  179. tmpRef.base.number:=NR_STACK_POINTER_REG;
  180. convert_register_to_enum(tmpref.base);
  181. tmpRef.offset := l;
  182. taicpu(hp2).loadRef(0,tmpRef);
  183. hp4 := hp1;
  184. getNextInstruction(hp1,hp1);
  185. asml.remove(hp4);
  186. hp4.free;
  187. getLastInstruction(hp2,hp2);
  188. dec(l,4);
  189. inc(l1);
  190. end;
  191. if l <> -4 then
  192. begin
  193. inc(l,4);
  194. for l1 := l1 downto 1 do
  195. begin
  196. getNextInstruction(hp2,hp2);
  197. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  198. end
  199. end
  200. end
  201. end
  202. else
  203. begin
  204. if (taicpu(p).oper[0]^.typ = top_reg) and
  205. GetNextInstruction(p, hp1) and
  206. (tai(hp1).typ=ait_instruction) and
  207. (taicpu(hp1).opcode=A_PUSH) and
  208. (taicpu(hp1).oper[0]^.typ = top_reg) and
  209. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  210. begin
  211. { change it to a two op operation }
  212. taicpu(p).oper[1]^.typ:=top_none;
  213. taicpu(p).ops:=2;
  214. taicpu(p).opcode := A_MOV;
  215. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  216. reference_reset(tmpref);
  217. TmpRef.base.enum := R_ESP;
  218. taicpu(p).loadRef(0,TmpRef);
  219. asml.remove(hp1);
  220. hp1.free;
  221. end;
  222. end;
  223. end;
  224. *)
  225. A_PUSH:
  226. begin
  227. if (taicpu(p).opsize = S_W) and
  228. (taicpu(p).oper[0]^.typ = Top_Const) and
  229. GetNextInstruction(p, hp1) and
  230. (tai(hp1).typ = ait_instruction) and
  231. (taicpu(hp1).opcode = A_PUSH) and
  232. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  233. (taicpu(hp1).opsize = S_W) then
  234. begin
  235. taicpu(p).changeopsize(S_L);
  236. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  237. asml.remove(hp1);
  238. hp1.free;
  239. Result:=true;
  240. end;
  241. end;
  242. A_SHL, A_SAL:
  243. Result:=OptPass1SHLSAL(p);
  244. A_SUB:
  245. Result:=OptPass1Sub(p);
  246. A_MOVAPD,
  247. A_MOVAPS,
  248. A_MOVUPD,
  249. A_MOVUPS,
  250. A_VMOVAPS,
  251. A_VMOVAPD,
  252. A_VMOVUPS,
  253. A_VMOVUPD:
  254. Result:=OptPass1_V_MOVAP(p);
  255. A_VDIVSD,
  256. A_VDIVSS,
  257. A_VSUBSD,
  258. A_VSUBSS,
  259. A_VMULSD,
  260. A_VMULSS,
  261. A_VADDSD,
  262. A_VADDSS,
  263. A_VANDPD,
  264. A_VANDPS,
  265. A_VORPD,
  266. A_VORPS,
  267. A_VXORPD,
  268. A_VXORPS:
  269. Result:=OptPass1VOP(p);
  270. A_MULSD,
  271. A_MULSS,
  272. A_ADDSD,
  273. A_ADDSS:
  274. Result:=OptPass1OP(p);
  275. A_VMOVSD,
  276. A_VMOVSS,
  277. A_MOVSD,
  278. A_MOVSS:
  279. Result:=OptPass1MOVXX(p);
  280. A_SETcc:
  281. Result:=OptPass1SETcc(p);
  282. else
  283. ;
  284. end;
  285. end;
  286. else
  287. ;
  288. end;
  289. end;
  290. function TCPUAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
  291. begin
  292. Result:=false;
  293. case p.Typ Of
  294. Ait_Instruction:
  295. begin
  296. if InsContainsSegRef(taicpu(p)) then
  297. exit;
  298. case taicpu(p).opcode Of
  299. A_Jcc:
  300. Result:=OptPass2Jcc(p);
  301. A_Lea:
  302. Result:=OptPass2Lea(p);
  303. A_FSTP,A_FISTP:
  304. Result:=OptPass1FSTP(p);
  305. A_IMUL:
  306. Result:=OptPass2Imul(p);
  307. A_JMP:
  308. Result:=OptPass2Jmp(p);
  309. A_MOV:
  310. Result:=OptPass2MOV(p);
  311. else
  312. ;
  313. end;
  314. end;
  315. else
  316. ;
  317. end;
  318. end;
  319. function TCPUAsmOptimizer.PostPeepHoleOptsCpu(var p : tai) : boolean;
  320. var
  321. hp1: tai;
  322. begin
  323. Result:=false;
  324. case p.Typ Of
  325. Ait_Instruction:
  326. begin
  327. if InsContainsSegRef(taicpu(p)) then
  328. Exit;
  329. case taicpu(p).opcode Of
  330. A_CALL:
  331. Result:=PostPeepHoleOptCall(p);
  332. A_LEA:
  333. Result:=PostPeepholeOptLea(p);
  334. A_CMP:
  335. Result:=PostPeepholeOptCmp(p);
  336. A_MOV:
  337. Result:=PostPeepholeOptMov(p);
  338. A_MOVZX:
  339. { if register vars are on, it's possible there is code like }
  340. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  341. { so we can't safely replace the movzx then with xor/mov, }
  342. { since that would change the flags (JM) }
  343. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  344. begin
  345. if (taicpu(p).oper[1]^.typ = top_reg) then
  346. if (taicpu(p).oper[0]^.typ = top_reg)
  347. then
  348. case taicpu(p).opsize of
  349. S_BL:
  350. begin
  351. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  352. not(cs_opt_size in current_settings.optimizerswitches) and
  353. (current_settings.optimizecputype = cpu_Pentium) then
  354. {Change "movzbl %reg1, %reg2" to
  355. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  356. PentiumMMX}
  357. begin
  358. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  359. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  360. InsertLLItem(p.previous, p, hp1);
  361. taicpu(p).opcode := A_MOV;
  362. taicpu(p).changeopsize(S_B);
  363. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  364. end;
  365. end;
  366. else
  367. ;
  368. end
  369. else if (taicpu(p).oper[0]^.typ = top_ref) and
  370. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  371. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  372. not(cs_opt_size in current_settings.optimizerswitches) and
  373. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  374. (current_settings.optimizecputype = cpu_Pentium) and
  375. (taicpu(p).opsize = S_BL) then
  376. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  377. Pentium and PentiumMMX}
  378. begin
  379. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  380. taicpu(p).oper[1]^.reg);
  381. taicpu(p).opcode := A_MOV;
  382. taicpu(p).changeopsize(S_B);
  383. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  384. InsertLLItem(p.previous, p, hp1);
  385. end;
  386. end;
  387. A_TEST, A_OR:
  388. Result:=PostPeepholeOptTestOr(p);
  389. else
  390. ;
  391. end;
  392. end;
  393. else
  394. ;
  395. end;
  396. end;
  397. procedure TCpuAsmOptimizer.PostPeepHoleOpts;
  398. begin
  399. inherited;
  400. OptReferences;
  401. end;
  402. begin
  403. casmoptimizer:=TCpuAsmOptimizer;
  404. end.