rgx86.pas 21 KB

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