aoptcpu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. A_PUSH:
  141. begin
  142. if (taicpu(p).opsize = S_W) and
  143. (taicpu(p).oper[0]^.typ = Top_Const) and
  144. GetNextInstruction(p, hp1) and
  145. (tai(hp1).typ = ait_instruction) and
  146. (taicpu(hp1).opcode = A_PUSH) and
  147. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  148. (taicpu(hp1).opsize = S_W) then
  149. begin
  150. taicpu(p).changeopsize(S_L);
  151. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  152. asml.remove(hp1);
  153. hp1.free;
  154. Result:=true;
  155. end;
  156. end;
  157. A_SHL, A_SAL:
  158. Result:=OptPass1SHLSAL(p);
  159. A_SUB:
  160. Result:=OptPass1Sub(p);
  161. A_MOVAPD,
  162. A_MOVAPS,
  163. A_MOVUPD,
  164. A_MOVUPS,
  165. A_VMOVAPS,
  166. A_VMOVAPD,
  167. A_VMOVUPS,
  168. A_VMOVUPD:
  169. Result:=OptPass1_V_MOVAP(p);
  170. A_VDIVSD,
  171. A_VDIVSS,
  172. A_VSUBSD,
  173. A_VSUBSS,
  174. A_VMULSD,
  175. A_VMULSS,
  176. A_VADDSD,
  177. A_VADDSS,
  178. A_VANDPD,
  179. A_VANDPS,
  180. A_VORPD,
  181. A_VORPS,
  182. A_VXORPD,
  183. A_VXORPS:
  184. Result:=OptPass1VOP(p);
  185. A_MULSD,
  186. A_MULSS,
  187. A_ADDSD,
  188. A_ADDSS:
  189. Result:=OptPass1OP(p);
  190. A_VMOVSD,
  191. A_VMOVSS,
  192. A_MOVSD,
  193. A_MOVSS:
  194. Result:=OptPass1MOVXX(p);
  195. A_SETcc:
  196. Result:=OptPass1SETcc(p);
  197. else
  198. ;
  199. end;
  200. end;
  201. else
  202. ;
  203. end;
  204. end;
  205. function TCPUAsmOptimizer.PeepHoleOptPass2Cpu(var p: tai): boolean;
  206. begin
  207. Result:=false;
  208. case p.Typ Of
  209. Ait_Instruction:
  210. begin
  211. if InsContainsSegRef(taicpu(p)) then
  212. exit;
  213. case taicpu(p).opcode Of
  214. A_Jcc:
  215. Result:=OptPass2Jcc(p);
  216. A_Lea:
  217. Result:=OptPass2Lea(p);
  218. A_FSTP,A_FISTP:
  219. Result:=OptPass1FSTP(p);
  220. A_IMUL:
  221. Result:=OptPass2Imul(p);
  222. A_JMP:
  223. Result:=OptPass2Jmp(p);
  224. A_MOV:
  225. Result:=OptPass2MOV(p);
  226. else
  227. ;
  228. end;
  229. end;
  230. else
  231. ;
  232. end;
  233. end;
  234. function TCPUAsmOptimizer.PostPeepHoleOptsCpu(var p : tai) : boolean;
  235. var
  236. hp1: tai;
  237. begin
  238. Result:=false;
  239. case p.Typ Of
  240. Ait_Instruction:
  241. begin
  242. if InsContainsSegRef(taicpu(p)) then
  243. Exit;
  244. case taicpu(p).opcode Of
  245. A_CALL:
  246. Result:=PostPeepHoleOptCall(p);
  247. A_LEA:
  248. Result:=PostPeepholeOptLea(p);
  249. A_CMP:
  250. Result:=PostPeepholeOptCmp(p);
  251. A_MOV:
  252. Result:=PostPeepholeOptMov(p);
  253. A_MOVZX:
  254. { if register vars are on, it's possible there is code like }
  255. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  256. { so we can't safely replace the movzx then with xor/mov, }
  257. { since that would change the flags (JM) }
  258. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  259. begin
  260. if (taicpu(p).oper[1]^.typ = top_reg) then
  261. if (taicpu(p).oper[0]^.typ = top_reg)
  262. then
  263. case taicpu(p).opsize of
  264. S_BL:
  265. begin
  266. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  267. not(cs_opt_size in current_settings.optimizerswitches) and
  268. (current_settings.optimizecputype = cpu_Pentium) then
  269. {Change "movzbl %reg1, %reg2" to
  270. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  271. PentiumMMX}
  272. begin
  273. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  274. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  275. InsertLLItem(p.previous, p, hp1);
  276. taicpu(p).opcode := A_MOV;
  277. taicpu(p).changeopsize(S_B);
  278. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  279. end;
  280. end;
  281. else
  282. ;
  283. end
  284. else if (taicpu(p).oper[0]^.typ = top_ref) and
  285. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  286. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  287. not(cs_opt_size in current_settings.optimizerswitches) and
  288. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  289. (current_settings.optimizecputype = cpu_Pentium) and
  290. (taicpu(p).opsize = S_BL) then
  291. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  292. Pentium and PentiumMMX}
  293. begin
  294. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  295. taicpu(p).oper[1]^.reg);
  296. taicpu(p).opcode := A_MOV;
  297. taicpu(p).changeopsize(S_B);
  298. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  299. InsertLLItem(p.previous, p, hp1);
  300. end;
  301. end;
  302. A_TEST, A_OR:
  303. Result:=PostPeepholeOptTestOr(p);
  304. else
  305. ;
  306. end;
  307. end;
  308. else
  309. ;
  310. end;
  311. end;
  312. procedure TCpuAsmOptimizer.PostPeepHoleOpts;
  313. begin
  314. inherited;
  315. OptReferences;
  316. end;
  317. begin
  318. casmoptimizer:=TCpuAsmOptimizer;
  319. end.