rgx86.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. (oper[1]^.typ=top_reg) and
  172. (oper[2]^.typ=top_reg) and
  173. { but they must be different }
  174. ((getregtype(oper[1]^.reg)<>regtype) or
  175. (get_alias(getsupreg(oper[0]^.reg))<>get_alias(getsupreg(oper[1]^.reg)))
  176. ) and
  177. ((getregtype(oper[2]^.reg)<>regtype) or
  178. (get_alias(getsupreg(oper[0]^.reg))<>get_alias(getsupreg(oper[2]^.reg)))
  179. ) and
  180. (get_alias(getsupreg(oper[0]^.reg))=orgreg) then
  181. replaceoper:=0;
  182. end
  183. else
  184. begin
  185. { We can handle opcodes with 2 and 3-op imul/shrd/shld the same way, where the 3rd operand is const or CL,
  186. that doesn't need spilling.
  187. However, due to AT&T order inside the compiler, the 3rd operand is
  188. numbered 0, so look at operand no. 1 and 2 if we have 3 operands by
  189. adding a "n". }
  190. n:=0;
  191. if ops=3 then
  192. n:=1;
  193. { lea is tricky: part of operand 0 can be spilled and the instruction can converted into an
  194. add, if base or index shall be spilled and the other one is equal the destination }
  195. if (opcode=A_LEA) then
  196. begin
  197. if (oper[0]^.ref^.offset=0) and
  198. (oper[0]^.ref^.scalefactor in [0,1]) and
  199. (((getregtype(oper[0]^.ref^.base)=regtype) and
  200. (get_alias(getsupreg(oper[0]^.ref^.base))=orgreg) and
  201. (getregtype(oper[0]^.ref^.index)=getregtype(oper[1]^.reg)) and
  202. (get_alias(getsupreg(oper[0]^.ref^.index))=get_alias(getsupreg(oper[1]^.reg)))) or
  203. ((getregtype(oper[0]^.ref^.index)=regtype) and
  204. (get_alias(getsupreg(oper[0]^.ref^.index))=orgreg) and
  205. (getregtype(oper[0]^.ref^.base)=getregtype(oper[1]^.reg)) and
  206. (get_alias(getsupreg(oper[0]^.ref^.base))=get_alias(getsupreg(oper[1]^.reg))))
  207. ) then
  208. replaceoper:=0;
  209. end
  210. else if (oper[n+0]^.typ=top_reg) and
  211. (oper[n+1]^.typ=top_reg) and
  212. ((getregtype(oper[n+0]^.reg)<>regtype) or
  213. (getregtype(oper[n+1]^.reg)<>regtype) or
  214. (get_alias(getsupreg(oper[n+0]^.reg))<>get_alias(getsupreg(oper[n+1]^.reg)))) then
  215. begin
  216. if (getregtype(oper[n+0]^.reg)=regtype) and
  217. (get_alias(getsupreg(oper[n+0]^.reg))=orgreg) then
  218. replaceoper:=0+n
  219. else if (getregtype(oper[n+1]^.reg)=regtype) and
  220. (get_alias(getsupreg(oper[n+1]^.reg))=orgreg) then
  221. replaceoper:=1+n;
  222. end
  223. else if (oper[n+0]^.typ=top_reg) and
  224. (oper[n+1]^.typ=top_const) then
  225. begin
  226. if (getregtype(oper[0+n]^.reg)=regtype) and
  227. (get_alias(getsupreg(oper[0+n]^.reg))=orgreg) then
  228. replaceoper:=0+n
  229. else
  230. internalerror(200704282);
  231. end
  232. else if (oper[n+0]^.typ=top_const) and
  233. (oper[n+1]^.typ=top_reg) then
  234. begin
  235. if (getregtype(oper[1+n]^.reg)=regtype) and
  236. (get_alias(getsupreg(oper[1+n]^.reg))=orgreg) then
  237. replaceoper:=1+n
  238. else
  239. internalerror(200704283);
  240. end;
  241. case replaceoper of
  242. 0 :
  243. begin
  244. { Some instructions don't allow memory references
  245. for source }
  246. case opcode of
  247. A_BT,
  248. A_BTS,
  249. A_BTC,
  250. A_BTR,
  251. { shufp*/unpcklp* would require 16 byte alignment for memory locations so we force the source
  252. operand into a register }
  253. A_SHUFPD,
  254. A_SHUFPS,
  255. A_UNPCKLPD,
  256. A_UNPCKLPS :
  257. replaceoper:=-1;
  258. { movlhps/movhlps requires the second parameter to be XMM registers }
  259. A_MOVHLPS,
  260. A_MOVLHPS:
  261. replaceoper:=-1;
  262. else
  263. ;
  264. end;
  265. end;
  266. 1 :
  267. begin
  268. { Some instructions don't allow memory references
  269. for destination }
  270. case opcode of
  271. A_CMOVcc,
  272. A_MOVZX,
  273. A_MOVSX,
  274. {$ifdef x86_64}
  275. A_MOVSXD,
  276. {$endif x86_64}
  277. A_MULSS,
  278. A_MULSD,
  279. A_SUBSS,
  280. A_SUBSD,
  281. A_ADDSD,
  282. A_ADDSS,
  283. A_DIVSD,
  284. A_DIVSS,
  285. A_SQRTSD,
  286. A_SQRTSS,
  287. A_SHLD,
  288. A_SHRD,
  289. A_COMISD,
  290. A_COMISS,
  291. A_CVTDQ2PD,
  292. A_CVTDQ2PS,
  293. A_CVTPD2DQ,
  294. A_CVTPD2PI,
  295. A_CVTPD2PS,
  296. A_CVTPI2PD,
  297. A_CVTPS2DQ,
  298. A_CVTPS2PD,
  299. A_CVTSD2SI,
  300. A_CVTSD2SS,
  301. A_CVTSI2SD,
  302. A_CVTSS2SD,
  303. A_CVTTPD2PI,
  304. A_CVTTPD2DQ,
  305. A_CVTTPS2DQ,
  306. A_CVTTSD2SI,
  307. A_CVTPI2PS,
  308. A_CVTPS2PI,
  309. A_CVTSI2SS,
  310. A_CVTSS2SI,
  311. A_CVTTPS2PI,
  312. A_CVTTSS2SI,
  313. A_XORPD,
  314. A_XORPS,
  315. A_PXOR,
  316. A_PAND,
  317. A_POR,
  318. A_ORPD,
  319. A_ORPS,
  320. A_ANDPD,
  321. A_ANDPS,
  322. A_UNPCKLPS,
  323. A_UNPCKHPS,
  324. A_SHUFPD,
  325. A_SHUFPS,
  326. A_VCOMISD,
  327. A_VCOMISS,
  328. A_MINSS,
  329. A_MINSD,
  330. A_MINPS,
  331. A_MINPD,
  332. A_MAXSS,
  333. A_MAXSD,
  334. A_MAXPS,
  335. A_MAXPD:
  336. replaceoper:=-1;
  337. A_IMUL:
  338. if ops<>3 then
  339. replaceoper:=-1;
  340. {$ifdef x86_64}
  341. A_MOV:
  342. { 64 bit constants can only be moved into registers }
  343. if (oper[0]^.typ=top_const) and
  344. (oper[1]^.typ=top_reg) and
  345. ((oper[0]^.val<low(longint)) or
  346. (oper[0]^.val>high(longint))) then
  347. replaceoper:=-1;
  348. {$endif x86_64}
  349. else
  350. if avx_opcode_only_op0_may_be_memref(opcode) then
  351. replaceoper:=-1;
  352. end;
  353. end;
  354. 2 :
  355. begin
  356. { Some 3-op instructions don't allow memory references
  357. for destination }
  358. case instr.opcode of
  359. A_IMUL:
  360. replaceoper:=-1;
  361. else
  362. if avx_opcode_only_op0_may_be_memref(opcode) then
  363. replaceoper:=-1;
  364. end;
  365. end;
  366. end;
  367. end;
  368. end;
  369. end;
  370. {$ifdef x86_64}
  371. { 32 bit operations on 32 bit registers on x86_64 can result in
  372. zeroing the upper 32 bits of the register. This does not happen
  373. with memory operations, so we have to perform these calculations
  374. in registers. }
  375. if (opsize=S_L) then
  376. replaceoper:=-1;
  377. {$endif x86_64}
  378. { Replace register with spill reference }
  379. if replaceoper<>-1 then
  380. begin
  381. if opcode=A_LEA then
  382. begin
  383. opcode:=A_ADD;
  384. oper[0]^.ref^:=spilltemp;
  385. end
  386. else
  387. begin
  388. is_subh:=getsubreg(oper[replaceoper]^.reg)=R_SUBH;
  389. oper[replaceoper]^.typ:=top_ref;
  390. new(oper[replaceoper]^.ref);
  391. oper[replaceoper]^.ref^:=spilltemp;
  392. if is_subh then
  393. inc(oper[replaceoper]^.ref^.offset);
  394. { memory locations aren't guaranteed to be aligned }
  395. case opcode of
  396. A_MOVAPS:
  397. opcode:=A_MOVSS;
  398. A_MOVAPD:
  399. opcode:=A_MOVSD;
  400. A_VMOVAPS:
  401. opcode:=A_VMOVSS;
  402. A_VMOVAPD:
  403. opcode:=A_VMOVSD;
  404. else
  405. ;
  406. end;
  407. end;
  408. result:=true;
  409. end;
  410. end;
  411. end;
  412. {******************************************************************************
  413. Trgx86fpu
  414. ******************************************************************************}
  415. constructor Trgx86fpu.create;
  416. begin
  417. used_in_proc:=[];
  418. end;
  419. function trgx86fpu.getregisterfpu(list: TAsmList) : tregister;
  420. begin
  421. { note: don't return R_ST0, see comments above implementation of }
  422. { a_loadfpu_* methods in cgcpu (JM) }
  423. result:=NR_ST;
  424. end;
  425. procedure trgx86fpu.ungetregisterfpu(list : TAsmList; r : tregister);
  426. begin
  427. { nothing to do, fpu stack management is handled by the load/ }
  428. { store operations in cgcpu (JM) }
  429. end;
  430. function trgx86fpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  431. begin
  432. correct_fpuregister:=r;
  433. setsupreg(correct_fpuregister,ofs);
  434. end;
  435. procedure trgx86fpu.saveusedfpuregisters(list: TAsmList;
  436. var saved : tpushedsavedfpu;
  437. const s: tcpuregisterset);
  438. { var
  439. r : tregister;
  440. hr : treference; }
  441. begin
  442. used_in_proc:=used_in_proc+s;
  443. { TODO: firstsavefpureg}
  444. (*
  445. { don't try to save the fpu registers if not desired (e.g. for }
  446. { the 80x86) }
  447. if firstsavefpureg <> R_NO then
  448. for r.enum:=firstsavefpureg to lastsavefpureg do
  449. begin
  450. saved[r.enum].ofs:=reg_not_saved;
  451. { if the register is used by the calling subroutine and if }
  452. { it's not a regvar (those are handled separately) }
  453. if not is_reg_var_other[r.enum] and
  454. (r.enum in s) and
  455. { and is present in use }
  456. not(r.enum in unusedregsfpu) then
  457. begin
  458. { then save it }
  459. tg.GetTemp(list,extended_size,tt_persistent,hr);
  460. saved[r.enum].ofs:=hr.offset;
  461. cg.a_loadfpu_reg_ref(list,OS_FLOAT,OS_FLOAT,r,hr);
  462. cg.a_reg_dealloc(list,r);
  463. include(unusedregsfpu,r.enum);
  464. inc(countunusedregsfpu);
  465. end;
  466. end;
  467. *)
  468. end;
  469. procedure trgx86fpu.restoreusedfpuregisters(list : TAsmList;
  470. const saved : tpushedsavedfpu);
  471. {
  472. var
  473. r,r2 : tregister;
  474. hr : treference;
  475. }
  476. begin
  477. { TODO: firstsavefpureg}
  478. (*
  479. if firstsavefpureg <> R_NO then
  480. for r.enum:=lastsavefpureg downto firstsavefpureg do
  481. begin
  482. if saved[r.enum].ofs <> reg_not_saved then
  483. begin
  484. r2.enum:=R_INTREGISTER;
  485. r2.number:=NR_FRAME_POINTER_REG;
  486. reference_reset_base(hr,r2,saved[r.enum].ofs);
  487. cg.a_reg_alloc(list,r);
  488. cg.a_loadfpu_ref_reg(list,OS_FLOAT,OS_FLOAT,hr,r);
  489. if not (r.enum in unusedregsfpu) then
  490. { internalerror(10)
  491. in n386cal we always save/restore the reg *state*
  492. using save/restoreunusedstate -> the current state
  493. may not be real (JM) }
  494. else
  495. begin
  496. dec(countunusedregsfpu);
  497. exclude(unusedregsfpu,r.enum);
  498. end;
  499. tg.UnGetTemp(list,hr);
  500. end;
  501. end;
  502. *)
  503. end;
  504. (*
  505. procedure Trgx86fpu.saveotherregvars(list: TAsmList; const s: totherregisterset);
  506. var
  507. r: Tregister;
  508. begin
  509. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  510. exit;
  511. if firstsavefpureg <> NR_NO then
  512. for r.enum := firstsavefpureg to lastsavefpureg do
  513. if is_reg_var_other[r.enum] and
  514. (r.enum in s) then
  515. store_regvar(list,r);
  516. end;
  517. *)
  518. end.