aoptcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe
  3. This unit calls the optimization procedures to optimize the assembler
  4. code for sparc
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses cpubase, aasmtai, aopt, aoptcpub;
  22. type
  23. TCpuAsmOptimizer = class(TAsmOptimizer)
  24. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  25. end;
  26. implementation
  27. uses
  28. globtype, globals,
  29. cutils,
  30. verbose,
  31. cgbase, cgutils,
  32. aasmbase, aasmdata, aasmcpu;
  33. function isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  34. begin
  35. isFoldableArithOp := False;
  36. case hp1.opcode of
  37. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR, A_SAR:
  38. isFoldableArithOp :=
  39. ((taicpu(hp1).oper[0]^.typ = top_const) or
  40. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  41. (taicpu(hp1).oper[0]^.reg<>reg))) and
  42. (taicpu(hp1).oper[1]^.typ = top_reg) and
  43. (taicpu(hp1).oper[1]^.reg = reg);
  44. A_INC, A_DEC:
  45. isFoldableArithOp :=
  46. (taicpu(hp1).oper[0]^.typ = top_reg) and
  47. (taicpu(hp1).oper[0]^.reg = reg);
  48. end;
  49. end;
  50. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  51. var
  52. next1: tai;
  53. hp1, hp2: tai;
  54. begin
  55. Result := False;
  56. case p.typ of
  57. ait_instruction:
  58. begin
  59. case taicpu(p).opcode of
  60. A_AND:
  61. begin
  62. if (taicpu(p).oper[0]^.typ = top_const) and
  63. (taicpu(p).oper[1]^.typ = top_reg) and
  64. GetNextInstruction(p, hp1) and
  65. (tai(hp1).typ = ait_instruction) and
  66. (taicpu(hp1).opcode = A_AND) and
  67. (taicpu(hp1).oper[0]^.typ = top_const) and
  68. (taicpu(hp1).oper[1]^.typ = top_reg) and
  69. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(
  70. taicpu(hp1).oper[1]^.reg)) and
  71. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(
  72. taicpu(hp1).oper[1]^.reg)) and
  73. (abs(taicpu(p).oper[0]^.val and
  74. taicpu(hp1).oper[0]^.val)<$80000000) then
  75. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  76. begin
  77. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and
  78. taicpu(hp1).oper[0]^.val);
  79. asml.remove(p);
  80. p.Free;
  81. p := hp1;
  82. end;
  83. (* else
  84. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  85. jump, but only if it's a conditional jump (PFV) }
  86. if (taicpu(p).oper[1]^.typ = top_reg) and
  87. GetNextInstruction(p, hp1) and
  88. (hp1.typ = ait_instruction) and
  89. (taicpu(hp1).is_jmp) and
  90. (taicpu(hp1).opcode<>A_JMP) and
  91. not(getsupreg(taicpu(p).oper[1]^.reg) in UsedRegs) then
  92. taicpu(p).opcode := A_TEST;*)
  93. end;
  94. A_MOVSX,
  95. A_MOVZX:
  96. begin
  97. if (taicpu(p).oper[1]^.typ = top_reg) and
  98. GetNextInstruction(p, hp1) and
  99. (hp1.typ = ait_instruction) and
  100. IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg) and
  101. GetNextInstruction(hp1, hp2) and
  102. (hp2.typ = ait_instruction) and
  103. (taicpu(hp2).opcode = A_MOV) and
  104. (taicpu(hp2).oper[0]^.typ = top_reg) and
  105. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) then
  106. { change movsX/movzX reg/ref, reg2 }
  107. { add/sub/or/... reg3/$const, reg2 }
  108. { mov reg2 reg/ref }
  109. { to add/sub/or/... reg3/$const, reg/ref }
  110. begin
  111. { by example:
  112. movswl %si,%eax movswl %si,%eax p
  113. decl %eax addl %edx,%eax hp1
  114. movw %ax,%si movw %ax,%si hp2
  115. ->
  116. movswl %si,%eax movswl %si,%eax p
  117. decw %eax addw %edx,%eax hp1
  118. movw %ax,%si movw %ax,%si hp2
  119. }
  120. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  121. {
  122. ->
  123. movswl %si,%eax movswl %si,%eax p
  124. decw %si addw %dx,%si hp1
  125. movw %ax,%si movw %ax,%si hp2
  126. }
  127. case taicpu(hp1).ops of
  128. 1:
  129. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  130. 2:
  131. begin
  132. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  133. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  134. setsubreg(taicpu(hp1).oper[0]^.reg,
  135. getsubreg(taicpu(hp2).oper[0]^.reg));
  136. end;
  137. else
  138. internalerror(2008042701);
  139. end;
  140. {
  141. ->
  142. decw %si addw %dx,%si p
  143. }
  144. asml.remove(p);
  145. asml.remove(hp2);
  146. p.Free;
  147. hp2.Free;
  148. p := hp1;
  149. end
  150. { removes superfluous And's after movzx's }
  151. else if taicpu(p).opcode = A_MOVZX then
  152. begin
  153. if (taicpu(p).oper[1]^.typ = top_reg) and
  154. GetNextInstruction(p, hp1) and
  155. (tai(hp1).typ = ait_instruction) and
  156. (taicpu(hp1).opcode = A_AND) and
  157. (taicpu(hp1).oper[0]^.typ = top_const) and
  158. (taicpu(hp1).oper[1]^.typ = top_reg) and
  159. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  160. case taicpu(p).opsize of
  161. S_BL, S_BW, S_BQ:
  162. if (taicpu(hp1).oper[0]^.val = $ff) then
  163. begin
  164. asml.remove(hp1);
  165. hp1.Free;
  166. end;
  167. S_WL, S_WQ:
  168. if (taicpu(hp1).oper[0]^.val = $ffff) then
  169. begin
  170. asml.remove(hp1);
  171. hp1.Free;
  172. end;
  173. S_LQ:
  174. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  175. begin
  176. asml.remove(hp1);
  177. hp1.Free;
  178. end;
  179. end;
  180. { changes some movzx constructs to faster synonims (all examples
  181. are given with eax/ax, but are also valid for other registers)}
  182. if (taicpu(p).oper[1]^.typ = top_reg) then
  183. if (taicpu(p).oper[0]^.typ = top_reg) then
  184. case taicpu(p).opsize of
  185. S_BW:
  186. begin
  187. if (getsupreg(taicpu(p).oper[0]^.reg) =
  188. getsupreg(taicpu(p).oper[1]^.reg)) and not
  189. (cs_opt_size in current_settings.optimizerswitches) then
  190. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  191. begin
  192. taicpu(p).opcode := A_AND;
  193. taicpu(p).changeopsize(S_W);
  194. taicpu(p).loadConst(0, $ff);
  195. end
  196. else if GetNextInstruction(p, hp1) and
  197. (tai(hp1).typ = ait_instruction) and
  198. (taicpu(hp1).opcode = A_AND) and
  199. (taicpu(hp1).oper[0]^.typ = top_const) and
  200. (taicpu(hp1).oper[1]^.typ = top_reg) and
  201. (taicpu(hp1).oper[1]^.reg =
  202. taicpu(p).oper[1]^.reg) then
  203. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  204. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  205. begin
  206. taicpu(p).opcode := A_MOV;
  207. taicpu(p).changeopsize(S_W);
  208. setsubreg(taicpu(p).oper[0]^.reg, R_SUBW);
  209. taicpu(hp1).loadConst(
  210. 0, taicpu(hp1).oper[0]^.val and $ff);
  211. end;
  212. end;
  213. S_BL:
  214. begin
  215. if (getsupreg(taicpu(p).oper[0]^.reg) =
  216. getsupreg(taicpu(p).oper[1]^.reg)) and not
  217. (cs_opt_size in current_settings.optimizerswitches) then
  218. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  219. begin
  220. taicpu(p).opcode := A_AND;
  221. taicpu(p).changeopsize(S_L);
  222. taicpu(p).loadConst(0, $ff);
  223. end
  224. else if GetNextInstruction(p, hp1) and
  225. (tai(hp1).typ = ait_instruction) and
  226. (taicpu(hp1).opcode = A_AND) and
  227. (taicpu(hp1).oper[0]^.typ = top_const) and
  228. (taicpu(hp1).oper[1]^.typ = top_reg) and
  229. (taicpu(hp1).oper[1]^.reg =
  230. taicpu(p).oper[1]^.reg) then
  231. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  232. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  233. begin
  234. taicpu(p).opcode := A_MOV;
  235. taicpu(p).changeopsize(S_L);
  236. setsubreg(taicpu(p).oper[0]^.reg, R_SUBWHOLE);
  237. taicpu(hp1).loadConst(
  238. 0, taicpu(hp1).oper[0]^.val and $ff);
  239. end;
  240. end;
  241. S_WL:
  242. begin
  243. if (getsupreg(taicpu(p).oper[0]^.reg) =
  244. getsupreg(taicpu(p).oper[1]^.reg)) and not
  245. (cs_opt_size in current_settings.optimizerswitches) then
  246. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  247. begin
  248. taicpu(p).opcode := A_AND;
  249. taicpu(p).changeopsize(S_L);
  250. taicpu(p).loadConst(0, $ffff);
  251. end
  252. else if GetNextInstruction(p, hp1) and
  253. (tai(hp1).typ = ait_instruction) and
  254. (taicpu(hp1).opcode = A_AND) and
  255. (taicpu(hp1).oper[0]^.typ = top_const) and
  256. (taicpu(hp1).oper[1]^.typ = top_reg) and
  257. (taicpu(hp1).oper[1]^.reg =
  258. taicpu(p).oper[1]^.reg) then
  259. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  260. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  261. begin
  262. taicpu(p).opcode := A_MOV;
  263. taicpu(p).changeopsize(S_L);
  264. setsubreg(taicpu(p).oper[0]^.reg, R_SUBWHOLE);
  265. taicpu(hp1).loadConst(
  266. 0, taicpu(hp1).oper[0]^.val and $ffff);
  267. end;
  268. end;
  269. end
  270. else if (taicpu(p).oper[0]^.typ = top_ref) then
  271. begin
  272. if GetNextInstruction(p, hp1) and
  273. (tai(hp1).typ = ait_instruction) and
  274. (taicpu(hp1).opcode = A_AND) and
  275. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  276. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  277. (taicpu(hp1).oper[1]^.reg =
  278. taicpu(p).oper[1]^.reg) then
  279. begin
  280. taicpu(p).opcode := A_MOV;
  281. case taicpu(p).opsize of
  282. S_BL:
  283. begin
  284. taicpu(p).changeopsize(S_L);
  285. taicpu(hp1).loadConst(
  286. 0, taicpu(hp1).oper[0]^.val and $ff);
  287. end;
  288. S_WL:
  289. begin
  290. taicpu(p).changeopsize(S_L);
  291. taicpu(hp1).loadConst(
  292. 0, taicpu(hp1).oper[0]^.val and $ffff);
  293. end;
  294. S_BW:
  295. begin
  296. taicpu(p).changeopsize(S_W);
  297. taicpu(hp1).loadConst(
  298. 0, taicpu(hp1).oper[0]^.val and $ff);
  299. end;
  300. S_BQ:
  301. begin
  302. taicpu(p).changeopsize(S_Q);
  303. taicpu(hp1).loadConst(
  304. 0, taicpu(hp1).oper[0]^.val and $ff);
  305. end;
  306. S_WQ:
  307. begin
  308. taicpu(p).changeopsize(S_Q);
  309. taicpu(hp1).loadConst(
  310. 0, taicpu(hp1).oper[0]^.val and $ffff);
  311. end;
  312. S_LQ:
  313. begin
  314. taicpu(p).changeopsize(S_Q);
  315. taicpu(hp1).loadConst(
  316. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  317. end;
  318. end;
  319. end;
  320. end;
  321. end;
  322. end;
  323. end;
  324. end;
  325. end;
  326. end;
  327. begin
  328. casmoptimizer := TCpuAsmOptimizer;
  329. end.