nllvmadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. Generate LLVM bytecode for add nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nllvmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. ncgadd;
  23. type
  24. tllvmaddnode = class(tcgaddnode)
  25. public
  26. function pass_1: tnode; override;
  27. procedure force_reg_left_right(allow_swap, allow_constant: boolean); override;
  28. protected
  29. procedure second_cmpsmallset; override;
  30. procedure second_cmpordinal; override;
  31. procedure second_add64bit; override;
  32. procedure second_cmp64bit; override;
  33. procedure second_addfloat; override;
  34. procedure second_cmpfloat; override;
  35. end;
  36. implementation
  37. uses
  38. verbose,globtype,globals,cutils,
  39. aasmdata,
  40. symconst,symtype,symdef,defutil,
  41. llvmbase,aasmllvm,aasmllvmmetadata,
  42. cgbase,cgutils,pass_1,
  43. hlcgobj,
  44. nadd,ncal,ncnv,ncon
  45. ;
  46. { tllvmaddnode }
  47. function tllvmaddnode.pass_1: tnode;
  48. var
  49. exceptmode: ansistring;
  50. intrname: string;
  51. iscompcurrency: boolean;
  52. begin
  53. result:=inherited pass_1;
  54. if not assigned(result) and
  55. is_fpu(left.resultdef) and
  56. not(cs_opt_fastmath in current_settings.optimizerswitches) then
  57. begin
  58. case nodetype of
  59. addn:
  60. begin
  61. intrname:='LLVM_EXPERIMENTAL_CONSTRAINED_FADD';
  62. end;
  63. subn:
  64. begin
  65. intrname:='LLVM_EXPERIMENTAL_CONSTRAINED_FSUB';
  66. end;
  67. muln:
  68. begin
  69. intrname:='LLVM_EXPERIMENTAL_CONSTRAINED_FMUL';
  70. end;
  71. slashn:
  72. begin
  73. intrname:='LLVM_EXPERIMENTAL_CONSTRAINED_FDIV';
  74. end;
  75. else
  76. begin
  77. intrname:='';
  78. end;
  79. end;
  80. if intrname<>'' then
  81. begin
  82. iscompcurrency:=tfloatdef(left.resultdef).floattype in [s64currency,s64comp];
  83. if iscompcurrency then
  84. begin
  85. inserttypeconv_internal(left,s80floattype);
  86. inserttypeconv_internal(right,s80floattype);
  87. end;
  88. exceptmode:=llvm_constrainedexceptmodestring;
  89. result:=ccallnode.createintern(intrname,
  90. ccallparanode.create(cstringconstnode.createpchar(ansistring2pchar(exceptmode),length(exceptmode),llvm_metadatatype),
  91. ccallparanode.create(cstringconstnode.createpchar(ansistring2pchar('round.dynamic'),length('round.dynamic'),llvm_metadatatype),
  92. ccallparanode.create(right,
  93. ccallparanode.create(left,nil)
  94. )
  95. )
  96. )
  97. );
  98. if iscompcurrency then
  99. begin
  100. result:=ctypeconvnode.create_internal(result,resultdef);
  101. end;
  102. left:=nil;
  103. right:=nil;
  104. exit;
  105. end;
  106. end;
  107. { there are no flags in LLVM }
  108. if expectloc=LOC_FLAGS then
  109. expectloc:=LOC_REGISTER;
  110. end;
  111. procedure tllvmaddnode.force_reg_left_right(allow_swap, allow_constant: boolean);
  112. begin
  113. { comparison with pointer -> no immediate, as icmp can't handle pointer
  114. immediates (except for nil as "null", but we don't generate that) }
  115. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  116. ((left.nodetype in [pointerconstn,niln]) or
  117. (right.nodetype in [pointerconstn,niln])) then
  118. allow_constant:=false;
  119. inherited;
  120. { pointer - pointer = integer -> make all defs pointer since we can't
  121. subtract pointers }
  122. if (nodetype=subn) and
  123. (left.resultdef.typ=pointerdef) and
  124. (right.resultdef.typ=pointerdef) then
  125. begin
  126. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  127. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  128. end
  129. { pointer +/- integer -> make defs the same since a_op_* only gets a
  130. single type as argument }
  131. else if (nodetype in [addn,subn]) and
  132. ((left.resultdef.typ=pointerdef)<>(right.resultdef.typ=pointerdef)) then
  133. begin
  134. { the result is a pointerdef -> typecast both arguments to pointer;
  135. a_op_*_reg will convert them back to integer as needed }
  136. if left.resultdef.typ<>pointerdef then
  137. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  138. if right.resultdef.typ<>pointerdef then
  139. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,true);
  140. end;
  141. end;
  142. procedure tllvmaddnode.second_cmpsmallset;
  143. var
  144. tmpreg,
  145. tmpreg2: tregister;
  146. cmpop : topcmp;
  147. begin
  148. pass_left_right;
  149. location_reset(location,LOC_REGISTER,OS_8);
  150. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  151. force_reg_left_right(false,false);
  152. case nodetype of
  153. equaln,
  154. unequaln:
  155. begin
  156. if nodetype=equaln then
  157. cmpop:=OC_EQ
  158. else
  159. cmpop:=OC_NE;
  160. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  161. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  162. end;
  163. lten,
  164. gten:
  165. begin
  166. if (not(nf_swapped in flags) and
  167. (nodetype = lten)) or
  168. ((nf_swapped in flags) and
  169. (nodetype = gten)) then
  170. swapleftright;
  171. { set1<=set2 <-> set2 and not(set1) = 0 }
  172. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  173. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,left.location.register,tmpreg);
  174. tmpreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  175. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,left.resultdef,right.location.register,tmpreg,tmpreg2);
  176. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  177. location.register,OC_EQ,left.resultdef,tmpreg2,0));
  178. end;
  179. else
  180. internalerror(2012042701);
  181. end;
  182. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  183. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  184. location.register:=tmpreg;
  185. end;
  186. procedure tllvmaddnode.second_cmpordinal;
  187. var
  188. tmpreg: tregister;
  189. cmpop: topcmp;
  190. unsigned : boolean;
  191. begin
  192. pass_left_right;
  193. force_reg_left_right(true,true);
  194. unsigned:=not(is_signed(left.resultdef)) or
  195. not(is_signed(right.resultdef));
  196. case nodetype of
  197. ltn:
  198. if unsigned then
  199. cmpop:=OC_B
  200. else
  201. cmpop:=OC_LT;
  202. lten:
  203. if unsigned then
  204. cmpop:=OC_BE
  205. else
  206. cmpop:=OC_LTE;
  207. gtn:
  208. if unsigned then
  209. cmpop:=OC_A
  210. else
  211. cmpop:=OC_GT;
  212. gten:
  213. if unsigned then
  214. cmpop:=OC_AE
  215. else
  216. cmpop:=OC_GTE;
  217. equaln:
  218. cmpop:=OC_EQ;
  219. unequaln:
  220. cmpop:=OC_NE;
  221. else
  222. internalerror(2015031505);
  223. end;
  224. if nf_swapped in flags then
  225. cmpop:=swap_opcmp(cmpop);
  226. location_reset(location,LOC_REGISTER,OS_8);
  227. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  228. if right.location.loc=LOC_CONSTANT then
  229. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_const(la_icmp,
  230. location.register,cmpop,left.resultdef,left.location.register,right.location.value64))
  231. else
  232. current_asmdata.CurrAsmList.concat(taillvm.op_reg_cond_size_reg_reg(la_icmp,
  233. location.register,cmpop,left.resultdef,left.location.register,right.location.register));
  234. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  235. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  236. location.register:=tmpreg;
  237. end;
  238. procedure tllvmaddnode.second_add64bit;
  239. begin
  240. second_addordinal;
  241. end;
  242. procedure tllvmaddnode.second_cmp64bit;
  243. begin
  244. second_cmpordinal;
  245. end;
  246. procedure tllvmaddnode.second_addfloat;
  247. var
  248. tmpreg: tregister;
  249. op : tllvmop;
  250. llvmfpcmp : tllvmfpcmp;
  251. size : tdef;
  252. begin
  253. pass_left_right;
  254. { get the operands in the correct order; there are no special cases here,
  255. everything is register-based }
  256. if nf_swapped in flags then
  257. swapleftright;
  258. { put both operands in a register }
  259. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  260. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  261. { see comment in thlcgllvm.a_loadfpu_ref_reg }
  262. if tfloatdef(left.resultdef).floattype in [s64comp,s64currency] then
  263. size:=sc80floattype
  264. else
  265. size:=left.resultdef;
  266. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  267. begin
  268. case nodetype of
  269. ltn:
  270. llvmfpcmp:=lfc_olt;
  271. lten:
  272. llvmfpcmp:=lfc_ole;
  273. gtn:
  274. llvmfpcmp:=lfc_ogt;
  275. gten:
  276. llvmfpcmp:=lfc_oge;
  277. equaln:
  278. llvmfpcmp:=lfc_oeq;
  279. unequaln:
  280. llvmfpcmp:=lfc_une;
  281. else
  282. internalerror(2015031506);
  283. end;
  284. location_reset(location,LOC_REGISTER,OS_8);
  285. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,llvmbool1type);
  286. current_asmdata.CurrAsmList.concat(taillvm.op_reg_fpcond_size_reg_reg(la_fcmp ,
  287. location.register,llvmfpcmp,size,left.location.register,right.location.register));
  288. tmpreg:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  289. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,llvmbool1type,resultdef,location.register,tmpreg);
  290. location.register:=tmpreg;
  291. end
  292. else
  293. begin
  294. case nodetype of
  295. addn :
  296. op:=la_fadd;
  297. muln :
  298. op:=la_fmul;
  299. subn :
  300. op:=la_fsub;
  301. slashn :
  302. op:=la_fdiv;
  303. else
  304. internalerror(2013102401);
  305. end;
  306. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  307. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  308. current_asmdata.CurrAsmList.concat(taillvm.op_reg_size_reg_reg(op,location.register,size,
  309. left.location.register,right.location.register))
  310. end;
  311. end;
  312. procedure tllvmaddnode.second_cmpfloat;
  313. begin
  314. second_addfloat;
  315. end;
  316. begin
  317. caddnode:=tllvmaddnode;
  318. end.