aoptcpu.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the PowerPC optimizer object
  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. Interface
  20. {$i fpcdefs.inc}
  21. uses cpubase, aoptobj, aoptcpub, aopt, aasmtai,aasmdata, aasmcpu;
  22. Type
  23. TCpuAsmOptimizer = class(TAsmOptimizer)
  24. { uses the same constructor as TAopObj }
  25. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  26. function PostPeepHoleOptsCpu(var p: tai): boolean; override;
  27. private
  28. function cmpi_mfcr_opt(p, next1, next2: taicpu): boolean;
  29. End;
  30. Implementation
  31. uses
  32. cutils, cgbase, cgcpu, cgobj;
  33. const
  34. calculation_target_op0: array[tasmop] of tasmop = (a_none,
  35. a_add, a_add_, a_addo, a_addo_, a_addc, a_addc_, a_addco, a_addco_,
  36. a_adde, a_adde_, a_addeo, a_addeo_, a_addi, a_addic, a_addic_, a_addis,
  37. a_addme, a_addme_, a_addmeo, a_addmeo_, a_addze, a_addze_, a_addzeo,
  38. a_addzeo_, a_and, a_and_, a_andc, a_andc_, a_andi_, a_andis_, a_none,
  39. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  40. a_none, a_none, a_none, a_none, a_none, a_cntlzw, a_cntlzw_, a_none,
  41. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  42. a_none, a_none, a_none, a_none, a_none, a_none, a_divw, a_divw_, a_divwo, a_divwo_,
  43. a_divwu, a_divwu_, a_divwuo, a_divwuo_, a_none, a_none, a_none, a_eqv,
  44. a_eqv_, a_extsb, a_extsb_, a_extsh, a_extsh_, a_fabs, a_fabs_, a_fadd,
  45. a_fadd_, a_fadds, a_fadds_, a_none, a_none,
  46. a_fctid, a_fctid_, a_fctidz, a_fctidz_,
  47. a_fctiw, a_fctiw_, a_fctiwz, a_fctiwz_,
  48. a_fdiv, a_fdiv_, a_fdivs, a_fdivs_, a_fmadd, a_fmadd_, a_fmadds,
  49. a_fmadds_, a_none, a_fmsub, a_fmsub_, a_fmsubs, a_fmsubs_, a_fmul, a_fmul_,
  50. a_fmuls, a_fmuls_, a_fnabs, a_fnabs_, a_fneg, a_fneg_, a_fnmadd,
  51. a_fnmadd_, a_fnmadds, a_fnmadds_, a_fnmsub, a_fnmsub_, a_fnmsubs,
  52. a_fnmsubs_, a_fres, a_fres_, a_frsp, a_frsp_, a_frsqrte, a_frsqrte_,
  53. a_none, a_none, a_fsqrt, a_fsqrt_, a_fsqrts, a_fsqrts_, a_fsub, a_fsub_,
  54. a_fsubs, a_fsubs_, a_none, a_none, a_none, a_none, a_none, a_none,
  55. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  56. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  57. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  58. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  59. a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  60. a_none, a_none, a_none, a_none, a_none, a_none, a_mulhw,
  61. a_mulhw_, a_mulhwu, a_mulhwu_, a_mulli, a_mullw, a_mullw_, a_mullwo,
  62. a_mullwo_, a_nand, a_nand_, a_neg, a_neg_, a_nego, a_nego_, a_nor, a_nor_,
  63. a_or, a_or_, a_orc, a_orc_, a_ori, a_oris, a_rfi, a_rlwimi, a_rlwimi_,
  64. a_rlwinm, a_rlwinm_, a_rlwnm, a_rlwnm_, a_none, a_slw, a_slw_, a_sraw, a_sraw_,
  65. a_srawi, a_srawi_,a_srw, a_srw_, a_none, a_none, a_none, a_none, a_none,
  66. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  67. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  68. a_none, a_none, a_none, a_none, a_none, a_subf, a_subf_, a_subfo,
  69. a_subfo_, a_subfc, a_subfc_, a_subfco, a_subfco_, a_subfe, a_subfe_,
  70. a_subfeo, a_subfeo_, a_subfic, a_subfme, a_subfme_, a_subfmeo, a_subfmeo_,
  71. a_subfze, a_subfze_, a_subfzeo, a_subfzeo_, a_none, a_none, a_none,
  72. a_none, a_none, a_none, a_xor, a_xor_, a_xori, a_xoris,
  73. { simplified mnemonics }
  74. a_subi, a_subis, a_subic, a_subic_, a_sub, a_sub_, a_subo, a_subo_,
  75. a_subc, a_subc_, a_subco, a_subco_, a_none, a_none, a_none, a_none,
  76. a_extlwi, a_extlwi_, a_extrwi, a_extrwi_, a_inslwi, a_inslwi_, a_insrwi,
  77. a_insrwi_, a_rotlwi, a_rotlwi_, a_rotlw, a_rotlw_, a_slwi, a_slwi_,
  78. a_srwi, a_srwi_, a_clrlwi, a_clrlwi_, a_clrrwi, a_clrrwi_, a_clrslwi,
  79. a_clrslwi_, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  80. a_none, a_none {move to special prupose reg}, a_none {move from special purpose reg},
  81. a_none, a_none, a_none, a_none, a_none, a_none, a_not, a_not_, a_none, a_none, a_none,
  82. a_none, a_none, a_none, a_none);
  83. function TCpuAsmOptimizer.cmpi_mfcr_opt(p, next1, next2: taicpu): boolean;
  84. var
  85. next3, prev: tai;
  86. inverse, prevrlwinm: boolean;
  87. begin
  88. result := true;
  89. inverse :=
  90. getnextinstruction(next2,next3) and
  91. (next3.typ = ait_instruction) and
  92. (taicpu(next3).opcode = A_XORI) and
  93. (taicpu(next3).oper[0]^.reg = taicpu(next3).oper[1]^.reg) and
  94. (taicpu(next3).oper[0]^.reg = taicpu(next2).oper[0]^.reg) and
  95. (taicpu(next3).oper[2]^.val = 1);
  96. case taicpu(next2).oper[2]^.val of
  97. 1:
  98. begin
  99. // less than zero or greater/equal than zero (the xori remains in
  100. // in the latter case). Doesn't make sense for unsigned comparisons.
  101. if (p.opcode = A_CMPWI) then
  102. begin
  103. p.opcode := A_SRWI;
  104. p.ops := 3;
  105. p.loadreg(1,p.oper[0]^.reg);
  106. p.loadreg(0,next1.oper[0]^.reg);
  107. p.loadconst(2,31);
  108. asml.remove(next1);
  109. next1.free;
  110. asml.remove(next2);
  111. next2.free;
  112. end
  113. else
  114. result := false;
  115. end;
  116. {
  117. needs two registers to work with
  118. 2:
  119. begin
  120. // greater or less/equal to zero
  121. end;
  122. }
  123. 3:
  124. begin
  125. prevrlwinm :=
  126. getlastinstruction(p,prev) and
  127. (prev.typ = ait_instruction) and
  128. ((taicpu(prev).opcode = A_RLWINM) or
  129. (taicpu(prev).opcode = A_RLWINM_)) and
  130. (taicpu(prev).oper[0]^.reg = p.oper[0]^.reg) and
  131. (taicpu(prev).oper[3]^.val = taicpu(prev).oper[4]^.val);
  132. if (prevrlwinm) then
  133. begin
  134. // isolate the bit we need
  135. if (taicpu(prev).oper[3]^.val <> 31) then
  136. begin
  137. p.opcode := A_RLWINM;
  138. p.ops := 5;
  139. p.loadreg(1,p.oper[0]^.reg);
  140. p.loadreg(0,next1.oper[0]^.reg);
  141. p.loadconst(2,taicpu(prev).oper[3]^.val + 1);
  142. p.loadconst(3,31);
  143. p.loadconst(4,31);
  144. end
  145. else { if (taicpu(prev).oper[0]^.reg <> next1.oper[0]^.reg) then }
  146. begin
  147. p.opcode := A_MR;
  148. p.loadreg(1,p.oper[0]^.reg);
  149. p.loadreg(0,next1.oper[0]^.reg);
  150. end;
  151. if not inverse then
  152. begin
  153. next1.ops := 3;
  154. next1.opcode := A_XORI;
  155. next1.loadreg(1,next1.oper[0]^.reg);
  156. next1.loadconst(2,1);
  157. end
  158. else
  159. begin
  160. asml.remove(next1);
  161. next1.free;
  162. asml.remove(next3);
  163. next3.free;
  164. end;
  165. asml.remove(next2);
  166. next2.free;
  167. end
  168. else
  169. begin
  170. // equal/not equal to zero (the xori remains in the latter case;
  171. // there's a more optimal sequence without it, but needs extra
  172. // register)
  173. p.opcode := A_CNTLZW;
  174. p.loadreg(1,p.oper[0]^.reg);
  175. p.loadreg(0,next1.oper[0]^.reg);
  176. next1.ops := 3;
  177. next1.opcode := A_SRWI;
  178. next1.loadreg(1,next1.oper[0]^.reg);
  179. next1.loadconst(2,5);
  180. asml.remove(next2);
  181. next2.free;
  182. end;
  183. end;
  184. else
  185. result := false;
  186. end;
  187. end;
  188. function rlwinm2mask(l1,l2: longint): longint;
  189. begin
  190. // 1 shl 32 = 1 instead of 0 on x86
  191. if (l1 <> 0) then
  192. result := longint(cardinal(1) shl (32 - l1) - 1) xor (cardinal(1) shl (31 - l2) - 1)
  193. else
  194. result := longint(not(cardinal(1) shl (31 - l2) - 1));
  195. if (l1 > l2) then
  196. result := not(result);
  197. end;
  198. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  199. var
  200. next1, next2: tai;
  201. l1, l2, shlcount: longint;
  202. begin
  203. result := false;
  204. case p.typ of
  205. ait_instruction:
  206. begin
  207. case taicpu(p).opcode of
  208. A_CMPWI,
  209. A_CMPLWI:
  210. begin
  211. if (taicpu(p).oper[1]^.typ = top_const) and
  212. (taicpu(p).oper[1]^.val = 0) and
  213. getnextinstruction(p,next1) and
  214. (next1.typ = ait_instruction) and
  215. (taicpu(next1).opcode = A_MFCR) and
  216. getnextinstruction(next1,next2) and
  217. (taicpu(next2).opcode = A_RLWINM) and
  218. (taicpu(next2).oper[0]^.reg = taicpu(next2).oper[1]^.reg) and
  219. (taicpu(next2).oper[0]^.reg = taicpu(next1).oper[0]^.reg) and
  220. (taicpu(next2).oper[3]^.val = 31) and
  221. (taicpu(next2).oper[4]^.val = 31) and
  222. cmpi_mfcr_opt(taicpu(p),taicpu(next1),taicpu(next2)) then
  223. result := true;
  224. end;
  225. { seems the register allocator doesn't generate superfluous fmr's }
  226. { A_FMR, }
  227. A_MR:
  228. begin
  229. if getnextinstruction(p,next1) and
  230. (next1.typ = ait_instruction) and
  231. (calculation_target_op0[taicpu(next1).opcode] <> a_none) and
  232. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) then
  233. begin
  234. for l1 := 1 to taicpu(next1).ops - 1 do
  235. if (taicpu(next1).oper[l1]^.typ = top_reg) and
  236. (taicpu(next1).oper[l1]^.reg = taicpu(p).oper[0]^.reg) then
  237. taicpu(next1).loadreg(l1,taicpu(p).oper[1]^.reg);
  238. asml.remove(p);
  239. p.free;
  240. p := next1;
  241. result := true;
  242. end;
  243. end;
  244. A_SLWI:
  245. begin
  246. if getnextinstruction(p,next1) and
  247. (next1.typ = ait_instruction) and
  248. ((taicpu(next1).opcode = A_RLWINM) or
  249. (taicpu(next1).opcode = A_SLWI) or
  250. (taicpu(next1).opcode = A_SRWI)) and
  251. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  252. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) then
  253. begin
  254. { convert slwi to rlwinm and see if the rlwinm }
  255. { optimization can do something with it }
  256. taicpu(p).opcode := A_RLWINM;
  257. taicpu(p).ops := 5;
  258. taicpu(p).loadconst(2,taicpu(p).oper[2]^.val);
  259. taicpu(p).loadconst(3,0);
  260. taicpu(p).loadconst(4,31-taicpu(p).oper[2]^.val);
  261. result := true;
  262. end;
  263. end;
  264. A_SRWI:
  265. begin
  266. if getnextinstruction(p,next1) and
  267. (next1.typ = ait_instruction) and
  268. ((taicpu(next1).opcode = A_SLWI) or
  269. (taicpu(next1).opcode = A_RLWINM) or
  270. (taicpu(next1).opcode = A_SRWI)) and
  271. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  272. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) then
  273. case taicpu(next1).opcode of
  274. A_SLWI:
  275. begin
  276. taicpu(p).opcode := A_RLWINM;
  277. taicpu(p).ops := 5;
  278. taicpu(p).loadconst(2,taicpu(next1).oper[2]^.val-taicpu(p).oper[2]^.val);
  279. if (taicpu(p).oper[2]^.val < 0) then
  280. begin
  281. taicpu(p).loadconst(3,-taicpu(p).oper[2]^.val);
  282. taicpu(p).loadconst(4,31-taicpu(next1).oper[2]^.val);
  283. inc(taicpu(p).oper[2]^.val,32);
  284. end
  285. else
  286. begin
  287. taicpu(p).loadconst(3,0);
  288. taicpu(p).loadconst(4,31-taicpu(next1).oper[2]^.val);
  289. end;
  290. asml.remove(next1);
  291. next1.free;
  292. result := true;
  293. end;
  294. A_RLWINM:
  295. begin
  296. { convert srwi to rlwinm and see if the rlwinm }
  297. { optimization can do something with it }
  298. taicpu(p).opcode := A_RLWINM;
  299. taicpu(p).ops := 5;
  300. taicpu(p).loadconst(3,taicpu(p).oper[2]^.val);
  301. taicpu(p).loadconst(4,31);
  302. taicpu(p).loadconst(2,(32-taicpu(p).oper[2]^.val) and 31);
  303. result := true;
  304. end;
  305. end;
  306. end;
  307. A_RLWINM:
  308. begin
  309. if getnextinstruction(p,next1) and
  310. (next1.typ = ait_instruction) and
  311. ((taicpu(next1).opcode = A_RLWINM) or
  312. (taicpu(next1).opcode = A_SRWI) or
  313. (taicpu(next1).opcode = A_SLWI)) and
  314. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg) and
  315. // both source and target of next1 must equal target of p
  316. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg) then
  317. begin
  318. case taicpu(next1).opcode of
  319. A_RLWINM:
  320. begin
  321. shlcount := taicpu(next1).oper[2]^.val;
  322. l2 := rlwinm2mask(taicpu(next1).oper[3]^.val,taicpu(next1).oper[4]^.val);
  323. end;
  324. A_SLWI:
  325. begin
  326. shlcount := taicpu(next1).oper[2]^.val;
  327. l2 := (-1) shl shlcount;
  328. end;
  329. A_SRWI:
  330. begin
  331. shlcount := 32-taicpu(next1).oper[2]^.val;
  332. l2 := (-1) shr taicpu(next1).oper[2]^.val;
  333. end;
  334. end;
  335. l1 := rlwinm2mask((taicpu(p).oper[3]^.val-shlcount) and 31,(taicpu(p).oper[4]^.val-shlcount) and 31);
  336. l1 := l1 and l2;
  337. case l1 of
  338. -1:
  339. begin
  340. taicpu(p).oper[2]^.val := (taicpu(p).oper[2]^.val + shlcount) and 31;
  341. asml.remove(next1);
  342. next1.free;
  343. if (taicpu(p).oper[2]^.val = 0) then
  344. begin
  345. next1 := tai(p.next);
  346. asml.remove(p);
  347. p.free;
  348. p := next1;
  349. result := true;
  350. end;
  351. end;
  352. 0:
  353. begin
  354. // masks have no bits in common
  355. taicpu(p).opcode := A_LI;
  356. taicpu(p).loadconst(1,0);
  357. taicpu(p).freeop(2);
  358. taicpu(p).freeop(3);
  359. taicpu(p).freeop(4);
  360. taicpu(p).ops := 2;
  361. taicpu(p).opercnt := 2;
  362. asml.remove(next1);
  363. next1.free;
  364. result := true;
  365. end
  366. else if tcgppc(cg).get_rlwi_const(l1,l1,l2) then
  367. begin
  368. taicpu(p).oper[2]^.val := (taicpu(p).oper[2]^.val + shlcount) and 31;
  369. taicpu(p).oper[3]^.val := l1;
  370. taicpu(p).oper[4]^.val := l2;
  371. asml.remove(next1);
  372. next1.free;
  373. result := true;
  374. end;
  375. end;
  376. end;
  377. end;
  378. end;
  379. end;
  380. end;
  381. end;
  382. const
  383. modifyflags: array[tasmop] of tasmop =
  384. (a_none, a_add_, a_add_, a_addo_, a_addo_, a_addc_, a_addc_, a_addco_, a_addco_,
  385. a_adde_, a_adde_, a_addeo_, a_addeo_, {a_addi could be addic_ if sure doesn't disturb carry} a_none, a_addic_, a_addic_, a_none,
  386. a_addme_, a_addme_, a_addmeo_, a_addmeo_, a_addze_, a_addze_, a_addzeo_,
  387. a_addzeo_, a_and_, a_and_, a_andc_, a_andc_, a_andi_, a_andis_, a_none,
  388. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  389. a_none, a_none, a_none, a_none, a_none, a_cntlzw_, a_cntlzw_, a_none,
  390. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  391. a_none, a_none, a_none, a_none, a_none, a_none, a_divw_, a_divw_, a_divwo_, a_divwo_,
  392. a_divwu_, a_divwu_, a_divwuo_, a_divwuo_, a_none, a_none, a_none, a_eqv_,
  393. a_eqv_, a_extsb_, a_extsb_, a_extsh_, a_extsh_, a_none, a_none, a_none,
  394. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  395. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  396. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  397. a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  398. a_none, a_none, a_none, a_none, a_none, a_none,
  399. a_none, a_none, a_none, a_none,
  400. a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  401. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  402. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  403. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  404. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  405. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  406. a_none, a_none, a_none, a_mffs, a_mffs_, a_mfmsr, a_mfspr, a_mfsr,
  407. a_mfsrin, a_mftb, a_mtcrf, a_none, a_none, a_none, a_none,
  408. a_none, a_none, a_none, a_none, a_none, a_none, a_mulhw_,
  409. a_mulhw_, a_mulhwu_, a_mulhwu_, a_none, a_mullw_, a_mullw_, a_mullwo_,
  410. a_mullwo_, a_nand_, a_nand_, a_neg_, a_neg_, a_nego_, a_nego_, a_nor_, a_nor_,
  411. a_or_, a_or_, a_orc_, a_orc_, a_none, a_none, a_none, a_rlwimi_, a_rlwimi_,
  412. a_rlwinm_, a_rlwinm_, a_rlwnm_, a_rlwnm_, a_none, a_slw_, a_slw_, a_sraw_, a_sraw_,
  413. a_srawi_, a_srawi_,a_srw_, a_srw_, a_none, a_none, a_none, a_none, a_none,
  414. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  415. a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  416. a_none, a_none, a_none, a_none, a_none, a_subf_, a_subf_, a_subfo_,
  417. a_subfo_, a_subfc_, a_subfc_, a_subfco_, a_subfco_, a_subfe_, a_subfe_,
  418. a_subfeo_, a_subfeo_, a_none, a_subfme_, a_subfme_, a_subfmeo_, a_subfmeo_,
  419. a_subfze_, a_subfze_, a_subfzeo_, a_subfzeo_, a_none, a_none, a_none,
  420. a_none, a_none, a_none, a_xor_, a_xor_, a_none, a_none,
  421. { simplified mnemonics }
  422. a_none, a_none, a_subic_, a_subic_, a_sub_, a_sub_, a_subo_, a_subo_,
  423. a_subc_, a_subc_, a_subco_, a_subco_, a_none, a_none, a_none, a_none,
  424. a_extlwi_, a_extlwi_, a_extrwi_, a_extrwi_, a_inslwi_, a_inslwi_, a_insrwi_,
  425. a_insrwi_, a_rotlwi_, a_rotlwi_, a_rotlw_, a_rotlw_, a_slwi_, a_slwi_,
  426. a_srwi_, a_srwi_, a_clrlwi_, a_clrlwi_, a_clrrwi_, a_clrrwi_, a_clrslwi_,
  427. a_clrslwi_, a_none, a_none, a_none, a_none, a_none, a_none, a_none,
  428. a_none, a_none {move to special prupose reg}, a_none {move from special purpose reg},
  429. a_none, a_none, a_none, a_none, a_mr_, a_mr_, a_not_, a_not_, a_none, a_none, a_none,
  430. a_none, a_none, a_none, a_none);
  431. function changetomodifyflags(p: taicpu): boolean;
  432. begin
  433. result := false;
  434. if (modifyflags[p.opcode] <> a_none) then
  435. begin
  436. p.opcode := modifyflags[p.opcode];
  437. result := true;
  438. end;
  439. end;
  440. function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  441. var
  442. next1: tai;
  443. begin
  444. result := false;
  445. case p.typ of
  446. ait_instruction:
  447. begin
  448. case taicpu(p).opcode of
  449. A_RLWINM_:
  450. begin
  451. // rlwinm_ is cracked on the G5, andi_/andis_ aren't
  452. if (taicpu(p).oper[2]^.val = 0) then
  453. if (taicpu(p).oper[3]^.val < 16) and
  454. (taicpu(p).oper[4]^.val < 16) then
  455. begin
  456. taicpu(p).opcode := A_ANDIS_;
  457. taicpu(p).oper[2]^.val := word(
  458. ((1 shl (16-taicpu(p).oper[3]^.val)) - 1) xor
  459. ((1 shl (15-taicpu(p).oper[4]^.val)) - 1));
  460. taicpu(p).freeop(3);
  461. taicpu(p).freeop(4);
  462. taicpu(p).ops := 3;
  463. taicpu(p).opercnt := 3;
  464. end
  465. else if (taicpu(p).oper[3]^.val >= 16) and
  466. (taicpu(p).oper[4]^.val >= 16) then
  467. begin
  468. taicpu(p).opcode := A_ANDI_;
  469. taicpu(p).oper[2]^.val := word(rlwinm2mask(taicpu(p).oper[3]^.val,taicpu(p).oper[4]^.val));
  470. taicpu(p).freeop(3);
  471. taicpu(p).freeop(4);
  472. taicpu(p).ops := 3;
  473. taicpu(p).opercnt := 3;
  474. end;
  475. end;
  476. end;
  477. // change "integer operation with destination reg" followed by a
  478. // comparison to zero of that reg, with a variant of that integer
  479. // operation which sets the flags (if it exists)
  480. if not(result) and
  481. (taicpu(p).ops >= 2) and
  482. (taicpu(p).oper[0]^.typ = top_reg) and
  483. (taicpu(p).oper[1]^.typ = top_reg) and
  484. getnextinstruction(p,next1) and
  485. (next1.typ = ait_instruction) and
  486. (taicpu(next1).opcode = A_CMPWI) and
  487. // make sure it the result goes to cr0
  488. (((taicpu(next1).ops = 2) and
  489. (taicpu(next1).oper[1]^.val = 0) and
  490. (taicpu(next1).oper[0]^.reg = taicpu(p).oper[0]^.reg)) or
  491. ((taicpu(next1).ops = 3) and
  492. (taicpu(next1).oper[2]^.val = 0) and
  493. (taicpu(next1).oper[0]^.typ = top_reg) and
  494. (getsupreg(taicpu(next1).oper[0]^.reg) = RS_CR0) and
  495. (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg))) and
  496. changetomodifyflags(taicpu(p)) then
  497. begin
  498. asml.remove(next1);
  499. next1.free;
  500. result := true;
  501. end;
  502. end;
  503. end;
  504. end;
  505. begin
  506. casmoptimizer:=TCpuAsmOptimizer;
  507. End.