lj_ir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. ** SSA IR (Intermediate Representation) emitter.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_ir_c
  6. #define LUA_CORE
  7. /* For pointers to libc/libm functions. */
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "lj_obj.h"
  11. #if LJ_HASJIT
  12. #include "lj_gc.h"
  13. #include "lj_buf.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_ir.h"
  17. #include "lj_jit.h"
  18. #include "lj_ircall.h"
  19. #include "lj_iropt.h"
  20. #include "lj_trace.h"
  21. #if LJ_HASFFI
  22. #include "lj_ctype.h"
  23. #include "lj_cdata.h"
  24. #include "lj_carith.h"
  25. #endif
  26. #include "lj_vm.h"
  27. #include "lj_strscan.h"
  28. #include "lj_serialize.h"
  29. #include "lj_strfmt.h"
  30. #include "lj_prng.h"
  31. /* Some local macros to save typing. Undef'd at the end. */
  32. #define IR(ref) (&J->cur.ir[(ref)])
  33. #define fins (&J->fold.ins)
  34. /* Pass IR on to next optimization in chain (FOLD). */
  35. #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
  36. /* -- IR tables ----------------------------------------------------------- */
  37. /* IR instruction modes. */
  38. LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = {
  39. IRDEF(IRMODE)
  40. 0
  41. };
  42. /* IR type sizes. */
  43. LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = {
  44. #define IRTSIZE(name, size) size,
  45. IRTDEF(IRTSIZE)
  46. #undef IRTSIZE
  47. 0
  48. };
  49. /* C call info for CALL* instructions. */
  50. LJ_DATADEF const CCallInfo lj_ir_callinfo[] = {
  51. #define IRCALLCI(cond, name, nargs, kind, type, flags) \
  52. { (ASMFunction)IRCALLCOND_##cond(name), \
  53. (nargs)|(CCI_CALL_##kind)|(IRT_##type<<CCI_OTSHIFT)|(flags) },
  54. IRCALLDEF(IRCALLCI)
  55. #undef IRCALLCI
  56. { NULL, 0 }
  57. };
  58. /* -- IR emitter ---------------------------------------------------------- */
  59. /* Grow IR buffer at the top. */
  60. void LJ_FASTCALL lj_ir_growtop(jit_State *J)
  61. {
  62. IRIns *baseir = J->irbuf + J->irbotlim;
  63. MSize szins = J->irtoplim - J->irbotlim;
  64. if (szins) {
  65. baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns),
  66. 2*szins*sizeof(IRIns));
  67. J->irtoplim = J->irbotlim + 2*szins;
  68. } else {
  69. baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns));
  70. J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4;
  71. J->irtoplim = J->irbotlim + LJ_MIN_IRSZ;
  72. }
  73. J->cur.ir = J->irbuf = baseir - J->irbotlim;
  74. }
  75. /* Grow IR buffer at the bottom or shift it up. */
  76. static void lj_ir_growbot(jit_State *J)
  77. {
  78. IRIns *baseir = J->irbuf + J->irbotlim;
  79. MSize szins = J->irtoplim - J->irbotlim;
  80. lj_assertJ(szins != 0, "zero IR size");
  81. lj_assertJ(J->cur.nk == J->irbotlim || J->cur.nk-1 == J->irbotlim,
  82. "unexpected IR growth");
  83. if (J->cur.nins + (szins >> 1) < J->irtoplim) {
  84. /* More than half of the buffer is free on top: shift up by a quarter. */
  85. MSize ofs = szins >> 2;
  86. memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));
  87. J->irbotlim -= ofs;
  88. J->irtoplim -= ofs;
  89. J->cur.ir = J->irbuf = baseir - J->irbotlim;
  90. } else {
  91. /* Double the buffer size, but split the growth amongst top/bottom. */
  92. IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns);
  93. MSize ofs = szins >= 256 ? 128 : (szins >> 1); /* Limit bottom growth. */
  94. memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));
  95. lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns));
  96. J->irbotlim -= ofs;
  97. J->irtoplim = J->irbotlim + 2*szins;
  98. J->cur.ir = J->irbuf = newbase - J->irbotlim;
  99. }
  100. }
  101. /* Emit IR without any optimizations. */
  102. TRef LJ_FASTCALL lj_ir_emit(jit_State *J)
  103. {
  104. IRRef ref = lj_ir_nextins(J);
  105. IRIns *ir = IR(ref);
  106. IROp op = fins->o;
  107. ir->prev = J->chain[op];
  108. J->chain[op] = (IRRef1)ref;
  109. ir->o = op;
  110. ir->op1 = fins->op1;
  111. ir->op2 = fins->op2;
  112. J->guardemit.irt |= fins->t.irt;
  113. return TREF(ref, irt_t((ir->t = fins->t)));
  114. }
  115. /* Emit call to a C function. */
  116. TRef lj_ir_call(jit_State *J, IRCallID id, ...)
  117. {
  118. const CCallInfo *ci = &lj_ir_callinfo[id];
  119. uint32_t n = CCI_NARGS(ci);
  120. TRef tr = TREF_NIL;
  121. va_list argp;
  122. va_start(argp, id);
  123. if ((ci->flags & CCI_L)) n--;
  124. if (n > 0)
  125. tr = va_arg(argp, IRRef);
  126. while (n-- > 1)
  127. tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef));
  128. va_end(argp);
  129. if (CCI_OP(ci) == IR_CALLS)
  130. J->needsnap = 1; /* Need snapshot after call with side effect. */
  131. return emitir(CCI_OPTYPE(ci), tr, id);
  132. }
  133. /* Load field of type t from GG_State + offset. Must be 32 bit aligned. */
  134. TRef lj_ir_ggfload(jit_State *J, IRType t, uintptr_t ofs)
  135. {
  136. lj_assertJ((ofs & 3) == 0, "unaligned GG_State field offset");
  137. ofs >>= 2;
  138. lj_assertJ(ofs >= IRFL__MAX && ofs <= 0x3ff,
  139. "GG_State field offset breaks 10 bit FOLD key limit");
  140. lj_ir_set(J, IRT(IR_FLOAD, t), REF_NIL, ofs);
  141. return lj_opt_fold(J);
  142. }
  143. /* -- Interning of constants ---------------------------------------------- */
  144. /*
  145. ** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS.
  146. ** They are chained like all other instructions, but grow downwards.
  147. ** The are interned (like strings in the VM) to facilitate reference
  148. ** comparisons. The same constant must get the same reference.
  149. */
  150. /* Get ref of next IR constant and optionally grow IR.
  151. ** Note: this may invalidate all IRIns *!
  152. */
  153. static LJ_AINLINE IRRef ir_nextk(jit_State *J)
  154. {
  155. IRRef ref = J->cur.nk;
  156. if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J);
  157. J->cur.nk = --ref;
  158. return ref;
  159. }
  160. /* Get ref of next 64 bit IR constant and optionally grow IR.
  161. ** Note: this may invalidate all IRIns *!
  162. */
  163. static LJ_AINLINE IRRef ir_nextk64(jit_State *J)
  164. {
  165. IRRef ref = J->cur.nk - 2;
  166. lj_assertJ(J->state != LJ_TRACE_ASM, "bad JIT state");
  167. if (LJ_UNLIKELY(ref < J->irbotlim)) lj_ir_growbot(J);
  168. J->cur.nk = ref;
  169. return ref;
  170. }
  171. #if LJ_GC64
  172. #define ir_nextkgc ir_nextk64
  173. #else
  174. #define ir_nextkgc ir_nextk
  175. #endif
  176. /* Intern int32_t constant. */
  177. TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k)
  178. {
  179. IRIns *ir, *cir = J->cur.ir;
  180. IRRef ref;
  181. for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev)
  182. if (cir[ref].i == k)
  183. goto found;
  184. ref = ir_nextk(J);
  185. ir = IR(ref);
  186. ir->i = k;
  187. ir->t.irt = IRT_INT;
  188. ir->o = IR_KINT;
  189. ir->prev = J->chain[IR_KINT];
  190. J->chain[IR_KINT] = (IRRef1)ref;
  191. found:
  192. return TREF(ref, IRT_INT);
  193. }
  194. /* Intern 64 bit constant, given by its 64 bit pattern. */
  195. TRef lj_ir_k64(jit_State *J, IROp op, uint64_t u64)
  196. {
  197. IRIns *ir, *cir = J->cur.ir;
  198. IRRef ref;
  199. IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64;
  200. for (ref = J->chain[op]; ref; ref = cir[ref].prev)
  201. if (ir_k64(&cir[ref])->u64 == u64)
  202. goto found;
  203. ref = ir_nextk64(J);
  204. ir = IR(ref);
  205. ir[1].tv.u64 = u64;
  206. ir->t.irt = t;
  207. ir->o = op;
  208. ir->op12 = 0;
  209. ir->prev = J->chain[op];
  210. J->chain[op] = (IRRef1)ref;
  211. found:
  212. return TREF(ref, t);
  213. }
  214. /* Intern FP constant, given by its 64 bit pattern. */
  215. TRef lj_ir_knum_u64(jit_State *J, uint64_t u64)
  216. {
  217. return lj_ir_k64(J, IR_KNUM, u64);
  218. }
  219. /* Intern 64 bit integer constant. */
  220. TRef lj_ir_kint64(jit_State *J, uint64_t u64)
  221. {
  222. return lj_ir_k64(J, IR_KINT64, u64);
  223. }
  224. /* Check whether a number is int and return it. -0 is NOT considered an int. */
  225. static int numistrueint(lua_Number n, int32_t *kp)
  226. {
  227. int32_t k = lj_num2int(n);
  228. if (n == (lua_Number)k) {
  229. if (kp) *kp = k;
  230. if (k == 0) { /* Special check for -0. */
  231. TValue tv;
  232. setnumV(&tv, n);
  233. if (tv.u32.hi != 0)
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. /* Intern number as int32_t constant if possible, otherwise as FP constant. */
  241. TRef lj_ir_knumint(jit_State *J, lua_Number n)
  242. {
  243. int32_t k;
  244. if (numistrueint(n, &k))
  245. return lj_ir_kint(J, k);
  246. else
  247. return lj_ir_knum(J, n);
  248. }
  249. /* Intern GC object "constant". */
  250. TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t)
  251. {
  252. IRIns *ir, *cir = J->cur.ir;
  253. IRRef ref;
  254. lj_assertJ(!isdead(J2G(J), o), "interning of dead GC object");
  255. for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev)
  256. if (ir_kgc(&cir[ref]) == o)
  257. goto found;
  258. ref = ir_nextkgc(J);
  259. ir = IR(ref);
  260. /* NOBARRIER: Current trace is a GC root. */
  261. ir->op12 = 0;
  262. setgcref(ir[LJ_GC64].gcr, o);
  263. ir->t.irt = (uint8_t)t;
  264. ir->o = IR_KGC;
  265. ir->prev = J->chain[IR_KGC];
  266. J->chain[IR_KGC] = (IRRef1)ref;
  267. found:
  268. return TREF(ref, t);
  269. }
  270. /* Allocate GCtrace constant placeholder (no interning). */
  271. TRef lj_ir_ktrace(jit_State *J)
  272. {
  273. IRRef ref = ir_nextkgc(J);
  274. IRIns *ir = IR(ref);
  275. lj_assertJ(irt_toitype_(IRT_P64) == LJ_TTRACE, "mismatched type mapping");
  276. ir->t.irt = IRT_P64;
  277. ir->o = LJ_GC64 ? IR_KNUM : IR_KNULL; /* Not IR_KGC yet, but same size. */
  278. ir->op12 = 0;
  279. ir->prev = 0;
  280. return TREF(ref, IRT_P64);
  281. }
  282. /* Intern pointer constant. */
  283. TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr)
  284. {
  285. IRIns *ir, *cir = J->cur.ir;
  286. IRRef ref;
  287. #if LJ_64 && !LJ_GC64
  288. lj_assertJ((void *)(uintptr_t)u32ptr(ptr) == ptr, "out-of-range GC pointer");
  289. #endif
  290. for (ref = J->chain[op]; ref; ref = cir[ref].prev)
  291. if (ir_kptr(&cir[ref]) == ptr)
  292. goto found;
  293. #if LJ_GC64
  294. ref = ir_nextk64(J);
  295. #else
  296. ref = ir_nextk(J);
  297. #endif
  298. ir = IR(ref);
  299. ir->op12 = 0;
  300. setmref(ir[LJ_GC64].ptr, ptr);
  301. ir->t.irt = IRT_PGC;
  302. ir->o = op;
  303. ir->prev = J->chain[op];
  304. J->chain[op] = (IRRef1)ref;
  305. found:
  306. return TREF(ref, IRT_PGC);
  307. }
  308. /* Intern typed NULL constant. */
  309. TRef lj_ir_knull(jit_State *J, IRType t)
  310. {
  311. IRIns *ir, *cir = J->cur.ir;
  312. IRRef ref;
  313. for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev)
  314. if (irt_t(cir[ref].t) == t)
  315. goto found;
  316. ref = ir_nextk(J);
  317. ir = IR(ref);
  318. ir->i = 0;
  319. ir->t.irt = (uint8_t)t;
  320. ir->o = IR_KNULL;
  321. ir->prev = J->chain[IR_KNULL];
  322. J->chain[IR_KNULL] = (IRRef1)ref;
  323. found:
  324. return TREF(ref, t);
  325. }
  326. /* Intern key slot. */
  327. TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot)
  328. {
  329. IRIns *ir, *cir = J->cur.ir;
  330. IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot);
  331. IRRef ref;
  332. /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */
  333. lj_assertJ(tref_isk(key) && slot == (IRRef)(IRRef1)slot,
  334. "out-of-range key/slot");
  335. for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev)
  336. if (cir[ref].op12 == op12)
  337. goto found;
  338. ref = ir_nextk(J);
  339. ir = IR(ref);
  340. ir->op12 = op12;
  341. ir->t.irt = IRT_P32;
  342. ir->o = IR_KSLOT;
  343. ir->prev = J->chain[IR_KSLOT];
  344. J->chain[IR_KSLOT] = (IRRef1)ref;
  345. found:
  346. return TREF(ref, IRT_P32);
  347. }
  348. /* -- Access to IR constants ---------------------------------------------- */
  349. /* Copy value of IR constant. */
  350. void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir)
  351. {
  352. UNUSED(L);
  353. lj_assertL(ir->o != IR_KSLOT, "unexpected KSLOT"); /* Common mistake. */
  354. switch (ir->o) {
  355. case IR_KPRI: setpriV(tv, irt_toitype(ir->t)); break;
  356. case IR_KINT: setintV(tv, ir->i); break;
  357. case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break;
  358. case IR_KPTR: case IR_KKPTR:
  359. setnumV(tv, (lua_Number)(uintptr_t)ir_kptr(ir));
  360. break;
  361. case IR_KNULL: setintV(tv, 0); break;
  362. case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break;
  363. #if LJ_HASFFI
  364. case IR_KINT64: {
  365. GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8);
  366. *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64;
  367. setcdataV(L, tv, cd);
  368. break;
  369. }
  370. #endif
  371. default: lj_assertL(0, "bad IR constant op %d", ir->o); break;
  372. }
  373. }
  374. /* -- Convert IR operand types -------------------------------------------- */
  375. /* Convert from string to number. */
  376. TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr)
  377. {
  378. if (!tref_isnumber(tr)) {
  379. if (tref_isstr(tr))
  380. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
  381. else
  382. lj_trace_err(J, LJ_TRERR_BADTYPE);
  383. }
  384. return tr;
  385. }
  386. /* Convert from integer or string to number. */
  387. TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr)
  388. {
  389. if (!tref_isnum(tr)) {
  390. if (tref_isinteger(tr))
  391. tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
  392. else if (tref_isstr(tr))
  393. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
  394. else
  395. lj_trace_err(J, LJ_TRERR_BADTYPE);
  396. }
  397. return tr;
  398. }
  399. /* Convert from integer or number to string. */
  400. TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr)
  401. {
  402. if (!tref_isstr(tr)) {
  403. if (!tref_isnumber(tr))
  404. lj_trace_err(J, LJ_TRERR_BADTYPE);
  405. tr = emitir(IRT(IR_TOSTR, IRT_STR), tr,
  406. tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);
  407. }
  408. return tr;
  409. }
  410. /* -- Miscellaneous IR ops ------------------------------------------------ */
  411. /* Evaluate numeric comparison. */
  412. int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op)
  413. {
  414. switch (op) {
  415. case IR_EQ: return (a == b);
  416. case IR_NE: return (a != b);
  417. case IR_LT: return (a < b);
  418. case IR_GE: return (a >= b);
  419. case IR_LE: return (a <= b);
  420. case IR_GT: return (a > b);
  421. case IR_ULT: return !(a >= b);
  422. case IR_UGE: return !(a < b);
  423. case IR_ULE: return !(a > b);
  424. case IR_UGT: return !(a <= b);
  425. default: lj_assertX(0, "bad IR op %d", op); return 0;
  426. }
  427. }
  428. /* Evaluate string comparison. */
  429. int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op)
  430. {
  431. int res = lj_str_cmp(a, b);
  432. switch (op) {
  433. case IR_LT: return (res < 0);
  434. case IR_GE: return (res >= 0);
  435. case IR_LE: return (res <= 0);
  436. case IR_GT: return (res > 0);
  437. default: lj_assertX(0, "bad IR op %d", op); return 0;
  438. }
  439. }
  440. /* Rollback IR to previous state. */
  441. void lj_ir_rollback(jit_State *J, IRRef ref)
  442. {
  443. IRRef nins = J->cur.nins;
  444. while (nins > ref) {
  445. IRIns *ir;
  446. nins--;
  447. ir = IR(nins);
  448. J->chain[ir->o] = ir->prev;
  449. }
  450. J->cur.nins = nins;
  451. }
  452. #undef IR
  453. #undef fins
  454. #undef emitir
  455. #endif