aoptcpu.pas 12 KB

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