rgx86.pas 19 KB

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