nx86add.pas 61 KB

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