nx86add.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Common code generation for add nodes on the i386 and x86
  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 nx86add;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,
  22. cgbase,
  23. cpubase,
  24. node,nadd,ncgadd;
  25. type
  26. tx86addnode = class(tcgaddnode)
  27. protected
  28. function getresflags(unsigned : boolean) : tresflags;
  29. function getfpuresflags : tresflags;
  30. procedure left_must_be_reg(opdef: tdef; opsize:TCGSize;noswap:boolean);
  31. procedure force_left_and_right_fpureg;
  32. procedure prepare_x87_locations(out refnode: tnode);
  33. procedure emit_op_right_left(op:TAsmOp;opsize:TCgSize);
  34. procedure emit_generic_code(op:TAsmOp;opsize:TCgSize;unsigned,extra_not,mboverflow:boolean);
  35. procedure second_cmpfloatvector;
  36. procedure second_addfloatsse;
  37. procedure second_addfloatavx;
  38. public
  39. function use_fma : boolean;override;
  40. procedure second_addfloat;override;
  41. {$ifndef i8086}
  42. procedure second_addsmallset;override;
  43. {$endif not i8086}
  44. procedure second_add64bit;override;
  45. procedure second_cmpfloat;override;
  46. procedure second_cmpsmallset;override;
  47. procedure second_cmp64bit;override;
  48. procedure second_cmpordinal;override;
  49. procedure second_addordinal;override;
  50. {$ifdef SUPPORT_MMX}
  51. procedure second_opmmx;override;
  52. {$endif SUPPORT_MMX}
  53. procedure second_opvector;override;
  54. end;
  55. implementation
  56. uses
  57. globtype,globals,
  58. verbose,cutils,compinnr,
  59. cpuinfo,
  60. aasmbase,aasmdata,aasmcpu,
  61. symconst,symdef,
  62. cgobj,hlcgobj,cgx86,cga,cgutils,
  63. tgobj,ncgutil,
  64. ncon,nset,ninl,
  65. defutil;
  66. { Range check must be disabled explicitly as the code serves
  67. on three different architecture sizes }
  68. {$R-}
  69. {*****************************************************************************
  70. Helpers
  71. *****************************************************************************}
  72. procedure tx86addnode.emit_generic_code(op:TAsmOp;opsize:TCGSize;unsigned,extra_not,mboverflow:boolean);
  73. var
  74. power : longint;
  75. hl4 : tasmlabel;
  76. r : Tregister;
  77. href : treference;
  78. begin
  79. { at this point, left.location.loc should be LOC_REGISTER }
  80. if right.location.loc=LOC_REGISTER then
  81. begin
  82. { right.location is a LOC_REGISTER }
  83. { when swapped another result register }
  84. if (nodetype=subn) and (nf_swapped in flags) then
  85. begin
  86. if extra_not then
  87. emit_reg(A_NOT,TCGSize2Opsize[opsize],left.location.register);
  88. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,right.location.register);
  89. { newly swapped also set swapped flag }
  90. location_swap(left.location,right.location);
  91. toggleflag(nf_swapped);
  92. end
  93. else
  94. begin
  95. if extra_not then
  96. emit_reg(A_NOT,TCGSize2Opsize[opsize],right.location.register);
  97. if (op=A_ADD) or (op=A_OR) or (op=A_AND) or (op=A_XOR) or (op=A_IMUL) then
  98. location_swap(left.location,right.location);
  99. emit_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register);
  100. end;
  101. end
  102. else
  103. begin
  104. { right.location is not a LOC_REGISTER }
  105. if (nodetype=subn) and (nf_swapped in flags) then
  106. begin
  107. if extra_not then
  108. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,opsize,left.location.register,left.location.register);
  109. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  110. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,cgsize_orddef(opsize),right.location,r);
  111. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,r);
  112. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,r,left.location.register);
  113. end
  114. else
  115. begin
  116. { Optimizations when right.location is a constant value }
  117. if (op=A_CMP) and
  118. (nodetype in [equaln,unequaln]) and
  119. (right.location.loc=LOC_CONSTANT) and
  120. (right.location.value=0) then
  121. begin
  122. { 'test $-1,%reg' is transformable into 'test $-1,spilltemp' if %reg needs
  123. spilling, while 'test %reg,%reg' still requires loading into register.
  124. If spilling is not necessary, it is changed back into 'test %reg,%reg' by
  125. peephole optimizer (this optimization is currently available only for i386). }
  126. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  127. {$ifdef i386}
  128. emit_const_reg(A_TEST,TCGSize2Opsize[opsize],aint(-1),left.location.register)
  129. {$else i386}
  130. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  131. {$endif i386}
  132. end
  133. else
  134. if (op=A_ADD) and
  135. (right.location.loc=LOC_CONSTANT) and
  136. (right.location.value=1) and
  137. not(cs_check_overflow in current_settings.localswitches) and
  138. UseIncDec then
  139. begin
  140. emit_reg(A_INC,TCGSize2Opsize[opsize],left.location.register);
  141. end
  142. else
  143. if (op=A_SUB) and
  144. (right.location.loc=LOC_CONSTANT) and
  145. (right.location.value=1) and
  146. not(cs_check_overflow in current_settings.localswitches) and
  147. UseIncDec then
  148. begin
  149. emit_reg(A_DEC,TCGSize2Opsize[opsize],left.location.register);
  150. end
  151. else
  152. if (op=A_IMUL) and
  153. (right.location.loc=LOC_CONSTANT) and
  154. (ispowerof2(int64(right.location.value),power)) and
  155. not(cs_check_overflow in current_settings.localswitches) then
  156. begin
  157. emit_const_reg(A_SHL,TCGSize2Opsize[opsize],power,left.location.register);
  158. end
  159. else if (op=A_IMUL) and
  160. (right.location.loc=LOC_CONSTANT) and
  161. (right.location.value>1) and (ispowerof2(int64(right.location.value)-1,power)) and
  162. (power in [1..3]) and
  163. not(cs_check_overflow in current_settings.localswitches) then
  164. begin
  165. reference_reset_base(href,left.location.register,0,ctempposinvalid,0,[]);
  166. href.index:=left.location.register;
  167. href.scalefactor:=int64(right.location.value)-1;
  168. left.location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  169. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[opsize],href,left.location.register));
  170. end
  171. else
  172. begin
  173. if extra_not then
  174. begin
  175. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  176. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,cgsize_orddef(opsize),right.location,r);
  177. emit_reg(A_NOT,TCGSize2Opsize[opsize],r);
  178. emit_reg_reg(A_AND,TCGSize2Opsize[opsize],r,left.location.register);
  179. end
  180. else
  181. begin
  182. emit_op_right_left(op,opsize);
  183. end;
  184. end;
  185. end;
  186. end;
  187. { only in case of overflow operations }
  188. { produce overflow code }
  189. { we must put it here directly, because sign of operation }
  190. { is in unsigned VAR!! }
  191. if mboverflow then
  192. begin
  193. if cs_check_overflow in current_settings.localswitches then
  194. begin
  195. current_asmdata.getjumplabel(hl4);
  196. if unsigned then
  197. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  198. else
  199. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  200. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  201. cg.a_label(current_asmdata.CurrAsmList,hl4);
  202. end;
  203. end;
  204. end;
  205. procedure tx86addnode.left_must_be_reg(opdef: tdef; opsize:TCGSize;noswap:boolean);
  206. begin
  207. { left location is not a register? }
  208. if (left.location.loc<>LOC_REGISTER) then
  209. begin
  210. { if right is register then we can swap the locations }
  211. if (not noswap) and
  212. (right.location.loc=LOC_REGISTER) then
  213. begin
  214. location_swap(left.location,right.location);
  215. toggleflag(nf_swapped);
  216. end
  217. else if (not noswap) and
  218. (right.location.loc=LOC_CREGISTER) then
  219. begin
  220. location_swap(left.location,right.location);
  221. toggleflag(nf_swapped);
  222. { maybe we can reuse a constant register when the
  223. operation is a comparison that doesn't change the
  224. value of the register }
  225. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  226. location:=left.location;
  227. end
  228. else
  229. begin
  230. { maybe we can reuse a constant register when the
  231. operation is a comparison that doesn't change the
  232. value of the register }
  233. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  234. end;
  235. end;
  236. if (right.location.loc<>LOC_CONSTANT) and
  237. (tcgsize2unsigned[right.location.size]<>tcgsize2unsigned[opsize]) then
  238. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  239. if (left.location.loc<>LOC_CONSTANT) and
  240. (tcgsize2unsigned[left.location.size]<>tcgsize2unsigned[opsize]) then
  241. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  242. end;
  243. procedure tx86addnode.force_left_and_right_fpureg;
  244. begin
  245. if (right.location.loc<>LOC_FPUREGISTER) then
  246. begin
  247. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,false);
  248. if (left.location.loc<>LOC_FPUREGISTER) then
  249. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false)
  250. else
  251. { left was on the stack => swap }
  252. toggleflag(nf_swapped);
  253. end
  254. { the nominator in st0 }
  255. else if (left.location.loc<>LOC_FPUREGISTER) then
  256. begin
  257. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false)
  258. end
  259. else
  260. begin
  261. { fpu operands are always in the wrong order on the stack }
  262. toggleflag(nf_swapped);
  263. end;
  264. end;
  265. { Makes sides suitable for executing an x87 instruction:
  266. if either side is OS_F32/OS_F64-sized LOC_REFERENCE, it is returned in 'refnode'
  267. everything else is loaded to FPU stack. }
  268. procedure tx86addnode.prepare_x87_locations(out refnode: tnode);
  269. begin
  270. refnode:=nil;
  271. { later on, no mm registers are allowed, so transfer everything to memory here
  272. below it is loaded into an fpu register if neede }
  273. if left.location.loc in [LOC_CMMREGISTER,LOC_MMREGISTER] then
  274. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  275. if right.location.loc in [LOC_CMMREGISTER,LOC_MMREGISTER] then
  276. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  277. case ord(left.location.loc=LOC_FPUREGISTER)+ord(right.location.loc=LOC_FPUREGISTER) of
  278. 0:
  279. begin
  280. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,false);
  281. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  282. InternalError(2013090803);
  283. if (left.location.size in [OS_F32,OS_F64]) then
  284. begin
  285. refnode:=left;
  286. toggleflag(nf_swapped);
  287. end
  288. else
  289. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  290. end;
  291. 1:
  292. begin { if left is on the stack then swap. }
  293. if (left.location.loc=LOC_FPUREGISTER) then
  294. refnode:=right
  295. else
  296. refnode:=left;
  297. if not(refnode.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  298. InternalError(2013090801);
  299. if not (refnode.location.size in [OS_F32,OS_F64]) then
  300. begin
  301. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,refnode.location,refnode.resultdef,false);
  302. if (refnode=right) then
  303. toggleflag(nf_swapped);
  304. refnode:=nil;
  305. end
  306. else
  307. begin
  308. if (refnode=left) then
  309. toggleflag(nf_swapped);
  310. end;
  311. end;
  312. 2: { fpu operands are always in the wrong order on the stack }
  313. toggleflag(nf_swapped);
  314. else
  315. InternalError(2013090802);
  316. end;
  317. end;
  318. procedure tx86addnode.emit_op_right_left(op:TAsmOp;opsize:TCgsize);
  319. {$ifdef x86_64}
  320. var
  321. tmpreg : tregister;
  322. {$endif x86_64}
  323. begin
  324. if (right.location.loc in [LOC_CSUBSETREG,LOC_SUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  325. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  326. { left must be a register }
  327. case right.location.loc of
  328. LOC_REGISTER,
  329. LOC_CREGISTER :
  330. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register));
  331. LOC_REFERENCE,
  332. LOC_CREFERENCE :
  333. begin
  334. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  335. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,TCGSize2Opsize[opsize],right.location.reference,left.location.register));
  336. end;
  337. LOC_CONSTANT :
  338. begin
  339. {$ifdef x86_64}
  340. { x86_64 only supports signed 32 bits constants directly }
  341. if (opsize in [OS_S64,OS_64]) and
  342. ((right.location.value<low(longint)) or (right.location.value>high(longint))) then
  343. begin
  344. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  345. cg.a_load_const_reg(current_asmdata.CurrAsmList,opsize,right.location.value,tmpreg);
  346. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],tmpreg,left.location.register));
  347. end
  348. else
  349. {$endif x86_64}
  350. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(op,TCGSize2Opsize[opsize],right.location.value,left.location.register));
  351. end;
  352. else
  353. internalerror(200203232);
  354. end;
  355. end;
  356. function tx86addnode.getresflags(unsigned : boolean) : tresflags;
  357. begin
  358. case nodetype of
  359. equaln : getresflags:=F_E;
  360. unequaln : getresflags:=F_NE;
  361. else
  362. if not(unsigned) then
  363. begin
  364. if nf_swapped in flags then
  365. case nodetype of
  366. ltn : getresflags:=F_G;
  367. lten : getresflags:=F_GE;
  368. gtn : getresflags:=F_L;
  369. gten : getresflags:=F_LE;
  370. else
  371. internalerror(2013120105);
  372. end
  373. else
  374. case nodetype of
  375. ltn : getresflags:=F_L;
  376. lten : getresflags:=F_LE;
  377. gtn : getresflags:=F_G;
  378. gten : getresflags:=F_GE;
  379. else
  380. internalerror(2013120106);
  381. end;
  382. end
  383. else
  384. begin
  385. if nf_swapped in flags then
  386. case nodetype of
  387. ltn : getresflags:=F_A;
  388. lten : getresflags:=F_AE;
  389. gtn : getresflags:=F_B;
  390. gten : getresflags:=F_BE;
  391. else
  392. internalerror(2013120107);
  393. end
  394. else
  395. case nodetype of
  396. ltn : getresflags:=F_B;
  397. lten : getresflags:=F_BE;
  398. gtn : getresflags:=F_A;
  399. gten : getresflags:=F_AE;
  400. else
  401. internalerror(2013120108);
  402. end;
  403. end;
  404. end;
  405. end;
  406. function tx86addnode.getfpuresflags : tresflags;
  407. begin
  408. if (nodetype=equaln) then
  409. result:=F_FE
  410. else if (nodetype=unequaln) then
  411. result:=F_FNE
  412. else if (nf_swapped in flags) then
  413. case nodetype of
  414. ltn : result:=F_FA;
  415. lten : result:=F_FAE;
  416. gtn : result:=F_FB;
  417. gten : result:=F_FBE;
  418. else
  419. internalerror(2014031402);
  420. end
  421. else
  422. case nodetype of
  423. ltn : result:=F_FB;
  424. lten : result:=F_FBE;
  425. gtn : result:=F_FA;
  426. gten : result:=F_FAE;
  427. else
  428. internalerror(2014031403);
  429. end;
  430. end;
  431. {*****************************************************************************
  432. AddSmallSet
  433. *****************************************************************************}
  434. {$ifndef i8086}
  435. procedure tx86addnode.second_addsmallset;
  436. var
  437. setbase : aint;
  438. opdef : tdef;
  439. opsize : TCGSize;
  440. op : TAsmOp;
  441. extra_not,
  442. noswap : boolean;
  443. all_member_optimization:boolean;
  444. begin
  445. pass_left_right;
  446. noswap:=false;
  447. extra_not:=false;
  448. all_member_optimization:=false;
  449. opdef:=resultdef;
  450. opsize:=int_cgsize(opdef.size);
  451. if (left.resultdef.typ=setdef) then
  452. setbase:=tsetdef(left.resultdef).setbase
  453. else
  454. setbase:=tsetdef(right.resultdef).setbase;
  455. case nodetype of
  456. addn :
  457. begin
  458. { adding elements is not commutative }
  459. if (nf_swapped in flags) and (left.nodetype=setelementn) then
  460. swapleftright;
  461. { are we adding set elements ? }
  462. if right.nodetype=setelementn then
  463. begin
  464. { no range support for smallsets! }
  465. if assigned(tsetelementnode(right).right) then
  466. internalerror(43244);
  467. { btsb isn't supported }
  468. if opsize=OS_8 then
  469. begin
  470. opsize:=OS_32;
  471. opdef:=u32inttype;
  472. end;
  473. { bts requires both elements to be registers }
  474. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  475. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  476. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,right.location,setbase);
  477. op:=A_BTS;
  478. noswap:=true;
  479. end
  480. else
  481. op:=A_OR;
  482. end;
  483. symdifn :
  484. op:=A_XOR;
  485. muln :
  486. op:=A_AND;
  487. subn :
  488. begin
  489. op:=A_AND;
  490. if (not(nf_swapped in flags) and (left.location.loc=LOC_CONSTANT) and (left.location.value=-1)) or
  491. ((nf_swapped in flags) and (right.location.loc=LOC_CONSTANT) and (right.location.value=-1)) then
  492. all_member_optimization:=true;
  493. if (not(nf_swapped in flags)) and
  494. (right.location.loc=LOC_CONSTANT) then
  495. right.location.value := not(right.location.value)
  496. else if (nf_swapped in flags) and
  497. (left.location.loc=LOC_CONSTANT) then
  498. left.location.value := not(left.location.value)
  499. else
  500. extra_not:=true;
  501. end;
  502. xorn :
  503. op:=A_XOR;
  504. orn :
  505. op:=A_OR;
  506. andn :
  507. op:=A_AND;
  508. else
  509. internalerror(2003042215);
  510. end;
  511. if all_member_optimization then
  512. begin
  513. {A set expression [0..31]-x can be implemented with a simple NOT.}
  514. if nf_swapped in flags then
  515. begin
  516. { newly swapped also set swapped flag }
  517. location_swap(left.location,right.location);
  518. toggleflag(nf_swapped);
  519. end;
  520. hlcg.location_force_reg(current_asmdata.currAsmList,right.location,right.resultdef,opdef,false);
  521. emit_reg(A_NOT,TCGSize2Opsize[opsize],right.location.register);
  522. location:=right.location;
  523. end
  524. else
  525. begin
  526. { can we use the BMI1 instruction andn? }
  527. if (op=A_AND) and extra_not and (CPUX86_HAS_BMI1 in cpu_capabilities[current_settings.cputype]) and
  528. (resultdef.size in [4{$ifdef x86_64},8{$endif x86_64}]) then
  529. begin
  530. location_reset(location,LOC_REGISTER,left.location.size);
  531. location.register:=cg.getintregister(current_asmdata.currAsmList,left.location.size);
  532. if nf_swapped in flags then
  533. begin
  534. location_swap(left.location,right.location);
  535. toggleflag(nf_swapped);
  536. end;
  537. hlcg.location_force_reg(current_asmdata.currAsmList,right.location,right.resultdef,opdef,true);
  538. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE]) then
  539. hlcg.location_force_reg(current_asmdata.currAsmList,left.location,left.resultdef,opdef,true);
  540. case left.location.loc of
  541. LOC_CREGISTER,LOC_REGISTER:
  542. emit_reg_reg_reg(A_ANDN,TCGSize2Opsize[opsize],left.location.register,right.location.register,location.register);
  543. LOC_CREFERENCE,LOC_REFERENCE:
  544. emit_ref_reg_reg(A_ANDN,TCGSize2Opsize[opsize],left.location.reference,right.location.register,location.register);
  545. else
  546. Internalerror(2018040201);
  547. end;
  548. end
  549. else
  550. begin
  551. { left must be a register }
  552. left_must_be_reg(opdef,opsize,noswap);
  553. emit_generic_code(op,opsize,true,extra_not,false);
  554. location_freetemp(current_asmdata.CurrAsmList,right.location);
  555. { left is always a register and contains the result }
  556. location:=left.location;
  557. end;
  558. end;
  559. { fix the changed opsize we did above because of the missing btsb }
  560. if opsize<>int_cgsize(resultdef.size) then
  561. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,opdef,cgsize_orddef(int_cgsize(resultdef.size)),false);
  562. end;
  563. {$endif not i8086}
  564. procedure tx86addnode.second_cmpsmallset;
  565. var
  566. opdef : tdef;
  567. opsize : TCGSize;
  568. op : TAsmOp;
  569. begin
  570. pass_left_right;
  571. opdef:=left.resultdef;
  572. opsize:=int_cgsize(opdef.size);
  573. case nodetype of
  574. equaln,
  575. unequaln :
  576. op:=A_CMP;
  577. lten,gten:
  578. begin
  579. if (not(nf_swapped in flags) and (nodetype = lten)) or
  580. ((nf_swapped in flags) and (nodetype = gten)) then
  581. swapleftright;
  582. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  583. emit_op_right_left(A_AND,opsize);
  584. op:=A_CMP;
  585. { warning: ugly hack, we need a JE so change the node to equaln }
  586. nodetype:=equaln;
  587. end;
  588. else
  589. internalerror(2003042215);
  590. end;
  591. { left must be a register }
  592. left_must_be_reg(opdef,opsize,false);
  593. emit_generic_code(op,opsize,true,false,false);
  594. location_freetemp(current_asmdata.CurrAsmList,right.location);
  595. location_freetemp(current_asmdata.CurrAsmList,left.location);
  596. location_reset(location,LOC_FLAGS,OS_NO);
  597. location.resflags:=getresflags(true);
  598. end;
  599. {*****************************************************************************
  600. AddMMX
  601. *****************************************************************************}
  602. {$ifdef SUPPORT_MMX}
  603. procedure tx86addnode.second_opmmx;
  604. var
  605. op : TAsmOp;
  606. cmpop : boolean;
  607. mmxbase : tmmxtype;
  608. hreg,
  609. hregister : tregister;
  610. begin
  611. pass_left_right;
  612. cmpop:=false;
  613. op:=A_NOP;
  614. mmxbase:=mmx_type(left.resultdef);
  615. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  616. case nodetype of
  617. addn :
  618. begin
  619. if (cs_mmx_saturation in current_settings.localswitches) then
  620. begin
  621. case mmxbase of
  622. mmxs8bit:
  623. op:=A_PADDSB;
  624. mmxu8bit:
  625. op:=A_PADDUSB;
  626. mmxs16bit,mmxfixed16:
  627. op:=A_PADDSW;
  628. mmxu16bit:
  629. op:=A_PADDUSW;
  630. end;
  631. end
  632. else
  633. begin
  634. case mmxbase of
  635. mmxs8bit,mmxu8bit:
  636. op:=A_PADDB;
  637. mmxs16bit,mmxu16bit,mmxfixed16:
  638. op:=A_PADDW;
  639. mmxs32bit,mmxu32bit:
  640. op:=A_PADDD;
  641. end;
  642. end;
  643. end;
  644. muln :
  645. begin
  646. case mmxbase of
  647. mmxs16bit,mmxu16bit:
  648. op:=A_PMULLW;
  649. mmxfixed16:
  650. op:=A_PMULHW;
  651. end;
  652. end;
  653. subn :
  654. begin
  655. if (cs_mmx_saturation in current_settings.localswitches) then
  656. begin
  657. case mmxbase of
  658. mmxs8bit:
  659. op:=A_PSUBSB;
  660. mmxu8bit:
  661. op:=A_PSUBUSB;
  662. mmxs16bit,mmxfixed16:
  663. op:=A_PSUBSB;
  664. mmxu16bit:
  665. op:=A_PSUBUSW;
  666. end;
  667. end
  668. else
  669. begin
  670. case mmxbase of
  671. mmxs8bit,mmxu8bit:
  672. op:=A_PSUBB;
  673. mmxs16bit,mmxu16bit,mmxfixed16:
  674. op:=A_PSUBW;
  675. mmxs32bit,mmxu32bit:
  676. op:=A_PSUBD;
  677. end;
  678. end;
  679. end;
  680. xorn:
  681. op:=A_PXOR;
  682. orn:
  683. op:=A_POR;
  684. andn:
  685. op:=A_PAND;
  686. else
  687. internalerror(2003042214);
  688. end;
  689. if op = A_NOP then
  690. internalerror(201408201);
  691. { left and right no register? }
  692. { then one must be demanded }
  693. if (left.location.loc<>LOC_MMXREGISTER) then
  694. begin
  695. if (right.location.loc=LOC_MMXREGISTER) then
  696. begin
  697. location_swap(left.location,right.location);
  698. toggleflag(nf_swapped);
  699. end
  700. else
  701. begin
  702. { register variable ? }
  703. if (left.location.loc=LOC_CMMXREGISTER) then
  704. begin
  705. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  706. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  707. end
  708. else
  709. begin
  710. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  711. internalerror(200203245);
  712. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  713. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  714. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  715. end;
  716. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  717. left.location.register:=hregister;
  718. end;
  719. end;
  720. { at this point, left.location.loc should be LOC_MMXREGISTER }
  721. if right.location.loc<>LOC_MMXREGISTER then
  722. begin
  723. if (nodetype=subn) and (nf_swapped in flags) then
  724. begin
  725. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  726. if right.location.loc=LOC_CMMXREGISTER then
  727. begin
  728. emit_reg_reg(A_MOVQ,S_NO,right.location.register,hreg);
  729. emit_reg_reg(op,S_NO,left.location.register,hreg);
  730. end
  731. else
  732. begin
  733. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  734. internalerror(200203247);
  735. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  736. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,hreg);
  737. emit_reg_reg(op,S_NO,left.location.register,hreg);
  738. end;
  739. location.register:=hreg;
  740. end
  741. else
  742. begin
  743. if (right.location.loc=LOC_CMMXREGISTER) then
  744. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  745. else
  746. begin
  747. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  748. internalerror(200203246);
  749. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  750. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  751. end;
  752. location.register:=left.location.register;
  753. end;
  754. end
  755. else
  756. begin
  757. { right.location=LOC_MMXREGISTER }
  758. if (nodetype=subn) and (nf_swapped in flags) then
  759. begin
  760. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  761. location_swap(left.location,right.location);
  762. toggleflag(nf_swapped);
  763. end
  764. else
  765. begin
  766. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  767. end;
  768. location.register:=left.location.register;
  769. end;
  770. location_freetemp(current_asmdata.CurrAsmList,right.location);
  771. if cmpop then
  772. location_freetemp(current_asmdata.CurrAsmList,left.location);
  773. end;
  774. {$endif SUPPORT_MMX}
  775. {*****************************************************************************
  776. AddFloat
  777. *****************************************************************************}
  778. procedure tx86addnode.second_addfloatsse;
  779. var
  780. op : topcg;
  781. sqr_sum : boolean;
  782. tmp : tnode;
  783. begin
  784. sqr_sum:=false;
  785. if (current_settings.fputype>=fpu_sse3) and
  786. use_vectorfpu(resultdef) and
  787. (nodetype in [addn,subn]) and
  788. (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) and
  789. (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  790. begin
  791. sqr_sum:=true;
  792. tmp:=tinlinenode(left).left;
  793. tinlinenode(left).left:=nil;
  794. left.free;
  795. left:=tmp;
  796. tmp:=tinlinenode(right).left;
  797. tinlinenode(right).left:=nil;
  798. right.free;
  799. right:=tmp;
  800. end;
  801. pass_left_right;
  802. { fpu operands are always in reversed order on the stack }
  803. if (left.location.loc=LOC_FPUREGISTER) and (right.location.loc=LOC_FPUREGISTER) then
  804. toggleflag(nf_swapped);
  805. if (nf_swapped in flags) then
  806. { can't use swapleftright if both are on the fpu stack, since then }
  807. { both are "R_ST" -> nothing would change -> manually switch }
  808. if (left.location.loc = LOC_FPUREGISTER) and
  809. (right.location.loc = LOC_FPUREGISTER) then
  810. emit_none(A_FXCH,S_NO)
  811. else
  812. swapleftright;
  813. case nodetype of
  814. addn :
  815. op:=OP_ADD;
  816. muln :
  817. op:=OP_MUL;
  818. subn :
  819. op:=OP_SUB;
  820. slashn :
  821. op:=OP_DIV;
  822. else
  823. internalerror(200312231);
  824. end;
  825. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  826. if sqr_sum then
  827. begin
  828. if nf_swapped in flags then
  829. swapleftright;
  830. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  831. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  832. location:=left.location;
  833. if is_double(resultdef) then
  834. begin
  835. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,right.location.register,location.register));
  836. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPD,S_NO,location.register,location.register));
  837. case nodetype of
  838. addn:
  839. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPD,S_NO,location.register,location.register));
  840. subn:
  841. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPD,S_NO,location.register,location.register));
  842. else
  843. internalerror(201108162);
  844. end;
  845. end
  846. else
  847. begin
  848. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_UNPCKLPS,S_NO,right.location.register,location.register));
  849. { ensure that bits 64..127 contain valid values }
  850. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,location.register,location.register));
  851. { the data is now in bits 0..32 and 64..95 }
  852. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPS,S_NO,location.register,location.register));
  853. case nodetype of
  854. addn:
  855. begin
  856. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPS,S_NO,location.register,location.register));
  857. end;
  858. subn:
  859. begin
  860. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPS,S_NO,location.register,location.register));
  861. end;
  862. else
  863. internalerror(201108163);
  864. end;
  865. end
  866. end
  867. { we can use only right as left operand if the operation is commutative }
  868. else if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  869. begin
  870. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  871. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,location.size,right.location.register,location.register,mms_movescalar);
  872. { force floating point reg. location to be written to memory,
  873. we don't force it to mm register because writing to memory
  874. allows probably shorter code because there is no direct fpu->mm register
  875. copy instruction
  876. }
  877. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  878. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  879. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,left.location,location.register,mms_movescalar);
  880. end
  881. else
  882. begin
  883. if nf_swapped in flags then
  884. swapleftright;
  885. { force floating point reg. location to be written to memory,
  886. we don't force it to mm register because writing to memory
  887. allows probably shorter code because there is no direct fpu->mm register
  888. copy instruction
  889. }
  890. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  891. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  892. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  893. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  894. { force floating point reg. location to be written to memory,
  895. we don't force it to mm register because writing to memory
  896. allows probably shorter code because there is no direct fpu->mm register
  897. copy instruction
  898. }
  899. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  900. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  901. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,location.register,mms_movescalar);
  902. end;
  903. end;
  904. procedure tx86addnode.second_addfloatavx;
  905. var
  906. op : topcg;
  907. sqr_sum : boolean;
  908. {$ifdef dummy}
  909. tmp : tnode;
  910. {$endif dummy}
  911. begin
  912. sqr_sum:=false;
  913. {$ifdef dummy}
  914. if (current_settings.fputype>=fpu_sse3) and
  915. use_vectorfpu(resultdef) and
  916. (nodetype in [addn,subn]) and
  917. (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) and
  918. (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  919. begin
  920. sqr_sum:=true;
  921. tmp:=tinlinenode(left).left;
  922. tinlinenode(left).left:=nil;
  923. left.free;
  924. left:=tmp;
  925. tmp:=tinlinenode(right).left;
  926. tinlinenode(right).left:=nil;
  927. right.free;
  928. right:=tmp;
  929. end;
  930. {$endif dummy}
  931. pass_left_right;
  932. { fpu operands are always in reversed order on the stack }
  933. if (left.location.loc=LOC_FPUREGISTER) and (right.location.loc=LOC_FPUREGISTER) then
  934. toggleflag(nf_swapped);
  935. if (nf_swapped in flags) then
  936. { can't use swapleftright if both are on the fpu stack, since then }
  937. { both are "R_ST" -> nothing would change -> manually switch }
  938. if (left.location.loc = LOC_FPUREGISTER) and
  939. (right.location.loc = LOC_FPUREGISTER) then
  940. emit_none(A_FXCH,S_NO)
  941. else
  942. swapleftright;
  943. case nodetype of
  944. addn :
  945. op:=OP_ADD;
  946. muln :
  947. op:=OP_MUL;
  948. subn :
  949. op:=OP_SUB;
  950. slashn :
  951. op:=OP_DIV;
  952. else
  953. internalerror(200312231);
  954. end;
  955. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  956. if sqr_sum then
  957. begin
  958. if nf_swapped in flags then
  959. swapleftright;
  960. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  961. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  962. location:=left.location;
  963. if is_double(resultdef) then
  964. begin
  965. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,right.location.register,location.register));
  966. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPD,S_NO,location.register,location.register));
  967. case nodetype of
  968. addn:
  969. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPD,S_NO,location.register,location.register));
  970. subn:
  971. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPD,S_NO,location.register,location.register));
  972. else
  973. internalerror(201108162);
  974. end;
  975. end
  976. else
  977. begin
  978. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_UNPCKLPS,S_NO,right.location.register,location.register));
  979. { ensure that bits 64..127 contain valid values }
  980. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,location.register,location.register));
  981. { the data is now in bits 0..32 and 64..95 }
  982. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPS,S_NO,location.register,location.register));
  983. case nodetype of
  984. addn:
  985. begin
  986. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPS,S_NO,location.register,location.register));
  987. end;
  988. subn:
  989. begin
  990. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPS,S_NO,location.register,location.register));
  991. end;
  992. else
  993. internalerror(201108163);
  994. end;
  995. end
  996. end
  997. { left*2 ? }
  998. else if (nodetype=muln) and is_constrealnode(right) and is_number_float(trealconstnode(right).value_real) and (trealconstnode(right).value_real=2) then
  999. begin
  1000. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  1001. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  1002. cg.a_opmm_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,location.size,
  1003. left.location.register,
  1004. left.location.register,
  1005. location.register,
  1006. mms_movescalar);
  1007. end
  1008. { right*2 ? }
  1009. else if (nodetype=muln) and is_constrealnode(left) and is_number_float(trealconstnode(left).value_real) and (trealconstnode(left).value_real=2) then
  1010. begin
  1011. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,right.location.size);
  1012. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  1013. cg.a_opmm_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,location.size,
  1014. right.location.register,
  1015. right.location.register,
  1016. location.register,
  1017. mms_movescalar);
  1018. end
  1019. { we can use only right as left operand if the operation is commutative }
  1020. else if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  1021. begin
  1022. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  1023. { force floating point reg. location to be written to memory,
  1024. we don't force it to mm register because writing to memory
  1025. allows probably shorter code because there is no direct fpu->mm register
  1026. copy instruction
  1027. }
  1028. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  1029. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  1030. cg.a_opmm_loc_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  1031. left.location,
  1032. right.location.register,
  1033. location.register,
  1034. mms_movescalar);
  1035. end
  1036. else
  1037. begin
  1038. if (nf_swapped in flags) then
  1039. swapleftright;
  1040. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  1041. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  1042. { force floating point reg. location to be written to memory,
  1043. we don't force it to mm register because writing to memory
  1044. allows probably shorter code because there is no direct fpu->mm register
  1045. copy instruction
  1046. }
  1047. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  1048. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  1049. cg.a_opmm_loc_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  1050. right.location,
  1051. left.location.register,
  1052. location.register,
  1053. mms_movescalar);
  1054. end;
  1055. end;
  1056. function tx86addnode.use_fma : boolean;
  1057. begin
  1058. {$ifndef i8086}
  1059. { test if the result stays in an xmm register, fiddeling with fpu registers and fma makes no sense }
  1060. Result:=use_vectorfpu(resultdef) and
  1061. ((cpu_capabilities[current_settings.cputype]*[CPUX86_HAS_FMA,CPUX86_HAS_FMA4])<>[]);
  1062. {$else i8086}
  1063. Result:=inherited use_fma;
  1064. {$endif i8086}
  1065. end;
  1066. procedure tx86addnode.second_cmpfloatvector;
  1067. var
  1068. op : tasmop;
  1069. const
  1070. ops_single: array[boolean] of tasmop = (A_COMISS,A_VCOMISS);
  1071. ops_double: array[boolean] of tasmop = (A_COMISD,A_VCOMISD);
  1072. begin
  1073. if is_single(left.resultdef) then
  1074. op:=ops_single[UseAVX]
  1075. else if is_double(left.resultdef) then
  1076. op:=ops_double[UseAVX]
  1077. else
  1078. internalerror(200402222);
  1079. pass_left_right;
  1080. location_reset(location,LOC_FLAGS,OS_NO);
  1081. { Direct move fpu->mm register is not possible, so force any fpu operands to
  1082. memory (not to mm registers because one of the memory locations can be used
  1083. directly in compare instruction, yielding shorter code) }
  1084. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  1085. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  1086. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  1087. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  1088. if (right.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  1089. begin
  1090. case left.location.loc of
  1091. LOC_REFERENCE,LOC_CREFERENCE:
  1092. begin
  1093. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  1094. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,left.location.reference,right.location.register));
  1095. end;
  1096. LOC_MMREGISTER,LOC_CMMREGISTER:
  1097. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,left.location.register,right.location.register));
  1098. else
  1099. internalerror(200402221);
  1100. end;
  1101. toggleflag(nf_swapped);
  1102. end
  1103. else
  1104. begin
  1105. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  1106. case right.location.loc of
  1107. LOC_REFERENCE,LOC_CREFERENCE:
  1108. begin
  1109. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  1110. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,right.location.reference,left.location.register));
  1111. end;
  1112. LOC_MMREGISTER,LOC_CMMREGISTER:
  1113. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,right.location.register,left.location.register));
  1114. else
  1115. internalerror(200402223);
  1116. end;
  1117. end;
  1118. location.resflags:=getfpuresflags;
  1119. location_freetemp(current_asmdata.CurrAsmList,left.location);
  1120. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1121. end;
  1122. procedure tx86addnode.second_opvector;
  1123. var
  1124. op : topcg;
  1125. begin
  1126. pass_left_right;
  1127. if (nf_swapped in flags) then
  1128. swapleftright;
  1129. case nodetype of
  1130. addn :
  1131. op:=OP_ADD;
  1132. muln :
  1133. op:=OP_MUL;
  1134. subn :
  1135. op:=OP_SUB;
  1136. slashn :
  1137. op:=OP_DIV;
  1138. else
  1139. internalerror(200610071);
  1140. end;
  1141. if fits_in_mm_register(left.resultdef) then
  1142. begin
  1143. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  1144. { we can use only right as left operand if the operation is commutative }
  1145. if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  1146. begin
  1147. location.register:=right.location.register;
  1148. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,tfloat2tcgsize[tfloatdef(left.resultdef).floattype],left.location,location.register,nil);
  1149. end
  1150. else
  1151. begin
  1152. location_force_mmreg(current_asmdata.CurrAsmList,left.location,false);
  1153. location.register:=left.location.register;
  1154. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,
  1155. tfloat2tcgsize[tfloatdef(tarraydef(left.resultdef).elementdef).floattype],right.location,location.register,nil);
  1156. end;
  1157. end
  1158. else
  1159. begin
  1160. { not yet supported }
  1161. internalerror(200610072);
  1162. end
  1163. end;
  1164. procedure tx86addnode.second_addfloat;
  1165. const
  1166. ops_add: array[boolean] of TAsmOp = (A_FADDP,A_FADD);
  1167. ops_mul: array[boolean] of TAsmOp = (A_FMULP,A_FMUL);
  1168. ops_sub: array[boolean] of TAsmOp = (A_FSUBP,A_FSUB);
  1169. ops_rsub: array[boolean] of TAsmOp = (A_FSUBRP,A_FSUBR);
  1170. ops_div: array[boolean] of TAsmOp = (A_FDIVP,A_FDIV);
  1171. ops_rdiv: array[boolean] of TAsmOp = (A_FDIVRP,A_FDIVR);
  1172. var
  1173. op : TAsmOp;
  1174. refnode : tnode;
  1175. hasref : boolean;
  1176. begin
  1177. if use_vectorfpu(resultdef) then
  1178. begin
  1179. if UseAVX then
  1180. second_addfloatavx
  1181. else
  1182. second_addfloatsse;
  1183. exit;
  1184. end;
  1185. pass_left_right;
  1186. prepare_x87_locations(refnode);
  1187. hasref:=assigned(refnode);
  1188. case nodetype of
  1189. addn :
  1190. op:=ops_add[hasref];
  1191. muln :
  1192. op:=ops_mul[hasref];
  1193. subn :
  1194. if (nf_swapped in flags) then
  1195. op:=ops_rsub[hasref]
  1196. else
  1197. op:=ops_sub[hasref];
  1198. slashn :
  1199. if (nf_swapped in flags) then
  1200. op:=ops_rdiv[hasref]
  1201. else
  1202. op:=ops_div[hasref];
  1203. else
  1204. internalerror(2003042214);
  1205. end;
  1206. if hasref then
  1207. emit_ref(op,tcgsize2opsize[refnode.location.size],refnode.location.reference)
  1208. else
  1209. begin
  1210. emit_reg_reg(op,S_NO,NR_ST,NR_ST1);
  1211. tcgx86(cg).dec_fpu_stack;
  1212. end;
  1213. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  1214. location.register:=NR_ST;
  1215. end;
  1216. procedure tx86addnode.second_cmpfloat;
  1217. {$ifdef i8086}
  1218. var
  1219. tmpref: treference;
  1220. {$endif i8086}
  1221. begin
  1222. if use_vectorfpu(left.resultdef) or use_vectorfpu(right.resultdef) then
  1223. begin
  1224. second_cmpfloatvector;
  1225. exit;
  1226. end;
  1227. pass_left_right;
  1228. force_left_and_right_fpureg;
  1229. {$ifndef x86_64}
  1230. if current_settings.cputype<cpu_Pentium2 then
  1231. begin
  1232. emit_none(A_FCOMPP,S_NO);
  1233. tcgx86(cg).dec_fpu_stack;
  1234. tcgx86(cg).dec_fpu_stack;
  1235. { load fpu flags }
  1236. {$ifdef i8086}
  1237. if current_settings.cputype < cpu_286 then
  1238. begin
  1239. tg.gettemp(current_asmdata.CurrAsmList,2,2,tt_normal,tmpref);
  1240. emit_ref(A_FSTSW,S_NO,tmpref);
  1241. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1242. inc(tmpref.offset);
  1243. emit_ref_reg(A_MOV,S_B,tmpref,NR_AH);
  1244. dec(tmpref.offset);
  1245. emit_none(A_SAHF,S_NO);
  1246. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1247. tg.ungettemp(current_asmdata.CurrAsmList,tmpref);
  1248. end
  1249. else
  1250. {$endif i8086}
  1251. begin
  1252. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1253. emit_reg(A_FNSTSW,S_NO,NR_AX);
  1254. emit_none(A_SAHF,S_NO);
  1255. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1256. end;
  1257. end
  1258. else
  1259. {$endif x86_64}
  1260. begin
  1261. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCOMIP,S_NO,NR_ST1,NR_ST0));
  1262. { fcomip pops only one fpu register }
  1263. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_FSTP,S_NO,NR_ST0));
  1264. tcgx86(cg).dec_fpu_stack;
  1265. tcgx86(cg).dec_fpu_stack;
  1266. end;
  1267. location_reset(location,LOC_FLAGS,OS_NO);
  1268. location.resflags:=getfpuresflags;
  1269. end;
  1270. {*****************************************************************************
  1271. Add64bit
  1272. *****************************************************************************}
  1273. procedure tx86addnode.second_add64bit;
  1274. begin
  1275. {$ifdef cpu64bitalu}
  1276. second_addordinal;
  1277. {$else cpu64bitalu}
  1278. { must be implemented separate }
  1279. internalerror(200402042);
  1280. {$endif cpu64bitalu}
  1281. end;
  1282. procedure tx86addnode.second_cmp64bit;
  1283. begin
  1284. {$ifdef cpu64bitalu}
  1285. second_cmpordinal;
  1286. {$else cpu64bitalu}
  1287. { must be implemented separate }
  1288. internalerror(200402043);
  1289. {$endif cpu64bitalu}
  1290. end;
  1291. {*****************************************************************************
  1292. AddOrdinal
  1293. *****************************************************************************}
  1294. procedure tx86addnode.second_addordinal;
  1295. var
  1296. opsize : tcgsize;
  1297. unsigned : boolean;
  1298. cgop : topcg;
  1299. checkoverflow : Boolean;
  1300. ovloc : tlocation;
  1301. tmpreg : TRegister;
  1302. begin
  1303. { determine if the comparison will be unsigned }
  1304. unsigned:=not(is_signed(left.resultdef)) or
  1305. not(is_signed(right.resultdef));
  1306. { assume no overflow checking is require }
  1307. checkoverflow := false;
  1308. ovloc.loc:=LOC_VOID;
  1309. case nodetype of
  1310. addn:
  1311. begin
  1312. cgop:=OP_ADD;
  1313. checkoverflow:=true;
  1314. end;
  1315. xorn :
  1316. begin
  1317. cgop:=OP_XOR;
  1318. end;
  1319. orn :
  1320. begin
  1321. cgop:=OP_OR;
  1322. end;
  1323. andn:
  1324. begin
  1325. cgop:=OP_AND;
  1326. end;
  1327. muln:
  1328. begin
  1329. checkoverflow:=true;
  1330. if unsigned then
  1331. cgop:=OP_MUL
  1332. else
  1333. cgop:=OP_IMUL;
  1334. end;
  1335. subn :
  1336. begin
  1337. checkoverflow:=true;
  1338. cgop:=OP_SUB;
  1339. end;
  1340. else
  1341. internalerror(2015022501);
  1342. end;
  1343. checkoverflow:=
  1344. checkoverflow and
  1345. (left.resultdef.typ<>pointerdef) and
  1346. (right.resultdef.typ<>pointerdef) and
  1347. (cs_check_overflow in current_settings.localswitches);
  1348. opsize:=def_cgsize(left.resultdef);
  1349. pass_left_right;
  1350. { do have to allocate a register? If yes, then three opcode instructions are better }
  1351. if ((left.location.loc<>LOC_REGISTER) and (right.location.loc<>LOC_REGISTER)) or
  1352. ((nodetype=addn) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT]) and (right.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT])) then
  1353. begin
  1354. { allocate registers }
  1355. force_reg_left_right(false,true);
  1356. set_result_location_reg;
  1357. if nodetype<>subn then
  1358. begin
  1359. if (right.location.loc<>LOC_CONSTANT) then
  1360. hlcg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,cgop,resultdef,
  1361. left.location.register,right.location.register,
  1362. location.register,checkoverflow,ovloc)
  1363. else
  1364. hlcg.a_op_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,cgop,resultdef,
  1365. right.location.value,left.location.register,
  1366. location.register,checkoverflow,ovloc);
  1367. end
  1368. else { subtract is a special case since its not commutative }
  1369. begin
  1370. if (nf_swapped in flags) then
  1371. swapleftright;
  1372. if left.location.loc<>LOC_CONSTANT then
  1373. begin
  1374. if right.location.loc<>LOC_CONSTANT then
  1375. hlcg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,resultdef,
  1376. right.location.register,left.location.register,
  1377. location.register,checkoverflow,ovloc)
  1378. else
  1379. hlcg.a_op_const_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,resultdef,
  1380. right.location.value,left.location.register,
  1381. location.register,checkoverflow,ovloc);
  1382. end
  1383. else
  1384. begin
  1385. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  1386. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,
  1387. left.location.value,tmpreg);
  1388. hlcg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,resultdef,
  1389. right.location.register,tmpreg,location.register,checkoverflow,ovloc);
  1390. end;
  1391. end
  1392. end
  1393. else
  1394. begin
  1395. { at least one location is a register, re-use it, so we can try two operand opcodes }
  1396. if left.location.loc<>LOC_REGISTER then
  1397. begin
  1398. if right.location.loc<>LOC_REGISTER then
  1399. begin
  1400. { tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  1401. cg.a_load_loc_reg(current_asmdata.CurrAsmList,opsize,left.location,tmpreg);
  1402. location_reset(left.location,LOC_REGISTER,opsize);
  1403. left.location.register:=tmpreg;
  1404. }
  1405. Internalerror(2018031102);
  1406. end
  1407. else
  1408. begin
  1409. location_swap(left.location,right.location);
  1410. toggleflag(nf_swapped);
  1411. end;
  1412. end;
  1413. { at this point, left.location.loc should be LOC_REGISTER }
  1414. if right.location.loc=LOC_REGISTER then
  1415. begin
  1416. { when swapped another result register }
  1417. if (nodetype=subn) and (nf_swapped in flags) then
  1418. begin
  1419. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,opsize,
  1420. left.location.register,right.location.register);
  1421. location_swap(left.location,right.location);
  1422. toggleflag(nf_swapped);
  1423. end
  1424. else
  1425. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,opsize,
  1426. right.location.register,left.location.register);
  1427. end
  1428. else
  1429. begin
  1430. { right.location<>LOC_REGISTER }
  1431. if right.location.loc in [LOC_CSUBSETREF,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_SUBSETREG] then
  1432. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef,true);
  1433. if (nodetype=subn) and (nf_swapped in flags) then
  1434. begin
  1435. tmpreg:=left.location.register;
  1436. left.location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  1437. cg.a_load_loc_reg(current_asmdata.CurrAsmList,opsize,right.location,left.location.register);
  1438. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,opsize,tmpreg,left.location.register);
  1439. end
  1440. else
  1441. cg.a_op_loc_reg(current_asmdata.CurrAsmList,cgop,opsize,right.location,left.location.register);
  1442. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1443. end;
  1444. location_copy(location,left.location);
  1445. end;
  1446. { emit overflow check if required }
  1447. if checkoverflow then
  1448. cg.g_overflowcheck_loc(current_asmdata.CurrAsmList,Location,resultdef,ovloc);
  1449. end;
  1450. procedure tx86addnode.second_cmpordinal;
  1451. var
  1452. opdef : tdef;
  1453. opsize : tcgsize;
  1454. unsigned : boolean;
  1455. begin
  1456. unsigned:=not(is_signed(left.resultdef)) or
  1457. not(is_signed(right.resultdef));
  1458. opdef:=left.resultdef;
  1459. opsize:=def_cgsize(opdef);
  1460. pass_left_right;
  1461. if (right.location.loc=LOC_CONSTANT) and
  1462. (left.location.loc in [LOC_REFERENCE, LOC_CREFERENCE])
  1463. {$ifdef x86_64}
  1464. and ((not (opsize in [OS_64,OS_S64])) or (
  1465. (right.location.value>=low(longint)) and (right.location.value<=high(longint))
  1466. ))
  1467. {$endif x86_64}
  1468. then
  1469. begin
  1470. emit_const_ref(A_CMP, TCGSize2Opsize[opsize], right.location.value, left.location.reference);
  1471. location_freetemp(current_asmdata.CurrAsmList,left.location);
  1472. end
  1473. else
  1474. begin
  1475. left_must_be_reg(opdef,opsize,false);
  1476. emit_generic_code(A_CMP,opsize,unsigned,false,false);
  1477. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1478. location_freetemp(current_asmdata.CurrAsmList,left.location);
  1479. end;
  1480. location_reset(location,LOC_FLAGS,OS_NO);
  1481. location.resflags:=getresflags(unsigned);
  1482. end;
  1483. begin
  1484. caddnode:=tx86addnode;
  1485. end.