rgx86.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the x86 specific class for the register
  4. allocator
  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 rgx86;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpubase,cgbase,cgutils,
  23. aasmtai,aasmdata,aasmsym,aasmcpu,
  24. rgobj;
  25. type
  26. trgx86 = class(trgobj)
  27. function get_spill_subreg(r : tregister) : tsubregister;override;
  28. function do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;override;
  29. end;
  30. tpushedsavedloc = record
  31. case byte of
  32. 0: (pushed: boolean);
  33. 1: (ofs: longint);
  34. end;
  35. tpushedsavedfpu = array[tsuperregister] of tpushedsavedloc;
  36. trgx86fpu = class
  37. { these counters contain the number of elements in the }
  38. { unusedregsxxx/usableregsxxx sets }
  39. countunusedregsfpu : byte;
  40. { Contains the registers which are really used by the proc itself.
  41. It doesn't take care of registers used by called procedures
  42. }
  43. used_in_proc : tcpuregisterset;
  44. {reg_pushes_other : regvarother_longintarray;
  45. is_reg_var_other : regvarother_booleanarray;
  46. regvar_loaded_other : regvarother_booleanarray;}
  47. fpuvaroffset : byte;
  48. constructor create;
  49. function getregisterfpu(list: TAsmList) : tregister;
  50. procedure ungetregisterfpu(list: TAsmList; r : tregister);
  51. { pushes and restores registers }
  52. procedure saveusedfpuregisters(list:TAsmList;
  53. var saved:Tpushedsavedfpu;
  54. const s:Tcpuregisterset);
  55. procedure restoreusedfpuregisters(list:TAsmList;
  56. const saved:Tpushedsavedfpu);
  57. { corrects the fpu stack register by ofs }
  58. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  59. end;
  60. implementation
  61. uses
  62. verbose;
  63. const
  64. { This value is used in tsaved. If the array value is equal
  65. to this, then this means that this register is not used.}
  66. reg_not_saved = $7fffffff;
  67. {******************************************************************************
  68. Trgcpu
  69. ******************************************************************************}
  70. function trgx86.get_spill_subreg(r : tregister) : tsubregister;
  71. begin
  72. result:=getsubreg(r);
  73. end;
  74. { Decide wether a "replace" spill is possible, i.e. wether we can replace a register
  75. in an instruction by a memory reference. For example, in "mov ireg26d,0", the imaginary
  76. register ireg26d can be replaced by a memory reference.}
  77. function trgx86.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  78. { returns true if opcde is an avx opcode which allows only the first (zero) operand might be a memory reference }
  79. function avx_opcode_only_op0_may_be_memref(opcode : TAsmOp) : boolean;
  80. begin
  81. case opcode of
  82. A_VMAXPD,
  83. A_VMAXPS,
  84. A_VMAXSD,
  85. A_VMAXSS,
  86. A_VMINPD,
  87. A_VMINPS,
  88. A_VMINSD,
  89. A_VMINSS,
  90. A_VMULSS,
  91. A_VMULSD,
  92. A_VSUBSS,
  93. A_VSUBSD,
  94. A_VADDSD,
  95. A_VADDSS,
  96. A_VDIVSD,
  97. A_VDIVSS,
  98. A_VSQRTSD,
  99. A_VSQRTSS,
  100. A_VCVTDQ2PD,
  101. A_VCVTDQ2PS,
  102. A_VCVTPD2DQ,
  103. A_VCVTPD2PS,
  104. A_VCVTPS2DQ,
  105. A_VCVTPS2PD,
  106. A_VCVTSD2SI,
  107. A_VCVTSD2SS,
  108. A_VCVTSI2SD,
  109. A_VCVTSS2SD,
  110. A_VCVTTPD2DQ,
  111. A_VCVTTPS2DQ,
  112. A_VCVTTSD2SI,
  113. A_VCVTSI2SS,
  114. A_VCVTSS2SI,
  115. A_VCVTTSS2SI,
  116. A_VXORPD,
  117. A_VXORPS,
  118. A_VORPD,
  119. A_VORPS,
  120. A_VANDPD,
  121. A_VANDPS,
  122. A_VUNPCKLPS,
  123. A_VUNPCKHPS,
  124. A_VSHUFPD,
  125. A_VREDUCEPD,
  126. A_VREDUCEPS,
  127. A_VREDUCESD,
  128. A_VREDUCESS,
  129. A_VROUNDSS,
  130. A_VROUNDSD:
  131. result:=true;
  132. else
  133. result:=false;
  134. end;
  135. end;
  136. var
  137. n,replaceoper : longint;
  138. is_subh: Boolean;
  139. begin
  140. result:=false;
  141. with taicpu(instr) do
  142. begin
  143. replaceoper:=-1;
  144. case ops of
  145. 1 :
  146. begin
  147. if (oper[0]^.typ=top_reg) and
  148. (getregtype(oper[0]^.reg)=regtype) then
  149. begin
  150. if get_alias(getsupreg(oper[0]^.reg))<>orgreg then
  151. internalerror(200410101);
  152. replaceoper:=0;
  153. end;
  154. end;
  155. 2,3 :
  156. begin
  157. { avx instruction?
  158. currently this rule is sufficient but it might be extended }
  159. if (ops=3) and (opcode<>A_SHRD) and (opcode<>A_SHLD) and (opcode<>A_IMUL) then
  160. begin
  161. { BMI shifting/rotating instructions have special requirements regarding spilling, only
  162. the middle operand can be replaced }
  163. if ((opcode=A_RORX) or (opcode=A_SHRX) or (opcode=A_SARX) or (opcode=A_SHLX)) then
  164. begin
  165. if (oper[1]^.typ=top_reg) and (getregtype(oper[1]^.reg)=regtype) and (get_alias(getsupreg(oper[1]^.reg))=orgreg) then
  166. replaceoper:=1;
  167. end
  168. { avx instructions allow only the first operand (at&t counting) to be a register operand
  169. all operands must be registers ... }
  170. else if (oper[0]^.typ=top_reg) and
  171. (getregtype(oper[0]^.reg)=regtype) and
  172. (oper[1]^.typ=top_reg) and
  173. (oper[2]^.typ=top_reg) and
  174. { but they must be different }
  175. ((getregtype(oper[1]^.reg)<>regtype) or
  176. (get_alias(getsupreg(oper[0]^.reg))<>get_alias(getsupreg(oper[1]^.reg)))
  177. ) and
  178. ((getregtype(oper[2]^.reg)<>regtype) or
  179. (get_alias(getsupreg(oper[0]^.reg))<>get_alias(getsupreg(oper[2]^.reg)))
  180. ) and
  181. (get_alias(getsupreg(oper[0]^.reg))=orgreg) then
  182. replaceoper:=0;
  183. end
  184. else
  185. begin
  186. { We can handle opcodes with 2 and 3-op imul/shrd/shld the same way, where the 3rd operand is const or CL,
  187. that doesn't need spilling.
  188. However, due to AT&T order inside the compiler, the 3rd operand is
  189. numbered 0, so look at operand no. 1 and 2 if we have 3 operands by
  190. adding a "n". }
  191. n:=0;
  192. if ops=3 then
  193. n:=1;
  194. { lea is tricky: part of operand 0 can be spilled and the instruction can converted into an
  195. add, if base or index shall be spilled and the other one is equal the destination }
  196. if (opcode=A_LEA) then
  197. begin
  198. if (oper[0]^.ref^.offset=0) and
  199. (oper[0]^.ref^.scalefactor in [0,1]) and
  200. (((getregtype(oper[0]^.ref^.base)=regtype) and
  201. (get_alias(getsupreg(oper[0]^.ref^.base))=orgreg) and
  202. (getregtype(oper[0]^.ref^.index)=getregtype(oper[1]^.reg)) and
  203. (get_alias(getsupreg(oper[0]^.ref^.index))=get_alias(getsupreg(oper[1]^.reg)))) or
  204. ((getregtype(oper[0]^.ref^.index)=regtype) and
  205. (get_alias(getsupreg(oper[0]^.ref^.index))=orgreg) and
  206. (getregtype(oper[0]^.ref^.base)=getregtype(oper[1]^.reg)) and
  207. (get_alias(getsupreg(oper[0]^.ref^.base))=get_alias(getsupreg(oper[1]^.reg))))
  208. ) then
  209. replaceoper:=0;
  210. end
  211. else if (oper[n+0]^.typ=top_reg) and
  212. (oper[n+1]^.typ=top_reg) and
  213. ((getregtype(oper[n+0]^.reg)<>regtype) or
  214. (getregtype(oper[n+1]^.reg)<>regtype) or
  215. (get_alias(getsupreg(oper[n+0]^.reg))<>get_alias(getsupreg(oper[n+1]^.reg)))) then
  216. begin
  217. if (getregtype(oper[n+0]^.reg)=regtype) and
  218. (get_alias(getsupreg(oper[n+0]^.reg))=orgreg) then
  219. replaceoper:=0+n
  220. else if (getregtype(oper[n+1]^.reg)=regtype) and
  221. (get_alias(getsupreg(oper[n+1]^.reg))=orgreg) then
  222. replaceoper:=1+n;
  223. end
  224. else if (oper[n+0]^.typ=top_reg) and
  225. (oper[n+1]^.typ=top_const) then
  226. begin
  227. if (getregtype(oper[0+n]^.reg)=regtype) and
  228. (get_alias(getsupreg(oper[0+n]^.reg))=orgreg) then
  229. replaceoper:=0+n
  230. else
  231. internalerror(200704282);
  232. end
  233. else if (oper[n+0]^.typ=top_const) and
  234. (oper[n+1]^.typ=top_reg) then
  235. begin
  236. if (getregtype(oper[1+n]^.reg)=regtype) and
  237. (get_alias(getsupreg(oper[1+n]^.reg))=orgreg) then
  238. replaceoper:=1+n
  239. else
  240. internalerror(200704283);
  241. end;
  242. case replaceoper of
  243. 0 :
  244. begin
  245. { Some instructions don't allow memory references
  246. for source }
  247. case opcode of
  248. A_BT,
  249. A_BTS,
  250. A_BTC,
  251. A_BTR,
  252. { shufp*/unpcklp* would require 16 byte alignment for memory locations so we force the source
  253. operand into a register }
  254. A_SHUFPD,
  255. A_SHUFPS,
  256. A_UNPCKLPD,
  257. A_UNPCKLPS :
  258. replaceoper:=-1;
  259. { movlhps/movhlps requires the second parameter to be XMM registers }
  260. A_MOVHLPS,
  261. A_MOVLHPS:
  262. replaceoper:=-1;
  263. else
  264. ;
  265. end;
  266. end;
  267. 1 :
  268. begin
  269. { Some instructions don't allow memory references
  270. for destination }
  271. case opcode of
  272. A_CMOVcc,
  273. A_MOVZX,
  274. A_MOVSX,
  275. {$ifdef x86_64}
  276. A_MOVSXD,
  277. {$endif x86_64}
  278. A_MULSS,
  279. A_MULSD,
  280. A_SUBSS,
  281. A_SUBSD,
  282. A_ADDSD,
  283. A_ADDSS,
  284. A_DIVSD,
  285. A_DIVSS,
  286. A_SQRTSD,
  287. A_SQRTSS,
  288. A_SHLD,
  289. A_SHRD,
  290. A_COMISD,
  291. A_COMISS,
  292. A_CVTDQ2PD,
  293. A_CVTDQ2PS,
  294. A_CVTPD2DQ,
  295. A_CVTPD2PI,
  296. A_CVTPD2PS,
  297. A_CVTPI2PD,
  298. A_CVTPS2DQ,
  299. A_CVTPS2PD,
  300. A_CVTSD2SI,
  301. A_CVTSD2SS,
  302. A_CVTSI2SD,
  303. A_CVTSS2SD,
  304. A_CVTTPD2PI,
  305. A_CVTTPD2DQ,
  306. A_CVTTPS2DQ,
  307. A_CVTTSD2SI,
  308. A_CVTPI2PS,
  309. A_CVTPS2PI,
  310. A_CVTSI2SS,
  311. A_CVTSS2SI,
  312. A_CVTTPS2PI,
  313. A_CVTTSS2SI,
  314. A_XORPD,
  315. A_XORPS,
  316. A_PXOR,
  317. A_PAND,
  318. A_POR,
  319. A_ORPD,
  320. A_ORPS,
  321. A_ANDPD,
  322. A_ANDPS,
  323. A_UNPCKLPS,
  324. A_UNPCKHPS,
  325. A_SHUFPD,
  326. A_SHUFPS,
  327. A_VCOMISD,
  328. A_VCOMISS,
  329. A_MINSS,
  330. A_MINSD,
  331. A_MINPS,
  332. A_MINPD,
  333. A_MAXSS,
  334. A_MAXSD,
  335. A_MAXPS,
  336. A_MAXPD:
  337. replaceoper:=-1;
  338. A_IMUL:
  339. if ops<>3 then
  340. replaceoper:=-1;
  341. {$ifdef x86_64}
  342. A_MOV:
  343. { 64 bit constants can only be moved into registers }
  344. if (oper[0]^.typ=top_const) and
  345. (oper[1]^.typ=top_reg) and
  346. ((oper[0]^.val<low(longint)) or
  347. (oper[0]^.val>high(longint))) then
  348. replaceoper:=-1;
  349. {$endif x86_64}
  350. else
  351. if avx_opcode_only_op0_may_be_memref(opcode) then
  352. replaceoper:=-1;
  353. end;
  354. end;
  355. 2 :
  356. begin
  357. { Some 3-op instructions don't allow memory references
  358. for destination }
  359. case instr.opcode of
  360. A_IMUL:
  361. replaceoper:=-1;
  362. else
  363. if avx_opcode_only_op0_may_be_memref(opcode) then
  364. replaceoper:=-1;
  365. end;
  366. end;
  367. end;
  368. end;
  369. end;
  370. end;
  371. {$ifdef x86_64}
  372. { 32 bit operations on 32 bit registers on x86_64 can result in
  373. zeroing the upper 32 bits of the register. This does not happen
  374. with memory operations, so we have to perform these calculations
  375. in registers.
  376. However, for instructions not modifying registers, this is not a problem }
  377. if (opsize=S_L) and (opcode<>A_CMP) and (opcode<>A_TEST) and (opcode<>A_BT) then
  378. replaceoper:=-1;
  379. {$endif x86_64}
  380. { Replace register with spill reference }
  381. if replaceoper<>-1 then
  382. begin
  383. if opcode=A_LEA then
  384. begin
  385. opcode:=A_ADD;
  386. oper[0]^.ref^:=spilltemp;
  387. end
  388. else
  389. begin
  390. is_subh:=getsubreg(oper[replaceoper]^.reg)=R_SUBH;
  391. oper[replaceoper]^.typ:=top_ref;
  392. new(oper[replaceoper]^.ref);
  393. oper[replaceoper]^.ref^:=spilltemp;
  394. if is_subh then
  395. inc(oper[replaceoper]^.ref^.offset);
  396. { memory locations aren't guaranteed to be aligned }
  397. case opcode of
  398. A_MOVAPS:
  399. opcode:=A_MOVSS;
  400. A_MOVAPD:
  401. opcode:=A_MOVSD;
  402. A_VMOVAPS:
  403. opcode:=A_VMOVSS;
  404. A_VMOVAPD:
  405. opcode:=A_VMOVSD;
  406. else
  407. ;
  408. end;
  409. end;
  410. result:=true;
  411. end;
  412. end;
  413. end;
  414. {******************************************************************************
  415. Trgx86fpu
  416. ******************************************************************************}
  417. constructor Trgx86fpu.create;
  418. begin
  419. used_in_proc:=[];
  420. end;
  421. function trgx86fpu.getregisterfpu(list: TAsmList) : tregister;
  422. begin
  423. { note: don't return R_ST0, see comments above implementation of }
  424. { a_loadfpu_* methods in cgcpu (JM) }
  425. result:=NR_ST;
  426. end;
  427. procedure trgx86fpu.ungetregisterfpu(list : TAsmList; r : tregister);
  428. begin
  429. { nothing to do, fpu stack management is handled by the load/ }
  430. { store operations in cgcpu (JM) }
  431. end;
  432. function trgx86fpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  433. begin
  434. correct_fpuregister:=r;
  435. setsupreg(correct_fpuregister,ofs);
  436. end;
  437. procedure trgx86fpu.saveusedfpuregisters(list: TAsmList;
  438. var saved : tpushedsavedfpu;
  439. const s: tcpuregisterset);
  440. { var
  441. r : tregister;
  442. hr : treference; }
  443. begin
  444. used_in_proc:=used_in_proc+s;
  445. { TODO: firstsavefpureg}
  446. (*
  447. { don't try to save the fpu registers if not desired (e.g. for }
  448. { the 80x86) }
  449. if firstsavefpureg <> R_NO then
  450. for r.enum:=firstsavefpureg to lastsavefpureg do
  451. begin
  452. saved[r.enum].ofs:=reg_not_saved;
  453. { if the register is used by the calling subroutine and if }
  454. { it's not a regvar (those are handled separately) }
  455. if not is_reg_var_other[r.enum] and
  456. (r.enum in s) and
  457. { and is present in use }
  458. not(r.enum in unusedregsfpu) then
  459. begin
  460. { then save it }
  461. tg.GetTemp(list,extended_size,tt_persistent,hr);
  462. saved[r.enum].ofs:=hr.offset;
  463. cg.a_loadfpu_reg_ref(list,OS_FLOAT,OS_FLOAT,r,hr);
  464. cg.a_reg_dealloc(list,r);
  465. include(unusedregsfpu,r.enum);
  466. inc(countunusedregsfpu);
  467. end;
  468. end;
  469. *)
  470. end;
  471. procedure trgx86fpu.restoreusedfpuregisters(list : TAsmList;
  472. const saved : tpushedsavedfpu);
  473. {
  474. var
  475. r,r2 : tregister;
  476. hr : treference;
  477. }
  478. begin
  479. { TODO: firstsavefpureg}
  480. (*
  481. if firstsavefpureg <> R_NO then
  482. for r.enum:=lastsavefpureg downto firstsavefpureg do
  483. begin
  484. if saved[r.enum].ofs <> reg_not_saved then
  485. begin
  486. r2.enum:=R_INTREGISTER;
  487. r2.number:=NR_FRAME_POINTER_REG;
  488. reference_reset_base(hr,r2,saved[r.enum].ofs);
  489. cg.a_reg_alloc(list,r);
  490. cg.a_loadfpu_ref_reg(list,OS_FLOAT,OS_FLOAT,hr,r);
  491. if not (r.enum in unusedregsfpu) then
  492. { internalerror(10)
  493. in n386cal we always save/restore the reg *state*
  494. using save/restoreunusedstate -> the current state
  495. may not be real (JM) }
  496. else
  497. begin
  498. dec(countunusedregsfpu);
  499. exclude(unusedregsfpu,r.enum);
  500. end;
  501. tg.UnGetTemp(list,hr);
  502. end;
  503. end;
  504. *)
  505. end;
  506. (*
  507. procedure Trgx86fpu.saveotherregvars(list: TAsmList; const s: totherregisterset);
  508. var
  509. r: Tregister;
  510. begin
  511. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  512. exit;
  513. if firstsavefpureg <> NR_NO then
  514. for r.enum := firstsavefpureg to lastsavefpureg do
  515. if is_reg_var_other[r.enum] and
  516. (r.enum in s) then
  517. store_regvar(list,r);
  518. end;
  519. *)
  520. end.