aboutsummaryrefslogtreecommitdiff
path: root/src/lj_ir.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_ir.c')
-rw-r--r--src/lj_ir.c174
1 files changed, 86 insertions, 88 deletions
diff --git a/src/lj_ir.c b/src/lj_ir.c
index 38f289cb..3ce29954 100644
--- a/src/lj_ir.c
+++ b/src/lj_ir.c
@@ -15,6 +15,7 @@
15#if LJ_HASJIT 15#if LJ_HASJIT
16 16
17#include "lj_gc.h" 17#include "lj_gc.h"
18#include "lj_buf.h"
18#include "lj_str.h" 19#include "lj_str.h"
19#include "lj_tab.h" 20#include "lj_tab.h"
20#include "lj_ir.h" 21#include "lj_ir.h"
@@ -29,14 +30,15 @@
29#endif 30#endif
30#include "lj_vm.h" 31#include "lj_vm.h"
31#include "lj_strscan.h" 32#include "lj_strscan.h"
32#include "lj_lib.h" 33#include "lj_strfmt.h"
34#include "lj_prng.h"
33 35
34/* Some local macros to save typing. Undef'd at the end. */ 36/* Some local macros to save typing. Undef'd at the end. */
35#define IR(ref) (&J->cur.ir[(ref)]) 37#define IR(ref) (&J->cur.ir[(ref)])
36#define fins (&J->fold.ins) 38#define fins (&J->fold.ins)
37 39
38/* Pass IR on to next optimization in chain (FOLD). */ 40/* Pass IR on to next optimization in chain (FOLD). */
39#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) 41#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
40 42
41/* -- IR tables ----------------------------------------------------------- */ 43/* -- IR tables ----------------------------------------------------------- */
42 44
@@ -88,8 +90,9 @@ static void lj_ir_growbot(jit_State *J)
88{ 90{
89 IRIns *baseir = J->irbuf + J->irbotlim; 91 IRIns *baseir = J->irbuf + J->irbotlim;
90 MSize szins = J->irtoplim - J->irbotlim; 92 MSize szins = J->irtoplim - J->irbotlim;
91 lua_assert(szins != 0); 93 lj_assertJ(szins != 0, "zero IR size");
92 lua_assert(J->cur.nk == J->irbotlim); 94 lj_assertJ(J->cur.nk == J->irbotlim || J->cur.nk-1 == J->irbotlim,
95 "unexpected IR growth");
93 if (J->cur.nins + (szins >> 1) < J->irtoplim) { 96 if (J->cur.nins + (szins >> 1) < J->irtoplim) {
94 /* More than half of the buffer is free on top: shift up by a quarter. */ 97 /* More than half of the buffer is free on top: shift up by a quarter. */
95 MSize ofs = szins >> 2; 98 MSize ofs = szins >> 2;
@@ -143,6 +146,17 @@ TRef lj_ir_call(jit_State *J, IRCallID id, ...)
143 return emitir(CCI_OPTYPE(ci), tr, id); 146 return emitir(CCI_OPTYPE(ci), tr, id);
144} 147}
145 148
149/* Load field of type t from GG_State + offset. Must be 32 bit aligned. */
150LJ_FUNC TRef lj_ir_ggfload(jit_State *J, IRType t, uintptr_t ofs)
151{
152 lj_assertJ((ofs & 3) == 0, "unaligned GG_State field offset");
153 ofs >>= 2;
154 lj_assertJ(ofs >= IRFL__MAX && ofs <= 0x3ff,
155 "GG_State field offset breaks 10 bit FOLD key limit");
156 lj_ir_set(J, IRT(IR_FLOAD, t), REF_NIL, ofs);
157 return lj_opt_fold(J);
158}
159
146/* -- Interning of constants ---------------------------------------------- */ 160/* -- Interning of constants ---------------------------------------------- */
147 161
148/* 162/*
@@ -163,6 +177,24 @@ static LJ_AINLINE IRRef ir_nextk(jit_State *J)
163 return ref; 177 return ref;
164} 178}
165 179
180/* Get ref of next 64 bit IR constant and optionally grow IR.
181** Note: this may invalidate all IRIns *!
182*/
183static LJ_AINLINE IRRef ir_nextk64(jit_State *J)
184{
185 IRRef ref = J->cur.nk - 2;
186 lj_assertJ(J->state != LJ_TRACE_ASM, "bad JIT state");
187 if (LJ_UNLIKELY(ref < J->irbotlim)) lj_ir_growbot(J);
188 J->cur.nk = ref;
189 return ref;
190}
191
192#if LJ_GC64
193#define ir_nextkgc ir_nextk64
194#else
195#define ir_nextkgc ir_nextk
196#endif
197
166/* Intern int32_t constant. */ 198/* Intern int32_t constant. */
167TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k) 199TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k)
168{ 200{
@@ -182,79 +214,21 @@ found:
182 return TREF(ref, IRT_INT); 214 return TREF(ref, IRT_INT);
183} 215}
184 216
185/* The MRef inside the KNUM/KINT64 IR instructions holds the address of the 217/* Intern 64 bit constant, given by its 64 bit pattern. */
186** 64 bit constant. The constants themselves are stored in a chained array 218TRef lj_ir_k64(jit_State *J, IROp op, uint64_t u64)
187** and shared across traces.
188**
189** Rationale for choosing this data structure:
190** - The address of the constants is embedded in the generated machine code
191** and must never move. A resizable array or hash table wouldn't work.
192** - Most apps need very few non-32 bit integer constants (less than a dozen).
193** - Linear search is hard to beat in terms of speed and low complexity.
194*/
195typedef struct K64Array {
196 MRef next; /* Pointer to next list. */
197 MSize numk; /* Number of used elements in this array. */
198 TValue k[LJ_MIN_K64SZ]; /* Array of constants. */
199} K64Array;
200
201/* Free all chained arrays. */
202void lj_ir_k64_freeall(jit_State *J)
203{
204 K64Array *k;
205 for (k = mref(J->k64, K64Array); k; ) {
206 K64Array *next = mref(k->next, K64Array);
207 lj_mem_free(J2G(J), k, sizeof(K64Array));
208 k = next;
209 }
210}
211
212/* Find 64 bit constant in chained array or add it. */
213cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64)
214{
215 K64Array *k, *kp = NULL;
216 TValue *ntv;
217 MSize idx;
218 /* Search for the constant in the whole chain of arrays. */
219 for (k = mref(J->k64, K64Array); k; k = mref(k->next, K64Array)) {
220 kp = k; /* Remember previous element in list. */
221 for (idx = 0; idx < k->numk; idx++) { /* Search one array. */
222 TValue *tv = &k->k[idx];
223 if (tv->u64 == u64) /* Needed for +-0/NaN/absmask. */
224 return tv;
225 }
226 }
227 /* Constant was not found, need to add it. */
228 if (!(kp && kp->numk < LJ_MIN_K64SZ)) { /* Allocate a new array. */
229 K64Array *kn = lj_mem_newt(J->L, sizeof(K64Array), K64Array);
230 setmref(kn->next, NULL);
231 kn->numk = 0;
232 if (kp)
233 setmref(kp->next, kn); /* Chain to the end of the list. */
234 else
235 setmref(J->k64, kn); /* Link first array. */
236 kp = kn;
237 }
238 ntv = &kp->k[kp->numk++]; /* Add to current array. */
239 ntv->u64 = u64;
240 return ntv;
241}
242
243/* Intern 64 bit constant, given by its address. */
244TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv)
245{ 219{
246 IRIns *ir, *cir = J->cur.ir; 220 IRIns *ir, *cir = J->cur.ir;
247 IRRef ref; 221 IRRef ref;
248 IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64; 222 IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64;
249 for (ref = J->chain[op]; ref; ref = cir[ref].prev) 223 for (ref = J->chain[op]; ref; ref = cir[ref].prev)
250 if (ir_k64(&cir[ref]) == tv) 224 if (ir_k64(&cir[ref])->u64 == u64)
251 goto found; 225 goto found;
252 ref = ir_nextk(J); 226 ref = ir_nextk64(J);
253 ir = IR(ref); 227 ir = IR(ref);
254 lua_assert(checkptr32(tv)); 228 ir[1].tv.u64 = u64;
255 setmref(ir->ptr, tv);
256 ir->t.irt = t; 229 ir->t.irt = t;
257 ir->o = op; 230 ir->o = op;
231 ir->op12 = 0;
258 ir->prev = J->chain[op]; 232 ir->prev = J->chain[op];
259 J->chain[op] = (IRRef1)ref; 233 J->chain[op] = (IRRef1)ref;
260found: 234found:
@@ -264,13 +238,13 @@ found:
264/* Intern FP constant, given by its 64 bit pattern. */ 238/* Intern FP constant, given by its 64 bit pattern. */
265TRef lj_ir_knum_u64(jit_State *J, uint64_t u64) 239TRef lj_ir_knum_u64(jit_State *J, uint64_t u64)
266{ 240{
267 return lj_ir_k64(J, IR_KNUM, lj_ir_k64_find(J, u64)); 241 return lj_ir_k64(J, IR_KNUM, u64);
268} 242}
269 243
270/* Intern 64 bit integer constant. */ 244/* Intern 64 bit integer constant. */
271TRef lj_ir_kint64(jit_State *J, uint64_t u64) 245TRef lj_ir_kint64(jit_State *J, uint64_t u64)
272{ 246{
273 return lj_ir_k64(J, IR_KINT64, lj_ir_k64_find(J, u64)); 247 return lj_ir_k64(J, IR_KINT64, u64);
274} 248}
275 249
276/* Check whether a number is int and return it. -0 is NOT considered an int. */ 250/* Check whether a number is int and return it. -0 is NOT considered an int. */
@@ -305,14 +279,15 @@ TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t)
305{ 279{
306 IRIns *ir, *cir = J->cur.ir; 280 IRIns *ir, *cir = J->cur.ir;
307 IRRef ref; 281 IRRef ref;
308 lua_assert(!isdead(J2G(J), o)); 282 lj_assertJ(!isdead(J2G(J), o), "interning of dead GC object");
309 for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev) 283 for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev)
310 if (ir_kgc(&cir[ref]) == o) 284 if (ir_kgc(&cir[ref]) == o)
311 goto found; 285 goto found;
312 ref = ir_nextk(J); 286 ref = ir_nextkgc(J);
313 ir = IR(ref); 287 ir = IR(ref);
314 /* NOBARRIER: Current trace is a GC root. */ 288 /* NOBARRIER: Current trace is a GC root. */
315 setgcref(ir->gcr, o); 289 ir->op12 = 0;
290 setgcref(ir[LJ_GC64].gcr, o);
316 ir->t.irt = (uint8_t)t; 291 ir->t.irt = (uint8_t)t;
317 ir->o = IR_KGC; 292 ir->o = IR_KGC;
318 ir->prev = J->chain[IR_KGC]; 293 ir->prev = J->chain[IR_KGC];
@@ -321,24 +296,44 @@ found:
321 return TREF(ref, t); 296 return TREF(ref, t);
322} 297}
323 298
324/* Intern 32 bit pointer constant. */ 299/* Allocate GCtrace constant placeholder (no interning). */
300TRef lj_ir_ktrace(jit_State *J)
301{
302 IRRef ref = ir_nextkgc(J);
303 IRIns *ir = IR(ref);
304 lj_assertJ(irt_toitype_(IRT_P64) == LJ_TTRACE, "mismatched type mapping");
305 ir->t.irt = IRT_P64;
306 ir->o = LJ_GC64 ? IR_KNUM : IR_KNULL; /* Not IR_KGC yet, but same size. */
307 ir->op12 = 0;
308 ir->prev = 0;
309 return TREF(ref, IRT_P64);
310}
311
312/* Intern pointer constant. */
325TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr) 313TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr)
326{ 314{
327 IRIns *ir, *cir = J->cur.ir; 315 IRIns *ir, *cir = J->cur.ir;
328 IRRef ref; 316 IRRef ref;
329 lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr); 317#if LJ_64 && !LJ_GC64
318 lj_assertJ((void *)(uintptr_t)u32ptr(ptr) == ptr, "out-of-range GC pointer");
319#endif
330 for (ref = J->chain[op]; ref; ref = cir[ref].prev) 320 for (ref = J->chain[op]; ref; ref = cir[ref].prev)
331 if (mref(cir[ref].ptr, void) == ptr) 321 if (ir_kptr(&cir[ref]) == ptr)
332 goto found; 322 goto found;
323#if LJ_GC64
324 ref = ir_nextk64(J);
325#else
333 ref = ir_nextk(J); 326 ref = ir_nextk(J);
327#endif
334 ir = IR(ref); 328 ir = IR(ref);
335 setmref(ir->ptr, ptr); 329 ir->op12 = 0;
336 ir->t.irt = IRT_P32; 330 setmref(ir[LJ_GC64].ptr, ptr);
331 ir->t.irt = IRT_PGC;
337 ir->o = op; 332 ir->o = op;
338 ir->prev = J->chain[op]; 333 ir->prev = J->chain[op];
339 J->chain[op] = (IRRef1)ref; 334 J->chain[op] = (IRRef1)ref;
340found: 335found:
341 return TREF(ref, IRT_P32); 336 return TREF(ref, IRT_PGC);
342} 337}
343 338
344/* Intern typed NULL constant. */ 339/* Intern typed NULL constant. */
@@ -367,7 +362,8 @@ TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot)
367 IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot); 362 IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot);
368 IRRef ref; 363 IRRef ref;
369 /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */ 364 /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */
370 lua_assert(tref_isk(key) && slot == (IRRef)(IRRef1)slot); 365 lj_assertJ(tref_isk(key) && slot == (IRRef)(IRRef1)slot,
366 "out-of-range key/slot");
371 for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev) 367 for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev)
372 if (cir[ref].op12 == op12) 368 if (cir[ref].op12 == op12)
373 goto found; 369 goto found;
@@ -388,14 +384,15 @@ found:
388void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir) 384void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir)
389{ 385{
390 UNUSED(L); 386 UNUSED(L);
391 lua_assert(ir->o != IR_KSLOT); /* Common mistake. */ 387 lj_assertL(ir->o != IR_KSLOT, "unexpected KSLOT"); /* Common mistake. */
392 switch (ir->o) { 388 switch (ir->o) {
393 case IR_KPRI: setitype(tv, irt_toitype(ir->t)); break; 389 case IR_KPRI: setpriV(tv, irt_toitype(ir->t)); break;
394 case IR_KINT: setintV(tv, ir->i); break; 390 case IR_KINT: setintV(tv, ir->i); break;
395 case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break; 391 case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break;
396 case IR_KPTR: case IR_KKPTR: case IR_KNULL: 392 case IR_KPTR: case IR_KKPTR:
397 setlightudV(tv, mref(ir->ptr, void)); 393 setnumV(tv, (lua_Number)(uintptr_t)ir_kptr(ir));
398 break; 394 break;
395 case IR_KNULL: setintV(tv, 0); break;
399 case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break; 396 case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break;
400#if LJ_HASFFI 397#if LJ_HASFFI
401 case IR_KINT64: { 398 case IR_KINT64: {
@@ -405,7 +402,7 @@ void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir)
405 break; 402 break;
406 } 403 }
407#endif 404#endif
408 default: lua_assert(0); break; 405 default: lj_assertL(0, "bad IR constant op %d", ir->o); break;
409 } 406 }
410} 407}
411 408
@@ -443,7 +440,8 @@ TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr)
443 if (!tref_isstr(tr)) { 440 if (!tref_isstr(tr)) {
444 if (!tref_isnumber(tr)) 441 if (!tref_isnumber(tr))
445 lj_trace_err(J, LJ_TRERR_BADTYPE); 442 lj_trace_err(J, LJ_TRERR_BADTYPE);
446 tr = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); 443 tr = emitir(IRT(IR_TOSTR, IRT_STR), tr,
444 tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);
447 } 445 }
448 return tr; 446 return tr;
449} 447}
@@ -464,7 +462,7 @@ int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op)
464 case IR_UGE: return !(a < b); 462 case IR_UGE: return !(a < b);
465 case IR_ULE: return !(a > b); 463 case IR_ULE: return !(a > b);
466 case IR_UGT: return !(a <= b); 464 case IR_UGT: return !(a <= b);
467 default: lua_assert(0); return 0; 465 default: lj_assertX(0, "bad IR op %d", op); return 0;
468 } 466 }
469} 467}
470 468
@@ -477,7 +475,7 @@ int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op)
477 case IR_GE: return (res >= 0); 475 case IR_GE: return (res >= 0);
478 case IR_LE: return (res <= 0); 476 case IR_LE: return (res <= 0);
479 case IR_GT: return (res > 0); 477 case IR_GT: return (res > 0);
480 default: lua_assert(0); return 0; 478 default: lj_assertX(0, "bad IR op %d", op); return 0;
481 } 479 }
482} 480}
483 481