rgx86.pas 20 KB

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