nx86inl.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nx86inl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. tx86inlinenode = class(tcginlinenode)
  24. protected
  25. procedure maybe_remove_round_trunc_typeconv; virtual;
  26. public
  27. { first pass override
  28. so that the code generator will actually generate
  29. these nodes.
  30. }
  31. function first_pi: tnode ; override;
  32. function first_arctan_real: tnode; override;
  33. function first_abs_real: tnode; override;
  34. function first_sqr_real: tnode; override;
  35. function first_sqrt_real: tnode; override;
  36. function first_ln_real: tnode; override;
  37. function first_cos_real: tnode; override;
  38. function first_sin_real: tnode; override;
  39. function first_round_real: tnode; override;
  40. function first_trunc_real: tnode; override;
  41. function first_popcnt: tnode; override;
  42. function first_fma: tnode; override;
  43. function first_frac_real : tnode; override;
  44. function first_int_real : tnode; override;
  45. function simplify(forinline : boolean) : tnode; override;
  46. { second pass override to generate these nodes }
  47. procedure second_IncludeExclude;override;
  48. procedure second_pi; override;
  49. procedure second_arctan_real; override;
  50. procedure second_abs_real; override;
  51. procedure second_round_real; override;
  52. procedure second_sqr_real; override;
  53. procedure second_sqrt_real; override;
  54. procedure second_ln_real; override;
  55. procedure second_cos_real; override;
  56. procedure second_sin_real; override;
  57. procedure second_trunc_real; override;
  58. procedure second_prefetch;override;
  59. procedure second_abs_long;override;
  60. procedure second_popcnt;override;
  61. procedure second_fma;override;
  62. procedure second_frac_real;override;
  63. procedure second_int_real;override;
  64. private
  65. procedure load_fpu_location(lnode: tnode);
  66. end;
  67. implementation
  68. uses
  69. systems,
  70. globtype,globals,
  71. verbose,compinnr,
  72. defutil,
  73. aasmbase,aasmdata,aasmcpu,
  74. symconst,symtype,symdef,symcpu,
  75. ncnv,
  76. cgbase,pass_1,pass_2,
  77. cpuinfo,cpubase,nutils,
  78. ncal,ncgutil,
  79. tgobj,
  80. cga,cgutils,cgx86,cgobj,hlcgobj;
  81. {*****************************************************************************
  82. TX86INLINENODE
  83. *****************************************************************************}
  84. procedure tx86inlinenode.maybe_remove_round_trunc_typeconv;
  85. begin
  86. { only makes a difference for x86_64 }
  87. end;
  88. function tx86inlinenode.first_pi : tnode;
  89. begin
  90. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  91. begin
  92. expectloc:=LOC_FPUREGISTER;
  93. first_pi := nil;
  94. end
  95. else
  96. result:=inherited;
  97. end;
  98. function tx86inlinenode.first_arctan_real : tnode;
  99. begin
  100. {$ifdef i8086}
  101. { FPATAN's range is limited to (0 <= value < 1) on the 8087 and 80287,
  102. so we need to use the RTL helper on these FPUs }
  103. if current_settings.cputype < cpu_386 then
  104. begin
  105. result := inherited;
  106. exit;
  107. end;
  108. {$endif i8086}
  109. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  110. begin
  111. expectloc:=LOC_FPUREGISTER;
  112. first_arctan_real := nil;
  113. end
  114. else
  115. result:=inherited;
  116. end;
  117. function tx86inlinenode.first_abs_real : tnode;
  118. begin
  119. if use_vectorfpu(resultdef) then
  120. expectloc:=LOC_MMREGISTER
  121. else
  122. expectloc:=LOC_FPUREGISTER;
  123. first_abs_real := nil;
  124. end;
  125. function tx86inlinenode.first_sqr_real : tnode;
  126. begin
  127. if use_vectorfpu(resultdef) then
  128. expectloc:=LOC_MMREGISTER
  129. else
  130. expectloc:=LOC_FPUREGISTER;
  131. first_sqr_real := nil;
  132. end;
  133. function tx86inlinenode.first_sqrt_real : tnode;
  134. begin
  135. if use_vectorfpu(resultdef) then
  136. expectloc:=LOC_MMREGISTER
  137. else
  138. expectloc:=LOC_FPUREGISTER;
  139. first_sqrt_real := nil;
  140. end;
  141. function tx86inlinenode.first_ln_real : tnode;
  142. begin
  143. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  144. begin
  145. expectloc:=LOC_FPUREGISTER;
  146. first_ln_real := nil;
  147. end
  148. else
  149. result:=inherited;
  150. end;
  151. function tx86inlinenode.first_cos_real : tnode;
  152. begin
  153. {$ifdef i8086}
  154. { FCOS is 387+ }
  155. if current_settings.cputype < cpu_386 then
  156. begin
  157. result := inherited;
  158. exit;
  159. end;
  160. {$endif i8086}
  161. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  162. begin
  163. expectloc:=LOC_FPUREGISTER;
  164. result:=nil;
  165. end
  166. else
  167. result:=inherited;
  168. end;
  169. function tx86inlinenode.first_sin_real : tnode;
  170. begin
  171. {$ifdef i8086}
  172. { FSIN is 387+ }
  173. if current_settings.cputype < cpu_386 then
  174. begin
  175. result := inherited;
  176. exit;
  177. end;
  178. {$endif i8086}
  179. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  180. begin
  181. expectloc:=LOC_FPUREGISTER;
  182. result:=nil;
  183. end
  184. else
  185. result:=inherited;
  186. end;
  187. function tx86inlinenode.first_round_real : tnode;
  188. begin
  189. maybe_remove_round_trunc_typeconv;
  190. {$ifdef x86_64}
  191. if use_vectorfpu(left.resultdef) then
  192. expectloc:=LOC_REGISTER
  193. else
  194. {$endif x86_64}
  195. expectloc:=LOC_REFERENCE;
  196. result:=nil;
  197. end;
  198. function tx86inlinenode.first_trunc_real: tnode;
  199. begin
  200. maybe_remove_round_trunc_typeconv;
  201. if (cs_opt_size in current_settings.optimizerswitches)
  202. {$ifdef x86_64}
  203. and not(use_vectorfpu(left.resultdef))
  204. {$endif x86_64}
  205. then
  206. result:=inherited
  207. else
  208. begin
  209. {$ifdef x86_64}
  210. if use_vectorfpu(left.resultdef) then
  211. expectloc:=LOC_REGISTER
  212. else
  213. {$endif x86_64}
  214. expectloc:=LOC_REFERENCE;
  215. result:=nil;
  216. end;
  217. end;
  218. function tx86inlinenode.first_popcnt: tnode;
  219. begin
  220. Result:=nil;
  221. {$ifndef i8086}
  222. if (CPUX86_HAS_POPCNT in cpu_capabilities[current_settings.cputype])
  223. {$ifdef i386}
  224. and not is_64bit(left.resultdef)
  225. {$endif i386}
  226. then
  227. expectloc:=LOC_REGISTER
  228. else
  229. {$endif not i8086}
  230. Result:=inherited first_popcnt
  231. end;
  232. function tx86inlinenode.first_fma : tnode;
  233. begin
  234. {$ifndef i8086}
  235. if ((cpu_capabilities[current_settings.cputype]*[CPUX86_HAS_FMA,CPUX86_HAS_FMA4])<>[]) and
  236. ((is_double(resultdef)) or (is_single(resultdef))) then
  237. begin
  238. expectloc:=LOC_MMREGISTER;
  239. Result:=nil;
  240. end
  241. else
  242. {$endif i8086}
  243. Result:=inherited first_fma;
  244. end;
  245. function tx86inlinenode.first_frac_real : tnode;
  246. begin
  247. if (current_settings.fputype>=fpu_sse41) and
  248. ((is_double(resultdef)) or (is_single(resultdef))) then
  249. begin
  250. maybe_remove_round_trunc_typeconv;
  251. expectloc:=LOC_MMREGISTER;
  252. Result:=nil;
  253. end
  254. else
  255. Result:=inherited first_frac_real;
  256. end;
  257. function tx86inlinenode.first_int_real : tnode;
  258. begin
  259. if (current_settings.fputype>=fpu_sse41) and
  260. ((is_double(resultdef)) or (is_single(resultdef))) then
  261. begin
  262. Result:=nil;
  263. expectloc:=LOC_MMREGISTER;
  264. end
  265. else
  266. Result:=inherited first_int_real;
  267. end;
  268. function tx86inlinenode.simplify(forinline : boolean) : tnode;
  269. var
  270. temp : tnode;
  271. begin
  272. if (current_settings.fputype>=fpu_sse41) and
  273. (inlinenumber=in_int_real) and (left.nodetype=typeconvn) and
  274. not(nf_explicit in left.flags) and
  275. (ttypeconvnode(left).left.resultdef.typ=floatdef) and
  276. ((is_double(ttypeconvnode(left).left.resultdef)) or (is_single(ttypeconvnode(left).left.resultdef))) then
  277. begin
  278. { get rid of the type conversion }
  279. temp:=ttypeconvnode(left).left;
  280. ttypeconvnode(left).left:=nil;
  281. left.free;
  282. left:=temp;
  283. result:=self.getcopy;
  284. tinlinenode(result).resultdef:=temp.resultdef;
  285. typecheckpass(result);
  286. end
  287. else
  288. Result:=inherited simplify(forinline);
  289. end;
  290. procedure tx86inlinenode.second_pi;
  291. begin
  292. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  293. emit_none(A_FLDPI,S_NO);
  294. tcgx86(cg).inc_fpu_stack;
  295. location.register:=NR_FPU_RESULT_REG;
  296. end;
  297. { load the FPU into the an fpu register }
  298. procedure tx86inlinenode.load_fpu_location(lnode: tnode);
  299. begin
  300. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  301. location.register:=NR_FPU_RESULT_REG;
  302. secondpass(lnode);
  303. case lnode.location.loc of
  304. LOC_FPUREGISTER:
  305. ;
  306. LOC_CFPUREGISTER:
  307. begin
  308. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,lnode.location.size,
  309. lnode.location.size,lnode.location.register,location.register);
  310. end;
  311. LOC_REFERENCE,LOC_CREFERENCE:
  312. begin
  313. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  314. lnode.location.size,lnode.location.size,
  315. lnode.location.reference,location.register);
  316. end;
  317. LOC_MMREGISTER,LOC_CMMREGISTER:
  318. begin
  319. location:=lnode.location;
  320. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,location,resultdef,false);
  321. end;
  322. else
  323. internalerror(309991);
  324. end;
  325. end;
  326. procedure tx86inlinenode.second_arctan_real;
  327. begin
  328. load_fpu_location(left);
  329. emit_none(A_FLD1,S_NO);
  330. emit_none(A_FPATAN,S_NO);
  331. end;
  332. procedure tx86inlinenode.second_abs_real;
  333. var
  334. href : treference;
  335. begin
  336. if use_vectorfpu(resultdef) then
  337. begin
  338. secondpass(left);
  339. if left.location.loc<>LOC_MMREGISTER then
  340. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,UseAVX);
  341. if UseAVX then
  342. begin
  343. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  344. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  345. end
  346. else
  347. location:=left.location;
  348. case tfloatdef(resultdef).floattype of
  349. s32real:
  350. begin
  351. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(target_info.cprefix+'FPC_ABSMASK_SINGLE',AT_DATA),0,4,[]);
  352. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  353. if UseAVX then
  354. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_reg(
  355. A_VANDPS,S_XMM,href,left.location.register,location.register))
  356. else
  357. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register));
  358. end;
  359. s64real:
  360. begin
  361. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(target_info.cprefix+'FPC_ABSMASK_DOUBLE',AT_DATA),0,4,[]);
  362. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  363. if UseAVX then
  364. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_reg(
  365. A_VANDPD,S_XMM,href,left.location.register,location.register))
  366. else
  367. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPD,S_XMM,href,location.register))
  368. end;
  369. else
  370. internalerror(200506081);
  371. end;
  372. end
  373. else
  374. begin
  375. load_fpu_location(left);
  376. emit_none(A_FABS,S_NO);
  377. end;
  378. end;
  379. procedure tx86inlinenode.second_round_real;
  380. begin
  381. {$ifdef x86_64}
  382. if use_vectorfpu(left.resultdef) then
  383. begin
  384. secondpass(left);
  385. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  386. location_reset(location,LOC_REGISTER,OS_S64);
  387. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  388. if UseAVX then
  389. case left.location.size of
  390. OS_F32:
  391. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSS2SI,S_NO,left.location.register,location.register));
  392. OS_F64:
  393. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSD2SI,S_NO,left.location.register,location.register));
  394. else
  395. internalerror(2007031402);
  396. end
  397. else
  398. case left.location.size of
  399. OS_F32:
  400. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSS2SI,S_NO,left.location.register,location.register));
  401. OS_F64:
  402. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSD2SI,S_NO,left.location.register,location.register));
  403. else
  404. internalerror(2007031402);
  405. end;
  406. end
  407. else
  408. {$endif x86_64}
  409. begin
  410. load_fpu_location(left);
  411. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  412. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  413. emit_ref(A_FISTP,S_IQ,location.reference);
  414. tcgx86(cg).dec_fpu_stack;
  415. emit_none(A_FWAIT,S_NO);
  416. end;
  417. end;
  418. procedure tx86inlinenode.second_trunc_real;
  419. var
  420. oldcw,newcw : treference;
  421. begin
  422. {$ifdef x86_64}
  423. if use_vectorfpu(left.resultdef) and
  424. not((left.location.loc=LOC_FPUREGISTER) and (current_settings.fputype>=fpu_sse3)) then
  425. begin
  426. secondpass(left);
  427. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  428. location_reset(location,LOC_REGISTER,OS_S64);
  429. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  430. if UseAVX then
  431. case left.location.size of
  432. OS_F32:
  433. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSS2SI,S_NO,left.location.register,location.register));
  434. OS_F64:
  435. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSD2SI,S_NO,left.location.register,location.register));
  436. else
  437. internalerror(2007031401);
  438. end
  439. else
  440. case left.location.size of
  441. OS_F32:
  442. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_NO,left.location.register,location.register));
  443. OS_F64:
  444. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_NO,left.location.register,location.register));
  445. else
  446. internalerror(2007031401);
  447. end;
  448. end
  449. else
  450. {$endif x86_64}
  451. begin
  452. if (current_settings.fputype>=fpu_sse3) then
  453. begin
  454. load_fpu_location(left);
  455. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  456. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  457. emit_ref(A_FISTTP,S_IQ,location.reference);
  458. tcgx86(cg).dec_fpu_stack;
  459. end
  460. else
  461. begin
  462. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,oldcw);
  463. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,newcw);
  464. {$ifdef i8086}
  465. if current_settings.cputype<=cpu_286 then
  466. begin
  467. emit_ref(A_FSTCW,S_NO,newcw);
  468. emit_ref(A_FSTCW,S_NO,oldcw);
  469. emit_none(A_FWAIT,S_NO);
  470. end
  471. else
  472. {$endif i8086}
  473. begin
  474. emit_ref(A_FNSTCW,S_NO,newcw);
  475. emit_ref(A_FNSTCW,S_NO,oldcw);
  476. end;
  477. emit_const_ref(A_OR,S_W,$0f00,newcw);
  478. load_fpu_location(left);
  479. emit_ref(A_FLDCW,S_NO,newcw);
  480. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  481. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  482. emit_ref(A_FISTP,S_IQ,location.reference);
  483. tcgx86(cg).dec_fpu_stack;
  484. emit_ref(A_FLDCW,S_NO,oldcw);
  485. emit_none(A_FWAIT,S_NO);
  486. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  487. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  488. end;
  489. end;
  490. end;
  491. procedure tx86inlinenode.second_sqr_real;
  492. begin
  493. if use_vectorfpu(resultdef) then
  494. begin
  495. secondpass(left);
  496. location_reset(location,LOC_MMREGISTER,left.location.size);
  497. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  498. if UseAVX then
  499. begin
  500. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  501. cg.a_opmm_reg_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location.register,left.location.register,location.register,mms_movescalar);
  502. end
  503. else
  504. begin
  505. if left.location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  506. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  507. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  508. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,location.register,location.register,mms_movescalar);
  509. end;
  510. end
  511. else
  512. begin
  513. load_fpu_location(left);
  514. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  515. end;
  516. end;
  517. procedure tx86inlinenode.second_sqrt_real;
  518. begin
  519. if use_vectorfpu(resultdef) then
  520. begin
  521. secondpass(left);
  522. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  523. location_reset(location,LOC_MMREGISTER,left.location.size);
  524. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  525. if UseAVX then
  526. case tfloatdef(resultdef).floattype of
  527. s32real:
  528. { we use S_NO instead of S_XMM here, regardless of the register size, as the size of the memory location is 32/64 bit }
  529. { using left.location.register here as 2nd parameter is crucial to break dependency chains }
  530. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSS,S_NO,left.location.register,left.location.register,location.register));
  531. s64real:
  532. { we use S_NO instead of S_XMM here, regardless of the register size, as the size of the memory location is 32/64 bit }
  533. { using left.location.register here as 2nd parameter is crucial to break dependency chains }
  534. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSD,S_NO,left.location.register,left.location.register,location.register));
  535. else
  536. internalerror(200510031);
  537. end
  538. else
  539. case tfloatdef(resultdef).floattype of
  540. s32real:
  541. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_NO,left.location.register,location.register));
  542. s64real:
  543. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_NO,left.location.register,location.register));
  544. else
  545. internalerror(200510031);
  546. end;
  547. end
  548. else
  549. begin
  550. load_fpu_location(left);
  551. emit_none(A_FSQRT,S_NO);
  552. end;
  553. end;
  554. procedure tx86inlinenode.second_ln_real;
  555. begin
  556. load_fpu_location(left);
  557. emit_none(A_FLDLN2,S_NO);
  558. emit_none(A_FXCH,S_NO);
  559. emit_none(A_FYL2X,S_NO);
  560. end;
  561. procedure tx86inlinenode.second_cos_real;
  562. begin
  563. {$ifdef i8086}
  564. { FCOS is 387+ }
  565. if current_settings.cputype < cpu_386 then
  566. begin
  567. inherited;
  568. exit;
  569. end;
  570. {$endif i8086}
  571. load_fpu_location(left);
  572. emit_none(A_FCOS,S_NO);
  573. end;
  574. procedure tx86inlinenode.second_sin_real;
  575. begin
  576. {$ifdef i8086}
  577. { FSIN is 387+ }
  578. if current_settings.cputype < cpu_386 then
  579. begin
  580. inherited;
  581. exit;
  582. end;
  583. {$endif i8086}
  584. load_fpu_location(left);
  585. emit_none(A_FSIN,S_NO)
  586. end;
  587. procedure tx86inlinenode.second_prefetch;
  588. var
  589. ref : treference;
  590. r : tregister;
  591. checkpointer_used : boolean;
  592. begin
  593. {$if defined(i386) or defined(i8086)}
  594. if current_settings.cputype>=cpu_Pentium3 then
  595. {$endif i386 or i8086}
  596. begin
  597. { do not call Checkpointer for left node }
  598. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  599. if checkpointer_used then
  600. node_change_local_switch(left,cs_checkpointer,false);
  601. secondpass(left);
  602. if checkpointer_used then
  603. node_change_local_switch(left,cs_checkpointer,false);
  604. case left.location.loc of
  605. LOC_CREFERENCE,
  606. LOC_REFERENCE:
  607. begin
  608. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  609. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  610. reference_reset_base(ref,r,0,left.location.reference.temppos,left.location.reference.alignment,left.location.reference.volatility);
  611. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  612. end;
  613. else
  614. { nothing to prefetch };
  615. end;
  616. end;
  617. end;
  618. procedure tx86inlinenode.second_abs_long;
  619. var
  620. hregister : tregister;
  621. opsize : tcgsize;
  622. hp : taicpu;
  623. begin
  624. {$if defined(i8086) or defined(i386)}
  625. if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  626. begin
  627. opsize:=def_cgsize(left.resultdef);
  628. secondpass(left);
  629. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  630. location:=left.location;
  631. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  632. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  633. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,opsize,tcgsize2size[opsize]*8-1,left.location.register);
  634. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,opsize,left.location.register,location.register);
  635. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,left.location.register,location.register);
  636. end
  637. else
  638. {$endif i8086 or i386}
  639. begin
  640. opsize:=def_cgsize(left.resultdef);
  641. secondpass(left);
  642. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  643. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  644. location:=left.location;
  645. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  646. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hregister);
  647. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  648. emit_reg(A_NEG,tcgsize2opsize[opsize],hregister);
  649. hp:=taicpu.op_reg_reg(A_CMOVcc,tcgsize2opsize[opsize],hregister,location.register);
  650. hp.condition:=C_NS;
  651. current_asmdata.CurrAsmList.concat(hp);
  652. end;
  653. end;
  654. {*****************************************************************************
  655. INCLUDE/EXCLUDE GENERIC HANDLING
  656. *****************************************************************************}
  657. procedure tx86inlinenode.second_IncludeExclude;
  658. var
  659. hregister,
  660. hregister2: tregister;
  661. setbase : aint;
  662. bitsperop,l : longint;
  663. cgop : topcg;
  664. asmop : tasmop;
  665. opdef : tdef;
  666. opsize,
  667. orgsize: tcgsize;
  668. begin
  669. {$ifdef i8086}
  670. { BTS and BTR are 386+ }
  671. if current_settings.cputype < cpu_386 then
  672. begin
  673. inherited;
  674. exit;
  675. end;
  676. {$endif i8086}
  677. if is_smallset(tcallparanode(left).resultdef) then
  678. begin
  679. opdef:=tcallparanode(left).resultdef;
  680. opsize:=int_cgsize(opdef.size)
  681. end
  682. else
  683. begin
  684. opdef:=u32inttype;
  685. opsize:=OS_32;
  686. end;
  687. bitsperop:=(8*tcgsize2size[opsize]);
  688. secondpass(tcallparanode(left).left);
  689. secondpass(tcallparanode(tcallparanode(left).right).left);
  690. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  691. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  692. begin
  693. { calculate bit position }
  694. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  695. { determine operator }
  696. if inlinenumber=in_include_x_y then
  697. cgop:=OP_OR
  698. else
  699. begin
  700. cgop:=OP_AND;
  701. l:=not(l);
  702. end;
  703. case tcallparanode(left).left.location.loc of
  704. LOC_REFERENCE :
  705. begin
  706. inc(tcallparanode(left).left.location.reference.offset,
  707. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  708. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  709. end;
  710. LOC_CREGISTER :
  711. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  712. else
  713. internalerror(200405022);
  714. end;
  715. end
  716. else
  717. begin
  718. orgsize:=opsize;
  719. if opsize in [OS_8,OS_S8] then
  720. begin
  721. opdef:=u32inttype;
  722. opsize:=OS_32;
  723. end;
  724. { determine asm operator }
  725. if inlinenumber=in_include_x_y then
  726. asmop:=A_BTS
  727. else
  728. asmop:=A_BTR;
  729. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,opdef,true);
  730. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.resultdef,tcallparanode(tcallparanode(left).right).left.location,setbase);
  731. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  732. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  733. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  734. else
  735. begin
  736. { second argument can't be an 8 bit register either }
  737. hregister2:=tcallparanode(left).left.location.register;
  738. if (orgsize in [OS_8,OS_S8]) then
  739. hregister2:=cg.makeregsize(current_asmdata.CurrAsmList,hregister2,opsize);
  740. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,hregister2);
  741. end;
  742. end;
  743. end;
  744. procedure tx86inlinenode.second_popcnt;
  745. var
  746. opsize: tcgsize;
  747. begin
  748. secondpass(left);
  749. opsize:=tcgsize2unsigned[left.location.size];
  750. { no 8 Bit popcont }
  751. if opsize=OS_8 then
  752. opsize:=OS_16;
  753. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or
  754. (left.location.size<>opsize) then
  755. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  756. location_reset(location,LOC_REGISTER,opsize);
  757. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  758. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  759. emit_reg_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.register,location.register)
  760. else
  761. emit_ref_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.reference,location.register);
  762. end;
  763. procedure tx86inlinenode.second_fma;
  764. const
  765. op : array[false..true,false..true,s32real..s64real,0..3] of TAsmOp =
  766. (
  767. { positive product }
  768. (
  769. { positive third operand }
  770. ((A_VFMADD231SS,A_VFMADD231SS,A_VFMADD231SS,A_VFMADD213SS),
  771. (A_VFMADD231SD,A_VFMADD231SD,A_VFMADD231SD,A_VFMADD213SD)
  772. ),
  773. { negative third operand }
  774. ((A_VFMSUB231SS,A_VFMSUB231SS,A_VFMSUB231SS,A_VFMSUB213SS),
  775. (A_VFMSUB231SD,A_VFMSUB231SD,A_VFMSUB231SD,A_VFMSUB213SD)
  776. )
  777. ),
  778. { negative product }
  779. (
  780. { positive third operand }
  781. ((A_VFNMADD231SS,A_VFNMADD231SS,A_VFNMADD231SS,A_VFNMADD213SS),
  782. (A_VFNMADD231SD,A_VFNMADD231SD,A_VFNMADD231SD,A_VFNMADD213SD)
  783. ),
  784. { negative third operand }
  785. ((A_VFNMSUB231SS,A_VFNMSUB231SS,A_VFNMSUB231SS,A_VFNMSUB213SS),
  786. (A_VFNMSUB231SD,A_VFNMSUB231SD,A_VFNMSUB231SD,A_VFNMSUB213SD)
  787. )
  788. )
  789. );
  790. var
  791. paraarray : array[1..3] of tnode;
  792. memop,
  793. i : integer;
  794. negop3,
  795. negproduct,
  796. gotmem : boolean;
  797. begin
  798. {$ifndef i8086}
  799. if (cpu_capabilities[current_settings.cputype]*[CPUX86_HAS_FMA,CPUX86_HAS_FMA4])<>[] then
  800. begin
  801. negop3:=false;
  802. negproduct:=false;
  803. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  804. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  805. paraarray[3]:=tcallparanode(parameters).paravalue;
  806. { check if a neg. node can be removed
  807. this is possible because changing the sign of
  808. a floating point number does not affect its absolute
  809. value in any way
  810. }
  811. if paraarray[1].nodetype=unaryminusn then
  812. begin
  813. paraarray[1]:=tunarynode(paraarray[1]).left;
  814. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  815. only no code is generated for it }
  816. negproduct:=not(negproduct);
  817. end;
  818. if paraarray[2].nodetype=unaryminusn then
  819. begin
  820. paraarray[2]:=tunarynode(paraarray[2]).left;
  821. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  822. only no code is generated for it }
  823. negproduct:=not(negproduct);
  824. end;
  825. if paraarray[3].nodetype=unaryminusn then
  826. begin
  827. paraarray[3]:=tunarynode(paraarray[3]).left;
  828. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  829. only no code is generated for it }
  830. negop3:=true;
  831. end;
  832. for i:=1 to 3 do
  833. secondpass(paraarray[i]);
  834. { only one memory operand is allowed }
  835. gotmem:=false;
  836. memop:=0;
  837. for i:=1 to 3 do
  838. begin
  839. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  840. begin
  841. if (paraarray[i].location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not(gotmem) then
  842. begin
  843. memop:=i;
  844. gotmem:=true;
  845. end
  846. else
  847. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  848. end;
  849. end;
  850. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  851. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  852. if gotmem then
  853. begin
  854. case memop of
  855. 1:
  856. begin
  857. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  858. paraarray[3].location.register,location.register,mms_movescalar);
  859. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  860. paraarray[1].location.reference,paraarray[2].location.register,location.register);
  861. end;
  862. 2:
  863. begin
  864. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  865. paraarray[3].location.register,location.register,mms_movescalar);
  866. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  867. paraarray[2].location.reference,paraarray[1].location.register,location.register);
  868. end;
  869. 3:
  870. begin
  871. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[1].resultdef,resultdef,
  872. paraarray[1].location.register,location.register,mms_movescalar);
  873. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  874. paraarray[3].location.reference,paraarray[2].location.register,location.register);
  875. end
  876. else
  877. internalerror(2014041301);
  878. end;
  879. end
  880. else
  881. begin
  882. { try to use the location which is already in a temp. mm register as destination,
  883. so the compiler might be able to re-use the register }
  884. if paraarray[1].location.loc=LOC_MMREGISTER then
  885. begin
  886. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[1].resultdef,resultdef,
  887. paraarray[1].location.register,location.register,mms_movescalar);
  888. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,3],S_NO,
  889. paraarray[3].location.register,paraarray[2].location.register,location.register);
  890. end
  891. else if paraarray[2].location.loc=LOC_MMREGISTER then
  892. begin
  893. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[2].resultdef,resultdef,
  894. paraarray[2].location.register,location.register,mms_movescalar);
  895. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,3],S_NO,
  896. paraarray[3].location.register,paraarray[1].location.register,location.register);
  897. end
  898. else
  899. begin
  900. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  901. paraarray[3].location.register,location.register,mms_movescalar);
  902. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,0],S_NO,
  903. paraarray[1].location.register,paraarray[2].location.register,location.register);
  904. end;
  905. end;
  906. end
  907. else
  908. {$endif i8086}
  909. internalerror(2014032301);
  910. end;
  911. procedure tx86inlinenode.second_frac_real;
  912. var
  913. extrareg : TRegister;
  914. begin
  915. if use_vectorfpu(resultdef) then
  916. begin
  917. secondpass(left);
  918. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  919. location_reset(location,LOC_MMREGISTER,left.location.size);
  920. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  921. if UseAVX then
  922. case tfloatdef(resultdef).floattype of
  923. s32real:
  924. begin
  925. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  926. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSS,S_NO,3,left.location.register,left.location.register,location.register));
  927. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSUBSS,S_NO,location.register,left.location.register,location.register));
  928. end;
  929. s64real:
  930. begin
  931. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  932. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSD,S_NO,3,left.location.register,left.location.register,location.register));
  933. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSUBSD,S_NO,location.register,left.location.register,location.register));
  934. end;
  935. else
  936. internalerror(2017052102);
  937. end
  938. else
  939. begin
  940. extrareg:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  941. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  942. case tfloatdef(resultdef).floattype of
  943. s32real:
  944. begin
  945. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSS,S_NO,3,left.location.register,extrareg));
  946. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBSS,S_NO,extrareg,location.register));
  947. end;
  948. s64real:
  949. begin
  950. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSD,S_NO,3,left.location.register,extrareg));
  951. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBSD,S_NO,extrareg,location.register));
  952. end;
  953. else
  954. internalerror(2017052103);
  955. end;
  956. end;
  957. end
  958. else
  959. internalerror(2017052101);
  960. end;
  961. procedure tx86inlinenode.second_int_real;
  962. var
  963. extrareg : TRegister;
  964. begin
  965. if use_vectorfpu(resultdef) then
  966. begin
  967. secondpass(left);
  968. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  969. location_reset(location,LOC_MMREGISTER,left.location.size);
  970. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  971. if UseAVX then
  972. case tfloatdef(resultdef).floattype of
  973. s32real:
  974. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  975. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSS,S_NO,3,left.location.register,left.location.register,location.register));
  976. s64real:
  977. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  978. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSD,S_NO,3,left.location.register,left.location.register,location.register));
  979. else
  980. internalerror(2017052105);
  981. end
  982. else
  983. begin
  984. case tfloatdef(resultdef).floattype of
  985. s32real:
  986. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSS,S_NO,3,left.location.register,location.register));
  987. s64real:
  988. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSD,S_NO,3,left.location.register,location.register));
  989. else
  990. internalerror(2017052106);
  991. end;
  992. end;
  993. end
  994. else
  995. internalerror(2017052107);
  996. end;
  997. end.