rgx86.pas 21 KB

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