aoptcpu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_XOR:
  84. begin
  85. if (taicpu(p).oper[0]^.typ = top_reg) and
  86. (taicpu(p).oper[1]^.typ = top_reg) and
  87. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  88. { temporarily change this to 'mov reg,0' to make it easier }
  89. { for the CSE. Will be changed back in pass 2 }
  90. begin
  91. taicpu(p).opcode := A_MOV;
  92. taicpu(p).loadConst(0,0);
  93. Result:=true;
  94. end;
  95. end;
  96. else
  97. { Do nothing };
  98. end;
  99. end;
  100. else
  101. { Do nothing };
  102. end;
  103. Break;
  104. until False;
  105. end;
  106. function TCPUAsmOPtimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  107. var
  108. hp1 : tai;
  109. begin
  110. result:=False;
  111. case p.Typ Of
  112. ait_instruction:
  113. begin
  114. current_filepos:=taicpu(p).fileinfo;
  115. if InsContainsSegRef(taicpu(p)) then
  116. exit;
  117. case taicpu(p).opcode Of
  118. A_AND:
  119. Result:=OptPass1And(p);
  120. A_IMUL:
  121. Result:=OptPass1Imul(p);
  122. A_CMP:
  123. Result:=OptPass1Cmp(p);
  124. A_VPXORD,
  125. A_VPXORQ,
  126. A_VXORPS,
  127. A_VXORPD,
  128. A_VPXOR:
  129. Result:=OptPass1VPXor(p);
  130. A_XORPS,
  131. A_XORPD,
  132. A_PXOR:
  133. Result:=OptPass1PXor(p);
  134. A_FLD:
  135. Result:=OptPass1FLD(p);
  136. A_FSTP,A_FISTP:
  137. Result:=OptPass1FSTP(p);
  138. A_LEA:
  139. Result:=OptPass1LEA(p);
  140. A_MOV:
  141. Result:=OptPass1MOV(p);
  142. A_MOVSX,
  143. A_MOVZX :
  144. Result:=OptPass1Movx(p);
  145. A_PUSH:
  146. begin
  147. if (taicpu(p).opsize = S_W) and
  148. (taicpu(p).oper[0]^.typ = Top_Const) and
  149. GetNextInstruction(p, hp1) and
  150. (tai(hp1).typ = ait_instruction) and
  151. (taicpu(hp1).opcode = A_PUSH) and
  152. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  153. (taicpu(hp1).opsize = S_W) then
  154. begin
  155. taicpu(p).changeopsize(S_L);
  156. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  157. asml.remove(hp1);
  158. hp1.free;
  159. Result:=true;
  160. end;
  161. end;
  162. A_SHL, A_SAL:
  163. Result:=OptPass1SHLSAL(p);
  164. A_SUB:
  165. Result:=OptPass1Sub(p);
  166. A_MOVAPD,
  167. A_MOVAPS,
  168. A_MOVUPD,
  169. A_MOVUPS,
  170. A_VMOVAPS,
  171. A_VMOVAPD,
  172. A_VMOVUPS,
  173. A_VMOVUPD:
  174. Result:=OptPass1_V_MOVAP(p);
  175. A_VDIVSD,
  176. A_VDIVSS,
  177. A_VSUBSD,
  178. A_VSUBSS,
  179. A_VMULSD,
  180. A_VMULSS,
  181. A_VADDSD,
  182. A_VADDSS,
  183. A_VANDPD,
  184. A_VANDPS,
  185. A_VORPD,
  186. A_VORPS:
  187. Result:=OptPass1VOP(p);
  188. A_MULSD,
  189. A_MULSS,
  190. A_ADDSD,
  191. A_ADDSS:
  192. Result:=OptPass1OP(p);
  193. A_VMOVSD,
  194. A_VMOVSS,
  195. A_MOVSD,
  196. A_MOVSS:
  197. Result:=OptPass1MOVXX(p);
  198. A_SETcc:
  199. Result:=OptPass1SETcc(p);
  200. else
  201. ;
  202. end;
  203. end;
  204. else
  205. ;
  206. end;
  207. end;
  208. function TCPUAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
  209. begin
  210. Result:=false;
  211. case p.Typ Of
  212. Ait_Instruction:
  213. begin
  214. if InsContainsSegRef(taicpu(p)) then
  215. exit;
  216. case taicpu(p).opcode Of
  217. A_Jcc:
  218. Result:=OptPass2Jcc(p);
  219. A_Lea:
  220. Result:=OptPass2Lea(p);
  221. A_FSTP,A_FISTP:
  222. Result:=OptPass1FSTP(p);
  223. A_IMUL:
  224. Result:=OptPass2Imul(p);
  225. A_JMP:
  226. Result:=OptPass2Jmp(p);
  227. A_MOV:
  228. Result:=OptPass2MOV(p);
  229. A_SUB:
  230. Result:=OptPass2SUB(p);
  231. else
  232. ;
  233. end;
  234. end;
  235. else
  236. ;
  237. end;
  238. end;
  239. function TCPUAsmOptimizer.PostPeepHoleOptsCpu(var p : tai) : boolean;
  240. var
  241. hp1: tai;
  242. begin
  243. Result:=false;
  244. case p.Typ Of
  245. Ait_Instruction:
  246. begin
  247. if InsContainsSegRef(taicpu(p)) then
  248. Exit;
  249. case taicpu(p).opcode Of
  250. A_CALL:
  251. Result:=PostPeepHoleOptCall(p);
  252. A_LEA:
  253. Result:=PostPeepholeOptLea(p);
  254. A_CMP:
  255. Result:=PostPeepholeOptCmp(p);
  256. A_MOV:
  257. Result:=PostPeepholeOptMov(p);
  258. A_MOVZX:
  259. { if register vars are on, it's possible there is code like }
  260. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  261. { so we can't safely replace the movzx then with xor/mov, }
  262. { since that would change the flags (JM) }
  263. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  264. begin
  265. if (taicpu(p).oper[1]^.typ = top_reg) then
  266. if (taicpu(p).oper[0]^.typ = top_reg)
  267. then
  268. case taicpu(p).opsize of
  269. S_BL:
  270. begin
  271. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  272. not(cs_opt_size in current_settings.optimizerswitches) and
  273. (current_settings.optimizecputype = cpu_Pentium) then
  274. {Change "movzbl %reg1, %reg2" to
  275. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  276. PentiumMMX}
  277. begin
  278. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  279. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  280. InsertLLItem(p.previous, p, hp1);
  281. taicpu(p).opcode := A_MOV;
  282. taicpu(p).changeopsize(S_B);
  283. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  284. Result := True;
  285. end;
  286. end;
  287. else
  288. ;
  289. end
  290. else if (taicpu(p).oper[0]^.typ = top_ref) and
  291. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  292. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  293. not(cs_opt_size in current_settings.optimizerswitches) and
  294. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  295. (current_settings.optimizecputype = cpu_Pentium) and
  296. (taicpu(p).opsize = S_BL) then
  297. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  298. Pentium and PentiumMMX}
  299. begin
  300. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  301. taicpu(p).oper[1]^.reg);
  302. taicpu(p).opcode := A_MOV;
  303. taicpu(p).changeopsize(S_B);
  304. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  305. InsertLLItem(p.previous, p, hp1);
  306. Result := True;
  307. end;
  308. end;
  309. A_TEST, A_OR:
  310. Result:=PostPeepholeOptTestOr(p);
  311. A_MOVSX:
  312. Result:=PostPeepholeOptMOVSX(p);
  313. else
  314. ;
  315. end;
  316. { Optimise any reference-type operands (if Result is True, the
  317. instruction will be checked on the next iteration) }
  318. if not Result then
  319. OptimizeRefs(taicpu(p));
  320. end;
  321. else
  322. ;
  323. end;
  324. end;
  325. begin
  326. casmoptimizer:=TCpuAsmOptimizer;
  327. end.