lj_opt_split.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. ** SPLIT: Split 64 bit IR instructions into 32 bit IR instructions.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_opt_split_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI))
  9. #include "lj_err.h"
  10. #include "lj_str.h"
  11. #include "lj_ir.h"
  12. #include "lj_jit.h"
  13. #include "lj_ircall.h"
  14. #include "lj_iropt.h"
  15. #include "lj_vm.h"
  16. /* SPLIT pass:
  17. **
  18. ** This pass splits up 64 bit IR instructions into multiple 32 bit IR
  19. ** instructions. It's only active for soft-float targets or for 32 bit CPUs
  20. ** which lack native 64 bit integer operations (the FFI is currently the
  21. ** only emitter for 64 bit integer instructions).
  22. **
  23. ** Splitting the IR in a separate pass keeps each 32 bit IR assembler
  24. ** backend simple. Only a small amount of extra functionality needs to be
  25. ** implemented. This is much easier than adding support for allocating
  26. ** register pairs to each backend (believe me, I tried). A few simple, but
  27. ** important optimizations can be performed by the SPLIT pass, which would
  28. ** be tedious to do in the backend.
  29. **
  30. ** The basic idea is to replace each 64 bit IR instruction with its 32 bit
  31. ** equivalent plus an extra HIOP instruction. The splitted IR is not passed
  32. ** through FOLD or any other optimizations, so each HIOP is guaranteed to
  33. ** immediately follow it's counterpart. The actual functionality of HIOP is
  34. ** inferred from the previous instruction.
  35. **
  36. ** The operands of HIOP hold the hiword input references. The output of HIOP
  37. ** is the hiword output reference, which is also used to hold the hiword
  38. ** register or spill slot information. The register allocator treats this
  39. ** instruction independently of any other instruction, which improves code
  40. ** quality compared to using fixed register pairs.
  41. **
  42. ** It's easier to split up some instructions into two regular 32 bit
  43. ** instructions. E.g. XLOAD is split up into two XLOADs with two different
  44. ** addresses. Obviously 64 bit constants need to be split up into two 32 bit
  45. ** constants, too. Some hiword instructions can be entirely omitted, e.g.
  46. ** when zero-extending a 32 bit value to 64 bits. 64 bit arguments for calls
  47. ** are split up into two 32 bit arguments each.
  48. **
  49. ** On soft-float targets, floating-point instructions are directly converted
  50. ** to soft-float calls by the SPLIT pass (except for comparisons and MIN/MAX).
  51. ** HIOP for number results has the type IRT_SOFTFP ("sfp" in -jdump).
  52. **
  53. ** Here's the IR and x64 machine code for 'x.b = x.a + 1' for a struct with
  54. ** two int64_t fields:
  55. **
  56. ** 0100 p32 ADD base +8
  57. ** 0101 i64 XLOAD 0100
  58. ** 0102 i64 ADD 0101 +1
  59. ** 0103 p32 ADD base +16
  60. ** 0104 i64 XSTORE 0103 0102
  61. **
  62. ** mov rax, [esi+0x8]
  63. ** add rax, +0x01
  64. ** mov [esi+0x10], rax
  65. **
  66. ** Here's the transformed IR and the x86 machine code after the SPLIT pass:
  67. **
  68. ** 0100 p32 ADD base +8
  69. ** 0101 int XLOAD 0100
  70. ** 0102 p32 ADD base +12
  71. ** 0103 int XLOAD 0102
  72. ** 0104 int ADD 0101 +1
  73. ** 0105 int HIOP 0103 +0
  74. ** 0106 p32 ADD base +16
  75. ** 0107 int XSTORE 0106 0104
  76. ** 0108 int HIOP 0106 0105
  77. **
  78. ** mov eax, [esi+0x8]
  79. ** mov ecx, [esi+0xc]
  80. ** add eax, +0x01
  81. ** adc ecx, +0x00
  82. ** mov [esi+0x10], eax
  83. ** mov [esi+0x14], ecx
  84. **
  85. ** You may notice the reassociated hiword address computation, which is
  86. ** later fused into the mov operands by the assembler.
  87. */
  88. /* Some local macros to save typing. Undef'd at the end. */
  89. #define IR(ref) (&J->cur.ir[(ref)])
  90. /* Directly emit the transformed IR without updating chains etc. */
  91. static IRRef split_emit(jit_State *J, uint16_t ot, IRRef1 op1, IRRef1 op2)
  92. {
  93. IRRef nref = lj_ir_nextins(J);
  94. IRIns *ir = IR(nref);
  95. ir->ot = ot;
  96. ir->op1 = op1;
  97. ir->op2 = op2;
  98. return nref;
  99. }
  100. #if LJ_SOFTFP
  101. /* Emit a (checked) number to integer conversion. */
  102. static IRRef split_num2int(jit_State *J, IRRef lo, IRRef hi, int check)
  103. {
  104. IRRef tmp, res;
  105. #if LJ_LE
  106. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), lo, hi);
  107. #else
  108. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hi, lo);
  109. #endif
  110. res = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_softfp_d2i);
  111. if (check) {
  112. tmp = split_emit(J, IRTI(IR_CALLN), res, IRCALL_softfp_i2d);
  113. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);
  114. split_emit(J, IRTGI(IR_EQ), tmp, lo);
  115. split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), tmp+1, hi);
  116. }
  117. return res;
  118. }
  119. /* Emit a CALLN with one split 64 bit argument. */
  120. static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir,
  121. IRIns *ir, IRCallID id)
  122. {
  123. IRRef tmp, op1 = ir->op1;
  124. J->cur.nins--;
  125. #if LJ_LE
  126. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);
  127. #else
  128. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);
  129. #endif
  130. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);
  131. return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);
  132. }
  133. /* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */
  134. static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir,
  135. IRIns *ir, IRCallID id)
  136. {
  137. IRRef tmp, op1 = ir->op1, op2 = ir->op2;
  138. J->cur.nins--;
  139. #if LJ_LE
  140. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);
  141. #else
  142. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);
  143. #endif
  144. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);
  145. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);
  146. return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);
  147. }
  148. #endif
  149. /* Emit a CALLN with two split 64 bit arguments. */
  150. static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir,
  151. IRIns *ir, IRCallID id)
  152. {
  153. IRRef tmp, op1 = ir->op1, op2 = ir->op2;
  154. J->cur.nins--;
  155. #if LJ_LE
  156. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);
  157. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);
  158. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]);
  159. #else
  160. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);
  161. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]);
  162. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev);
  163. #endif
  164. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id);
  165. return split_emit(J,
  166. IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT),
  167. tmp, tmp);
  168. }
  169. /* Get a pointer to the other 32 bit word (LE: hiword, BE: loword). */
  170. static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref)
  171. {
  172. IRRef nref = oir[ref].prev;
  173. IRIns *ir = IR(nref);
  174. int32_t ofs = 4;
  175. if (ir->o == IR_KPTR)
  176. return lj_ir_kptr(J, (char *)ir_kptr(ir) + ofs);
  177. if (ir->o == IR_ADD && irref_isk(ir->op2) && !irt_isphi(oir[ref].t)) {
  178. /* Reassociate address. */
  179. ofs += IR(ir->op2)->i;
  180. nref = ir->op1;
  181. if (ofs == 0) return nref;
  182. }
  183. return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs));
  184. }
  185. /* Substitute references of a snapshot. */
  186. static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir)
  187. {
  188. SnapEntry *map = &J->cur.snapmap[snap->mapofs];
  189. MSize n, nent = snap->nent;
  190. for (n = 0; n < nent; n++) {
  191. SnapEntry sn = map[n];
  192. IRIns *ir = &oir[snap_ref(sn)];
  193. if (!(LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && irref_isk(snap_ref(sn))))
  194. map[n] = ((sn & 0xffff0000) | ir->prev);
  195. }
  196. }
  197. /* Transform the old IR to the new IR. */
  198. static void split_ir(jit_State *J)
  199. {
  200. IRRef nins = J->cur.nins, nk = J->cur.nk;
  201. MSize irlen = nins - nk;
  202. MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1));
  203. IRIns *oir = (IRIns *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, need);
  204. IRRef1 *hisubst;
  205. IRRef ref, snref;
  206. SnapShot *snap;
  207. /* Copy old IR to buffer. */
  208. memcpy(oir, IR(nk), irlen*sizeof(IRIns));
  209. /* Bias hiword substitution table and old IR. Loword kept in field prev. */
  210. hisubst = (IRRef1 *)&oir[irlen] - nk;
  211. oir -= nk;
  212. /* Remove all IR instructions, but retain IR constants. */
  213. J->cur.nins = REF_FIRST;
  214. J->loopref = 0;
  215. /* Process constants and fixed references. */
  216. for (ref = nk; ref <= REF_BASE; ref++) {
  217. IRIns *ir = &oir[ref];
  218. if ((LJ_SOFTFP && ir->o == IR_KNUM) || ir->o == IR_KINT64) {
  219. /* Split up 64 bit constant. */
  220. TValue tv = *ir_k64(ir);
  221. ir->prev = lj_ir_kint(J, (int32_t)tv.u32.lo);
  222. hisubst[ref] = lj_ir_kint(J, (int32_t)tv.u32.hi);
  223. } else {
  224. ir->prev = ref; /* Identity substitution for loword. */
  225. hisubst[ref] = 0;
  226. }
  227. }
  228. /* Process old IR instructions. */
  229. snap = J->cur.snap;
  230. snref = snap->ref;
  231. for (ref = REF_FIRST; ref < nins; ref++) {
  232. IRIns *ir = &oir[ref];
  233. IRRef nref = lj_ir_nextins(J);
  234. IRIns *nir = IR(nref);
  235. IRRef hi = 0;
  236. if (ref >= snref) {
  237. snap->ref = nref;
  238. split_subst_snap(J, snap++, oir);
  239. snref = snap < &J->cur.snap[J->cur.nsnap] ? snap->ref : ~(IRRef)0;
  240. }
  241. /* Copy-substitute old instruction to new instruction. */
  242. nir->op1 = ir->op1 < nk ? ir->op1 : oir[ir->op1].prev;
  243. nir->op2 = ir->op2 < nk ? ir->op2 : oir[ir->op2].prev;
  244. ir->prev = nref; /* Loword substitution. */
  245. nir->o = ir->o;
  246. nir->t.irt = ir->t.irt & ~(IRT_MARK|IRT_ISPHI);
  247. hisubst[ref] = 0;
  248. /* Split 64 bit instructions. */
  249. #if LJ_SOFTFP
  250. if (irt_isnum(ir->t)) {
  251. nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */
  252. /* Note: hi ref = lo ref + 1! Required for SNAP_SOFTFPNUM logic. */
  253. switch (ir->o) {
  254. case IR_ADD:
  255. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_add);
  256. break;
  257. case IR_SUB:
  258. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_sub);
  259. break;
  260. case IR_MUL:
  261. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_mul);
  262. break;
  263. case IR_DIV:
  264. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_div);
  265. break;
  266. case IR_POW:
  267. hi = split_call_li(J, hisubst, oir, ir, IRCALL_lj_vm_powi);
  268. break;
  269. case IR_FPMATH:
  270. /* Try to rejoin pow from EXP2, MUL and LOG2. */
  271. if (nir->op2 == IRFPM_EXP2 && nir->op1 > J->loopref) {
  272. IRIns *irp = IR(nir->op1);
  273. if (irp->o == IR_CALLN && irp->op2 == IRCALL_softfp_mul) {
  274. IRIns *irm4 = IR(irp->op1);
  275. IRIns *irm3 = IR(irm4->op1);
  276. IRIns *irm12 = IR(irm3->op1);
  277. IRIns *irl1 = IR(irm12->op1);
  278. if (irm12->op1 > J->loopref && irl1->o == IR_CALLN &&
  279. irl1->op2 == IRCALL_lj_vm_log2) {
  280. IRRef tmp = irl1->op1; /* Recycle first two args from LOG2. */
  281. IRRef arg3 = irm3->op2, arg4 = irm4->op2;
  282. J->cur.nins--;
  283. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg3);
  284. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg4);
  285. ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_pow);
  286. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp);
  287. break;
  288. }
  289. }
  290. }
  291. hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2);
  292. break;
  293. case IR_ATAN2:
  294. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_atan2);
  295. break;
  296. case IR_LDEXP:
  297. hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp);
  298. break;
  299. case IR_NEG: case IR_ABS:
  300. nir->o = IR_CONV; /* Pass through loword. */
  301. nir->op2 = (IRT_INT << 5) | IRT_INT;
  302. hi = split_emit(J, IRT(ir->o == IR_NEG ? IR_BXOR : IR_BAND, IRT_SOFTFP),
  303. hisubst[ir->op1], hisubst[ir->op2]);
  304. break;
  305. case IR_SLOAD:
  306. if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from int to number. */
  307. nir->op2 &= ~IRSLOAD_CONVERT;
  308. ir->prev = nref = split_emit(J, IRTI(IR_CALLN), nref,
  309. IRCALL_softfp_i2d);
  310. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);
  311. break;
  312. }
  313. /* fallthrough */
  314. case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
  315. case IR_STRTO:
  316. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);
  317. break;
  318. case IR_XLOAD: {
  319. IRIns inslo = *nir; /* Save/undo the emit of the lo XLOAD. */
  320. J->cur.nins--;
  321. hi = split_ptr(J, oir, ir->op1); /* Insert the hiref ADD. */
  322. nref = lj_ir_nextins(J);
  323. nir = IR(nref);
  324. *nir = inslo; /* Re-emit lo XLOAD immediately before hi XLOAD. */
  325. hi = split_emit(J, IRT(IR_XLOAD, IRT_SOFTFP), hi, ir->op2);
  326. #if LJ_LE
  327. ir->prev = nref;
  328. #else
  329. ir->prev = hi; hi = nref;
  330. #endif
  331. break;
  332. }
  333. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_XSTORE:
  334. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nir->op1, hisubst[ir->op2]);
  335. break;
  336. case IR_CONV: { /* Conversion to number. Others handled below. */
  337. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  338. UNUSED(st);
  339. #if LJ_32 && LJ_HASFFI
  340. if (st == IRT_I64 || st == IRT_U64) {
  341. hi = split_call_l(J, hisubst, oir, ir,
  342. st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d);
  343. break;
  344. }
  345. #endif
  346. lua_assert(st == IRT_INT ||
  347. (LJ_32 && LJ_HASFFI && (st == IRT_U32 || st == IRT_FLOAT)));
  348. nir->o = IR_CALLN;
  349. #if LJ_32 && LJ_HASFFI
  350. nir->op2 = st == IRT_INT ? IRCALL_softfp_i2d :
  351. st == IRT_FLOAT ? IRCALL_softfp_f2d :
  352. IRCALL_softfp_ui2d;
  353. #else
  354. nir->op2 = IRCALL_softfp_i2d;
  355. #endif
  356. hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);
  357. break;
  358. }
  359. case IR_CALLN:
  360. case IR_CALLL:
  361. case IR_CALLS:
  362. case IR_CALLXS:
  363. goto split_call;
  364. case IR_PHI:
  365. if (nir->op1 == nir->op2)
  366. J->cur.nins--; /* Drop useless PHIs. */
  367. if (hisubst[ir->op1] != hisubst[ir->op2])
  368. split_emit(J, IRT(IR_PHI, IRT_SOFTFP),
  369. hisubst[ir->op1], hisubst[ir->op2]);
  370. break;
  371. case IR_HIOP:
  372. J->cur.nins--; /* Drop joining HIOP. */
  373. ir->prev = nir->op1;
  374. hi = nir->op2;
  375. break;
  376. default:
  377. lua_assert(ir->o <= IR_NE || ir->o == IR_MIN || ir->o == IR_MAX);
  378. hi = split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP),
  379. hisubst[ir->op1], hisubst[ir->op2]);
  380. break;
  381. }
  382. } else
  383. #endif
  384. #if LJ_32 && LJ_HASFFI
  385. if (irt_isint64(ir->t)) {
  386. IRRef hiref = hisubst[ir->op1];
  387. nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */
  388. switch (ir->o) {
  389. case IR_ADD:
  390. case IR_SUB:
  391. /* Use plain op for hiword if loword cannot produce a carry/borrow. */
  392. if (irref_isk(nir->op2) && IR(nir->op2)->i == 0) {
  393. ir->prev = nir->op1; /* Pass through loword. */
  394. nir->op1 = hiref; nir->op2 = hisubst[ir->op2];
  395. hi = nref;
  396. break;
  397. }
  398. /* fallthrough */
  399. case IR_NEG:
  400. hi = split_emit(J, IRTI(IR_HIOP), hiref, hisubst[ir->op2]);
  401. break;
  402. case IR_MUL:
  403. hi = split_call_ll(J, hisubst, oir, ir, IRCALL_lj_carith_mul64);
  404. break;
  405. case IR_DIV:
  406. hi = split_call_ll(J, hisubst, oir, ir,
  407. irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 :
  408. IRCALL_lj_carith_divu64);
  409. break;
  410. case IR_MOD:
  411. hi = split_call_ll(J, hisubst, oir, ir,
  412. irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 :
  413. IRCALL_lj_carith_modu64);
  414. break;
  415. case IR_POW:
  416. hi = split_call_ll(J, hisubst, oir, ir,
  417. irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 :
  418. IRCALL_lj_carith_powu64);
  419. break;
  420. case IR_FLOAD:
  421. lua_assert(ir->op2 == IRFL_CDATA_INT64);
  422. hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4);
  423. #if LJ_BE
  424. ir->prev = hi; hi = nref;
  425. #endif
  426. break;
  427. case IR_XLOAD:
  428. hi = split_emit(J, IRTI(IR_XLOAD), split_ptr(J, oir, ir->op1), ir->op2);
  429. #if LJ_BE
  430. ir->prev = hi; hi = nref;
  431. #endif
  432. break;
  433. case IR_XSTORE:
  434. split_emit(J, IRTI(IR_HIOP), nir->op1, hisubst[ir->op2]);
  435. break;
  436. case IR_CONV: { /* Conversion to 64 bit integer. Others handled below. */
  437. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  438. #if LJ_SOFTFP
  439. if (st == IRT_NUM) { /* NUM to 64 bit int conv. */
  440. hi = split_call_l(J, hisubst, oir, ir,
  441. irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul);
  442. } else if (st == IRT_FLOAT) { /* FLOAT to 64 bit int conv. */
  443. nir->o = IR_CALLN;
  444. nir->op2 = irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul;
  445. hi = split_emit(J, IRTI(IR_HIOP), nref, nref);
  446. }
  447. #else
  448. if (st == IRT_NUM || st == IRT_FLOAT) { /* FP to 64 bit int conv. */
  449. hi = split_emit(J, IRTI(IR_HIOP), nir->op1, nref);
  450. }
  451. #endif
  452. else if (st == IRT_I64 || st == IRT_U64) { /* 64/64 bit cast. */
  453. /* Drop cast, since assembler doesn't care. */
  454. goto fwdlo;
  455. } else if ((ir->op2 & IRCONV_SEXT)) { /* Sign-extend to 64 bit. */
  456. IRRef k31 = lj_ir_kint(J, 31);
  457. nir = IR(nref); /* May have been reallocated. */
  458. ir->prev = nir->op1; /* Pass through loword. */
  459. nir->o = IR_BSAR; /* hi = bsar(lo, 31). */
  460. nir->op2 = k31;
  461. hi = nref;
  462. } else { /* Zero-extend to 64 bit. */
  463. hi = lj_ir_kint(J, 0);
  464. goto fwdlo;
  465. }
  466. break;
  467. }
  468. case IR_CALLXS:
  469. goto split_call;
  470. case IR_PHI: {
  471. IRRef hiref2;
  472. if ((irref_isk(nir->op1) && irref_isk(nir->op2)) ||
  473. nir->op1 == nir->op2)
  474. J->cur.nins--; /* Drop useless PHIs. */
  475. hiref2 = hisubst[ir->op2];
  476. if (!((irref_isk(hiref) && irref_isk(hiref2)) || hiref == hiref2))
  477. split_emit(J, IRTI(IR_PHI), hiref, hiref2);
  478. break;
  479. }
  480. case IR_HIOP:
  481. J->cur.nins--; /* Drop joining HIOP. */
  482. ir->prev = nir->op1;
  483. hi = nir->op2;
  484. break;
  485. default:
  486. lua_assert(ir->o <= IR_NE); /* Comparisons. */
  487. split_emit(J, IRTGI(IR_HIOP), hiref, hisubst[ir->op2]);
  488. break;
  489. }
  490. } else
  491. #endif
  492. #if LJ_SOFTFP
  493. if (ir->o == IR_SLOAD) {
  494. if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from number to int. */
  495. nir->op2 &= ~IRSLOAD_CONVERT;
  496. if (!(nir->op2 & IRSLOAD_TYPECHECK))
  497. nir->t.irt = IRT_INT; /* Drop guard. */
  498. split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref);
  499. ir->prev = split_num2int(J, nref, nref+1, irt_isguard(ir->t));
  500. }
  501. } else if (ir->o == IR_TOBIT) {
  502. IRRef tmp, op1 = ir->op1;
  503. J->cur.nins--;
  504. #if LJ_LE
  505. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]);
  506. #else
  507. tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev);
  508. #endif
  509. ir->prev = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_lj_vm_tobit);
  510. } else if (ir->o == IR_TOSTR) {
  511. if (hisubst[ir->op1]) {
  512. if (irref_isk(ir->op1))
  513. nir->op1 = ir->op1;
  514. else
  515. split_emit(J, IRT(IR_HIOP, IRT_NIL), hisubst[ir->op1], nref);
  516. }
  517. } else if (ir->o == IR_HREF || ir->o == IR_NEWREF) {
  518. if (irref_isk(ir->op2) && hisubst[ir->op2])
  519. nir->op2 = ir->op2;
  520. } else
  521. #endif
  522. if (ir->o == IR_CONV) { /* See above, too. */
  523. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  524. #if LJ_32 && LJ_HASFFI
  525. if (st == IRT_I64 || st == IRT_U64) { /* Conversion from 64 bit int. */
  526. #if LJ_SOFTFP
  527. if (irt_isfloat(ir->t)) {
  528. split_call_l(J, hisubst, oir, ir,
  529. st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f);
  530. J->cur.nins--; /* Drop unused HIOP. */
  531. }
  532. #else
  533. if (irt_isfp(ir->t)) { /* 64 bit integer to FP conversion. */
  534. ir->prev = split_emit(J, IRT(IR_HIOP, irt_type(ir->t)),
  535. hisubst[ir->op1], nref);
  536. }
  537. #endif
  538. else { /* Truncate to lower 32 bits. */
  539. fwdlo:
  540. ir->prev = nir->op1; /* Forward loword. */
  541. /* Replace with NOP to avoid messing up the snapshot logic. */
  542. nir->ot = IRT(IR_NOP, IRT_NIL);
  543. nir->op1 = nir->op2 = 0;
  544. }
  545. }
  546. #endif
  547. #if LJ_SOFTFP && LJ_32 && LJ_HASFFI
  548. else if (irt_isfloat(ir->t)) {
  549. if (st == IRT_NUM) {
  550. split_call_l(J, hisubst, oir, ir, IRCALL_softfp_d2f);
  551. J->cur.nins--; /* Drop unused HIOP. */
  552. } else {
  553. nir->o = IR_CALLN;
  554. nir->op2 = st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f;
  555. }
  556. } else if (st == IRT_FLOAT) {
  557. nir->o = IR_CALLN;
  558. nir->op2 = irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui;
  559. } else
  560. #endif
  561. #if LJ_SOFTFP
  562. if (st == IRT_NUM || (LJ_32 && LJ_HASFFI && st == IRT_FLOAT)) {
  563. if (irt_isguard(ir->t)) {
  564. lua_assert(st == IRT_NUM && irt_isint(ir->t));
  565. J->cur.nins--;
  566. ir->prev = split_num2int(J, nir->op1, hisubst[ir->op1], 1);
  567. } else {
  568. split_call_l(J, hisubst, oir, ir,
  569. #if LJ_32 && LJ_HASFFI
  570. st == IRT_NUM ?
  571. (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) :
  572. (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui)
  573. #else
  574. IRCALL_softfp_d2i
  575. #endif
  576. );
  577. J->cur.nins--; /* Drop unused HIOP. */
  578. }
  579. }
  580. #endif
  581. } else if (ir->o == IR_CALLXS) {
  582. IRRef hiref;
  583. split_call:
  584. hiref = hisubst[ir->op1];
  585. if (hiref) {
  586. IROpT ot = nir->ot;
  587. IRRef op2 = nir->op2;
  588. nir->ot = IRT(IR_CARG, IRT_NIL);
  589. #if LJ_LE
  590. nir->op2 = hiref;
  591. #else
  592. nir->op2 = nir->op1; nir->op1 = hiref;
  593. #endif
  594. ir->prev = nref = split_emit(J, ot, nref, op2);
  595. }
  596. if (LJ_SOFTFP ? irt_is64(ir->t) : irt_isint64(ir->t))
  597. hi = split_emit(J,
  598. IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT),
  599. nref, nref);
  600. } else if (ir->o == IR_CARG) {
  601. IRRef hiref = hisubst[ir->op1];
  602. if (hiref) {
  603. IRRef op2 = nir->op2;
  604. #if LJ_LE
  605. nir->op2 = hiref;
  606. #else
  607. nir->op2 = nir->op1; nir->op1 = hiref;
  608. #endif
  609. ir->prev = nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2);
  610. nir = IR(nref);
  611. }
  612. hiref = hisubst[ir->op2];
  613. if (hiref) {
  614. #if !LJ_TARGET_X86
  615. int carg = 0;
  616. IRIns *cir;
  617. for (cir = IR(nir->op1); cir->o == IR_CARG; cir = IR(cir->op1))
  618. carg++;
  619. if ((carg & 1) == 0) { /* Align 64 bit arguments. */
  620. IRRef op2 = nir->op2;
  621. nir->op2 = REF_NIL;
  622. nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2);
  623. nir = IR(nref);
  624. }
  625. #endif
  626. #if LJ_BE
  627. { IRRef tmp = nir->op2; nir->op2 = hiref; hiref = tmp; }
  628. #endif
  629. ir->prev = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, hiref);
  630. }
  631. } else if (ir->o == IR_CNEWI) {
  632. if (hisubst[ir->op2])
  633. split_emit(J, IRT(IR_HIOP, IRT_NIL), nref, hisubst[ir->op2]);
  634. } else if (ir->o == IR_LOOP) {
  635. J->loopref = nref; /* Needed by assembler. */
  636. }
  637. hisubst[ref] = hi; /* Store hiword substitution. */
  638. }
  639. if (snref == nins) { /* Substitution for last snapshot. */
  640. snap->ref = J->cur.nins;
  641. split_subst_snap(J, snap, oir);
  642. }
  643. /* Add PHI marks. */
  644. for (ref = J->cur.nins-1; ref >= REF_FIRST; ref--) {
  645. IRIns *ir = IR(ref);
  646. if (ir->o != IR_PHI) break;
  647. if (!irref_isk(ir->op1)) irt_setphi(IR(ir->op1)->t);
  648. if (ir->op2 > J->loopref) irt_setphi(IR(ir->op2)->t);
  649. }
  650. }
  651. /* Protected callback for split pass. */
  652. static TValue *cpsplit(lua_State *L, lua_CFunction dummy, void *ud)
  653. {
  654. jit_State *J = (jit_State *)ud;
  655. split_ir(J);
  656. UNUSED(L); UNUSED(dummy);
  657. return NULL;
  658. }
  659. #if defined(LUA_USE_ASSERT) || LJ_SOFTFP
  660. /* Slow, but sure way to check whether a SPLIT pass is needed. */
  661. static int split_needsplit(jit_State *J)
  662. {
  663. IRIns *ir, *irend;
  664. IRRef ref;
  665. for (ir = IR(REF_FIRST), irend = IR(J->cur.nins); ir < irend; ir++)
  666. if (LJ_SOFTFP ? irt_is64orfp(ir->t) : irt_isint64(ir->t))
  667. return 1;
  668. if (LJ_SOFTFP) {
  669. for (ref = J->chain[IR_SLOAD]; ref; ref = IR(ref)->prev)
  670. if ((IR(ref)->op2 & IRSLOAD_CONVERT))
  671. return 1;
  672. if (J->chain[IR_TOBIT])
  673. return 1;
  674. }
  675. for (ref = J->chain[IR_CONV]; ref; ref = IR(ref)->prev) {
  676. IRType st = (IR(ref)->op2 & IRCONV_SRCMASK);
  677. if ((LJ_SOFTFP && (st == IRT_NUM || st == IRT_FLOAT)) ||
  678. st == IRT_I64 || st == IRT_U64)
  679. return 1;
  680. }
  681. return 0; /* Nope. */
  682. }
  683. #endif
  684. /* SPLIT pass. */
  685. void lj_opt_split(jit_State *J)
  686. {
  687. #if LJ_SOFTFP
  688. if (!J->needsplit)
  689. J->needsplit = split_needsplit(J);
  690. #else
  691. lua_assert(J->needsplit >= split_needsplit(J)); /* Verify flag. */
  692. #endif
  693. if (J->needsplit) {
  694. int errcode = lj_vm_cpcall(J->L, NULL, J, cpsplit);
  695. if (errcode) {
  696. /* Completely reset the trace to avoid inconsistent dump on abort. */
  697. J->cur.nins = J->cur.nk = REF_BASE;
  698. J->cur.nsnap = 0;
  699. lj_err_throw(J->L, errcode); /* Propagate errors. */
  700. }
  701. }
  702. }
  703. #undef IR
  704. #endif