FFI: Fix code generation for replay of sunk float fields.
[luajit-2.0.git] / src / lj_opt_mem.c
blob560c4ade69e0badfd9152042ecc3acc39380ef44
1 /*
2 ** Memory access optimizations.
3 ** AA: Alias Analysis using high-level semantic disambiguation.
4 ** FWD: Load Forwarding (L2L) + Store Forwarding (S2L).
5 ** DSE: Dead-Store Elimination.
6 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
7 */
9 #define lj_opt_mem_c
10 #define LUA_CORE
12 #include "lj_obj.h"
14 #if LJ_HASJIT
16 #include "lj_tab.h"
17 #include "lj_ir.h"
18 #include "lj_jit.h"
19 #include "lj_iropt.h"
21 /* Some local macros to save typing. Undef'd at the end. */
22 #define IR(ref) (&J->cur.ir[(ref)])
23 #define fins (&J->fold.ins)
24 #define fleft (&J->fold.left)
25 #define fright (&J->fold.right)
28 ** Caveat #1: return value is not always a TRef -- only use with tref_ref().
29 ** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold().
32 /* Return values from alias analysis. */
33 typedef enum {
34 ALIAS_NO, /* The two refs CANNOT alias (exact). */
35 ALIAS_MAY, /* The two refs MAY alias (inexact). */
36 ALIAS_MUST /* The two refs MUST alias (exact). */
37 } AliasRet;
39 /* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */
41 /* Simplified escape analysis: check for intervening stores. */
42 static AliasRet aa_escape(jit_State *J, IRIns *ir, IRIns *stop)
44 IRRef ref = (IRRef)(ir - J->cur.ir); /* The ref that might be stored. */
45 for (ir++; ir < stop; ir++)
46 if (ir->op2 == ref &&
47 (ir->o == IR_ASTORE || ir->o == IR_HSTORE ||
48 ir->o == IR_USTORE || ir->o == IR_FSTORE))
49 return ALIAS_MAY; /* Reference was stored and might alias. */
50 return ALIAS_NO; /* Reference was not stored. */
53 /* Alias analysis for two different table references. */
54 static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb)
56 IRIns *taba = IR(ta), *tabb = IR(tb);
57 int newa, newb;
58 lua_assert(ta != tb);
59 lua_assert(irt_istab(taba->t) && irt_istab(tabb->t));
60 /* Disambiguate new allocations. */
61 newa = (taba->o == IR_TNEW || taba->o == IR_TDUP);
62 newb = (tabb->o == IR_TNEW || tabb->o == IR_TDUP);
63 if (newa && newb)
64 return ALIAS_NO; /* Two different allocations never alias. */
65 if (newb) { /* At least one allocation? */
66 IRIns *tmp = taba; taba = tabb; tabb = tmp;
67 } else if (!newa) {
68 return ALIAS_MAY; /* Anything else: we just don't know. */
70 return aa_escape(J, taba, tabb);
73 /* Alias analysis for array and hash access using key-based disambiguation. */
74 static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb)
76 IRRef ka = refa->op2;
77 IRRef kb = refb->op2;
78 IRIns *keya, *keyb;
79 IRRef ta, tb;
80 if (refa == refb)
81 return ALIAS_MUST; /* Shortcut for same refs. */
82 keya = IR(ka);
83 if (keya->o == IR_KSLOT) { ka = keya->op1; keya = IR(ka); }
84 keyb = IR(kb);
85 if (keyb->o == IR_KSLOT) { kb = keyb->op1; keyb = IR(kb); }
86 ta = (refa->o==IR_HREFK || refa->o==IR_AREF) ? IR(refa->op1)->op1 : refa->op1;
87 tb = (refb->o==IR_HREFK || refb->o==IR_AREF) ? IR(refb->op1)->op1 : refb->op1;
88 if (ka == kb) {
89 /* Same key. Check for same table with different ref (NEWREF vs. HREF). */
90 if (ta == tb)
91 return ALIAS_MUST; /* Same key, same table. */
92 else
93 return aa_table(J, ta, tb); /* Same key, possibly different table. */
95 if (irref_isk(ka) && irref_isk(kb))
96 return ALIAS_NO; /* Different constant keys. */
97 if (refa->o == IR_AREF) {
98 /* Disambiguate array references based on index arithmetic. */
99 int32_t ofsa = 0, ofsb = 0;
100 IRRef basea = ka, baseb = kb;
101 lua_assert(refb->o == IR_AREF);
102 /* Gather base and offset from t[base] or t[base+-ofs]. */
103 if (keya->o == IR_ADD && irref_isk(keya->op2)) {
104 basea = keya->op1;
105 ofsa = IR(keya->op2)->i;
106 if (basea == kb && ofsa != 0)
107 return ALIAS_NO; /* t[base+-ofs] vs. t[base]. */
109 if (keyb->o == IR_ADD && irref_isk(keyb->op2)) {
110 baseb = keyb->op1;
111 ofsb = IR(keyb->op2)->i;
112 if (ka == baseb && ofsb != 0)
113 return ALIAS_NO; /* t[base] vs. t[base+-ofs]. */
115 if (basea == baseb && ofsa != ofsb)
116 return ALIAS_NO; /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */
117 } else {
118 /* Disambiguate hash references based on the type of their keys. */
119 lua_assert((refa->o==IR_HREF || refa->o==IR_HREFK || refa->o==IR_NEWREF) &&
120 (refb->o==IR_HREF || refb->o==IR_HREFK || refb->o==IR_NEWREF));
121 if (!irt_sametype(keya->t, keyb->t))
122 return ALIAS_NO; /* Different key types. */
124 if (ta == tb)
125 return ALIAS_MAY; /* Same table, cannot disambiguate keys. */
126 else
127 return aa_table(J, ta, tb); /* Try to disambiguate tables. */
130 /* Array and hash load forwarding. */
131 static TRef fwd_ahload(jit_State *J, IRRef xref)
133 IRIns *xr = IR(xref);
134 IRRef lim = xref; /* Search limit. */
135 IRRef ref;
137 /* Search for conflicting stores. */
138 ref = J->chain[fins->o+IRDELTA_L2S];
139 while (ref > xref) {
140 IRIns *store = IR(ref);
141 switch (aa_ahref(J, xr, IR(store->op1))) {
142 case ALIAS_NO: break; /* Continue searching. */
143 case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */
144 case ALIAS_MUST: return store->op2; /* Store forwarding. */
146 ref = store->prev;
149 /* No conflicting store (yet): const-fold loads from allocations. */
151 IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr;
152 IRRef tab = ir->op1;
153 ir = IR(tab);
154 if (ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) {
155 /* A NEWREF with a number key may end up pointing to the array part.
156 ** But it's referenced from HSTORE and not found in the ASTORE chain.
157 ** For now simply consider this a conflict without forwarding anything.
159 if (xr->o == IR_AREF) {
160 IRRef ref2 = J->chain[IR_NEWREF];
161 while (ref2 > tab) {
162 IRIns *newref = IR(ref2);
163 if (irt_isnum(IR(newref->op2)->t))
164 goto cselim;
165 ref2 = newref->prev;
168 /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF.
169 ** But the above search for conflicting stores was limited by xref.
170 ** So continue searching, limited by the TNEW/TDUP. Store forwarding
171 ** is ok, too. A conflict does NOT limit the search for a matching load.
173 while (ref > tab) {
174 IRIns *store = IR(ref);
175 switch (aa_ahref(J, xr, IR(store->op1))) {
176 case ALIAS_NO: break; /* Continue searching. */
177 case ALIAS_MAY: goto cselim; /* Conflicting store. */
178 case ALIAS_MUST: return store->op2; /* Store forwarding. */
180 ref = store->prev;
182 lua_assert(ir->o != IR_TNEW || irt_isnil(fins->t));
183 if (irt_ispri(fins->t)) {
184 return TREF_PRI(irt_type(fins->t));
185 } else if (irt_isnum(fins->t) || (LJ_DUALNUM && irt_isint(fins->t)) ||
186 irt_isstr(fins->t)) {
187 TValue keyv;
188 cTValue *tv;
189 IRIns *key = IR(xr->op2);
190 if (key->o == IR_KSLOT) key = IR(key->op1);
191 lj_ir_kvalue(J->L, &keyv, key);
192 tv = lj_tab_get(J->L, ir_ktab(IR(ir->op1)), &keyv);
193 lua_assert(itype2irt(tv) == irt_type(fins->t));
194 if (irt_isnum(fins->t))
195 return lj_ir_knum_u64(J, tv->u64);
196 else if (LJ_DUALNUM && irt_isint(fins->t))
197 return lj_ir_kint(J, intV(tv));
198 else
199 return lj_ir_kstr(J, strV(tv));
201 /* Othwerwise: don't intern as a constant. */
205 cselim:
206 /* Try to find a matching load. Below the conflicting store, if any. */
207 ref = J->chain[fins->o];
208 while (ref > lim) {
209 IRIns *load = IR(ref);
210 if (load->op1 == xref)
211 return ref; /* Load forwarding. */
212 ref = load->prev;
214 return 0; /* Conflict or no match. */
217 /* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */
218 static TRef fwd_aload_reassoc(jit_State *J)
220 IRIns *irx = IR(fins->op1);
221 IRIns *key = IR(irx->op2);
222 if (key->o == IR_ADD && irref_isk(key->op2)) {
223 IRIns *add2 = IR(key->op1);
224 if (add2->o == IR_ADD && irref_isk(add2->op2) &&
225 IR(key->op2)->i == -IR(add2->op2)->i) {
226 IRRef ref = J->chain[IR_AREF];
227 IRRef lim = add2->op1;
228 if (irx->op1 > lim) lim = irx->op1;
229 while (ref > lim) {
230 IRIns *ir = IR(ref);
231 if (ir->op1 == irx->op1 && ir->op2 == add2->op1)
232 return fwd_ahload(J, ref);
233 ref = ir->prev;
237 return 0;
240 /* ALOAD forwarding. */
241 TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J)
243 IRRef ref;
244 if ((ref = fwd_ahload(J, fins->op1)) ||
245 (ref = fwd_aload_reassoc(J)))
246 return ref;
247 return EMITFOLD;
250 /* HLOAD forwarding. */
251 TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J)
253 IRRef ref = fwd_ahload(J, fins->op1);
254 if (ref)
255 return ref;
256 return EMITFOLD;
259 /* HREFK forwarding. */
260 TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J)
262 IRRef tab = fleft->op1;
263 IRRef ref = J->chain[IR_NEWREF];
264 while (ref > tab) {
265 IRIns *newref = IR(ref);
266 if (tab == newref->op1) {
267 if (fright->op1 == newref->op2)
268 return ref; /* Forward from NEWREF. */
269 else
270 goto docse;
271 } else if (aa_table(J, tab, newref->op1) != ALIAS_NO) {
272 goto docse;
274 ref = newref->prev;
276 /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */
277 if (IR(tab)->o == IR_TDUP)
278 fins->t.irt &= ~IRT_GUARD; /* Drop HREFK guard. */
279 docse:
280 return CSEFOLD;
283 /* Check whether HREF of TNEW/TDUP can be folded to niltv. */
284 int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J)
286 IRRef lim = fins->op1; /* Search limit. */
287 IRRef ref;
289 /* The key for an ASTORE may end up in the hash part after a NEWREF. */
290 if (irt_isnum(fright->t) && J->chain[IR_NEWREF] > lim) {
291 ref = J->chain[IR_ASTORE];
292 while (ref > lim) {
293 if (ref < J->chain[IR_NEWREF])
294 return 0; /* Conflict. */
295 ref = IR(ref)->prev;
299 /* Search for conflicting stores. */
300 ref = J->chain[IR_HSTORE];
301 while (ref > lim) {
302 IRIns *store = IR(ref);
303 if (aa_ahref(J, fins, IR(store->op1)) != ALIAS_NO)
304 return 0; /* Conflict. */
305 ref = store->prev;
308 return 1; /* No conflict. Can fold to niltv. */
311 /* Check whether there's no aliasing NEWREF for the left operand. */
312 int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim)
314 IRRef ta = fins->op1;
315 IRRef ref = J->chain[IR_NEWREF];
316 while (ref > lim) {
317 IRIns *newref = IR(ref);
318 if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO)
319 return 0; /* Conflict. */
320 ref = newref->prev;
322 return 1; /* No conflict. Can safely FOLD/CSE. */
325 /* ASTORE/HSTORE elimination. */
326 TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J)
328 IRRef xref = fins->op1; /* xREF reference. */
329 IRRef val = fins->op2; /* Stored value reference. */
330 IRIns *xr = IR(xref);
331 IRRef1 *refp = &J->chain[fins->o];
332 IRRef ref = *refp;
333 while (ref > xref) { /* Search for redundant or conflicting stores. */
334 IRIns *store = IR(ref);
335 switch (aa_ahref(J, xr, IR(store->op1))) {
336 case ALIAS_NO:
337 break; /* Continue searching. */
338 case ALIAS_MAY: /* Store to MAYBE the same location. */
339 if (store->op2 != val) /* Conflict if the value is different. */
340 goto doemit;
341 break; /* Otherwise continue searching. */
342 case ALIAS_MUST: /* Store to the same location. */
343 if (store->op2 == val) /* Same value: drop the new store. */
344 return DROPFOLD;
345 /* Different value: try to eliminate the redundant store. */
346 if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */
347 IRIns *ir;
348 /* Check for any intervening guards (includes conflicting loads). */
349 for (ir = IR(J->cur.nins-1); ir > store; ir--)
350 if (irt_isguard(ir->t))
351 goto doemit; /* No elimination possible. */
352 /* Remove redundant store from chain and replace with NOP. */
353 *refp = store->prev;
354 store->o = IR_NOP;
355 store->t.irt = IRT_NIL;
356 store->op1 = store->op2 = 0;
357 store->prev = 0;
358 /* Now emit the new store instead. */
360 goto doemit;
362 ref = *(refp = &store->prev);
364 doemit:
365 return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
368 /* -- ULOAD forwarding ---------------------------------------------------- */
370 /* The current alias analysis for upvalues is very simplistic. It only
371 ** disambiguates between the unique upvalues of the same function.
372 ** This is good enough for now, since most upvalues are read-only.
374 ** A more precise analysis would be feasible with the help of the parser:
375 ** generate a unique key for every upvalue, even across all prototypes.
376 ** Lacking a realistic use-case, it's unclear whether this is beneficial.
378 static AliasRet aa_uref(IRIns *refa, IRIns *refb)
380 if (refa->o != refb->o)
381 return ALIAS_NO; /* Different UREFx type. */
382 if (refa->op1 == refb->op1) { /* Same function. */
383 if (refa->op2 == refb->op2)
384 return ALIAS_MUST; /* Same function, same upvalue idx. */
385 else
386 return ALIAS_NO; /* Same function, different upvalue idx. */
387 } else { /* Different functions, check disambiguation hash values. */
388 if (((refa->op2 ^ refb->op2) & 0xff))
389 return ALIAS_NO; /* Upvalues with different hash values cannot alias. */
390 else
391 return ALIAS_MAY; /* No conclusion can be drawn for same hash value. */
395 /* ULOAD forwarding. */
396 TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J)
398 IRRef uref = fins->op1;
399 IRRef lim = uref; /* Search limit. */
400 IRIns *xr = IR(uref);
401 IRRef ref;
403 /* Search for conflicting stores. */
404 ref = J->chain[IR_USTORE];
405 while (ref > uref) {
406 IRIns *store = IR(ref);
407 switch (aa_uref(xr, IR(store->op1))) {
408 case ALIAS_NO: break; /* Continue searching. */
409 case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */
410 case ALIAS_MUST: return store->op2; /* Store forwarding. */
412 ref = store->prev;
415 cselim:
416 /* Try to find a matching load. Below the conflicting store, if any. */
417 return lj_opt_cselim(J, lim);
420 /* USTORE elimination. */
421 TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J)
423 IRRef xref = fins->op1; /* xREF reference. */
424 IRRef val = fins->op2; /* Stored value reference. */
425 IRIns *xr = IR(xref);
426 IRRef1 *refp = &J->chain[IR_USTORE];
427 IRRef ref = *refp;
428 while (ref > xref) { /* Search for redundant or conflicting stores. */
429 IRIns *store = IR(ref);
430 switch (aa_uref(xr, IR(store->op1))) {
431 case ALIAS_NO:
432 break; /* Continue searching. */
433 case ALIAS_MAY: /* Store to MAYBE the same location. */
434 if (store->op2 != val) /* Conflict if the value is different. */
435 goto doemit;
436 break; /* Otherwise continue searching. */
437 case ALIAS_MUST: /* Store to the same location. */
438 if (store->op2 == val) /* Same value: drop the new store. */
439 return DROPFOLD;
440 /* Different value: try to eliminate the redundant store. */
441 if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */
442 IRIns *ir;
443 /* Check for any intervening guards (includes conflicting loads). */
444 for (ir = IR(J->cur.nins-1); ir > store; ir--)
445 if (irt_isguard(ir->t))
446 goto doemit; /* No elimination possible. */
447 /* Remove redundant store from chain and replace with NOP. */
448 *refp = store->prev;
449 store->o = IR_NOP;
450 store->t.irt = IRT_NIL;
451 store->op1 = store->op2 = 0;
452 store->prev = 0;
453 if (ref+1 < J->cur.nins &&
454 store[1].o == IR_OBAR && store[1].op1 == xref) {
455 IRRef1 *bp = &J->chain[IR_OBAR];
456 IRIns *obar;
457 for (obar = IR(*bp); *bp > ref+1; obar = IR(*bp))
458 bp = &obar->prev;
459 /* Remove OBAR, too. */
460 *bp = obar->prev;
461 obar->o = IR_NOP;
462 obar->t.irt = IRT_NIL;
463 obar->op1 = obar->op2 = 0;
464 obar->prev = 0;
466 /* Now emit the new store instead. */
468 goto doemit;
470 ref = *(refp = &store->prev);
472 doemit:
473 return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
476 /* -- FLOAD forwarding and FSTORE elimination ----------------------------- */
478 /* Alias analysis for field access.
479 ** Field loads are cheap and field stores are rare.
480 ** Simple disambiguation based on field types is good enough.
482 static AliasRet aa_fref(jit_State *J, IRIns *refa, IRIns *refb)
484 if (refa->op2 != refb->op2)
485 return ALIAS_NO; /* Different fields. */
486 if (refa->op1 == refb->op1)
487 return ALIAS_MUST; /* Same field, same object. */
488 else if (refa->op2 >= IRFL_TAB_META && refa->op2 <= IRFL_TAB_NOMM)
489 return aa_table(J, refa->op1, refb->op1); /* Disambiguate tables. */
490 else
491 return ALIAS_MAY; /* Same field, possibly different object. */
494 /* Only the loads for mutable fields end up here (see FOLD). */
495 TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J)
497 IRRef oref = fins->op1; /* Object reference. */
498 IRRef fid = fins->op2; /* Field ID. */
499 IRRef lim = oref; /* Search limit. */
500 IRRef ref;
502 /* Search for conflicting stores. */
503 ref = J->chain[IR_FSTORE];
504 while (ref > oref) {
505 IRIns *store = IR(ref);
506 switch (aa_fref(J, fins, IR(store->op1))) {
507 case ALIAS_NO: break; /* Continue searching. */
508 case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */
509 case ALIAS_MUST: return store->op2; /* Store forwarding. */
511 ref = store->prev;
514 /* No conflicting store: const-fold field loads from allocations. */
515 if (fid == IRFL_TAB_META) {
516 IRIns *ir = IR(oref);
517 if (ir->o == IR_TNEW || ir->o == IR_TDUP)
518 return lj_ir_knull(J, IRT_TAB);
521 cselim:
522 /* Try to find a matching load. Below the conflicting store, if any. */
523 return lj_opt_cselim(J, lim);
526 /* FSTORE elimination. */
527 TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J)
529 IRRef fref = fins->op1; /* FREF reference. */
530 IRRef val = fins->op2; /* Stored value reference. */
531 IRIns *xr = IR(fref);
532 IRRef1 *refp = &J->chain[IR_FSTORE];
533 IRRef ref = *refp;
534 while (ref > fref) { /* Search for redundant or conflicting stores. */
535 IRIns *store = IR(ref);
536 switch (aa_fref(J, xr, IR(store->op1))) {
537 case ALIAS_NO:
538 break; /* Continue searching. */
539 case ALIAS_MAY:
540 if (store->op2 != val) /* Conflict if the value is different. */
541 goto doemit;
542 break; /* Otherwise continue searching. */
543 case ALIAS_MUST:
544 if (store->op2 == val) /* Same value: drop the new store. */
545 return DROPFOLD;
546 /* Different value: try to eliminate the redundant store. */
547 if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */
548 IRIns *ir;
549 /* Check for any intervening guards or conflicting loads. */
550 for (ir = IR(J->cur.nins-1); ir > store; ir--)
551 if (irt_isguard(ir->t) || (ir->o == IR_FLOAD && ir->op2 == xr->op2))
552 goto doemit; /* No elimination possible. */
553 /* Remove redundant store from chain and replace with NOP. */
554 *refp = store->prev;
555 store->o = IR_NOP;
556 store->t.irt = IRT_NIL;
557 store->op1 = store->op2 = 0;
558 store->prev = 0;
559 /* Now emit the new store instead. */
561 goto doemit;
563 ref = *(refp = &store->prev);
565 doemit:
566 return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
569 /* -- XLOAD forwarding and XSTORE elimination ----------------------------- */
571 /* Find cdata allocation for a reference (if any). */
572 static IRIns *aa_findcnew(jit_State *J, IRIns *ir)
574 while (ir->o == IR_ADD) {
575 if (!irref_isk(ir->op1)) {
576 IRIns *ir1 = aa_findcnew(J, IR(ir->op1)); /* Left-recursion. */
577 if (ir1) return ir1;
579 if (irref_isk(ir->op2)) return NULL;
580 ir = IR(ir->op2); /* Flatten right-recursion. */
582 return ir->o == IR_CNEW ? ir : NULL;
585 /* Alias analysis for two cdata allocations. */
586 static AliasRet aa_cnew(jit_State *J, IRIns *refa, IRIns *refb)
588 IRIns *cnewa = aa_findcnew(J, refa);
589 IRIns *cnewb = aa_findcnew(J, refb);
590 if (cnewa == cnewb)
591 return ALIAS_MAY; /* Same allocation or neither is an allocation. */
592 if (cnewa && cnewb)
593 return ALIAS_NO; /* Two different allocations never alias. */
594 if (cnewb) { cnewa = cnewb; refb = refa; }
595 return aa_escape(J, cnewa, refb);
598 /* Alias analysis for XLOAD/XSTORE. */
599 static AliasRet aa_xref(jit_State *J, IRIns *refa, IRIns *xa, IRIns *xb)
601 ptrdiff_t ofsa = 0, ofsb = 0;
602 IRIns *refb = IR(xb->op1);
603 IRIns *basea = refa, *baseb = refb;
604 if (refa == refb && irt_sametype(xa->t, xb->t))
605 return ALIAS_MUST; /* Shortcut for same refs with identical type. */
606 /* Offset-based disambiguation. */
607 if (refa->o == IR_ADD && irref_isk(refa->op2)) {
608 IRIns *irk = IR(refa->op2);
609 basea = IR(refa->op1);
610 ofsa = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :
611 (ptrdiff_t)irk->i;
612 if (basea == refb && ofsa != 0)
613 return ALIAS_NO; /* base+-ofs vs. base. */
615 if (refb->o == IR_ADD && irref_isk(refb->op2)) {
616 IRIns *irk = IR(refb->op2);
617 baseb = IR(refb->op1);
618 ofsb = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :
619 (ptrdiff_t)irk->i;
620 if (refa == baseb && ofsb != 0)
621 return ALIAS_NO; /* base vs. base+-ofs. */
623 /* This implements (very) strict aliasing rules.
624 ** Different types do NOT alias, except for differences in signedness.
625 ** Type punning through unions is allowed (but forces a reload).
627 if (basea == baseb) {
628 ptrdiff_t sza = irt_size(xa->t), szb = irt_size(xb->t);
629 if (ofsa == ofsb) {
630 if (sza == szb && irt_isfp(xa->t) == irt_isfp(xb->t))
631 return ALIAS_MUST; /* Same-sized, same-kind. May need to convert. */
632 } else if (ofsa + sza <= ofsb || ofsb + szb <= ofsa) {
633 return ALIAS_NO; /* Non-overlapping base+-o1 vs. base+-o2. */
635 /* NYI: extract, extend or reinterpret bits (int <-> fp). */
636 return ALIAS_MAY; /* Overlapping or type punning: force reload. */
638 if (!irt_sametype(xa->t, xb->t) &&
639 !(irt_typerange(xa->t, IRT_I8, IRT_U64) &&
640 ((xa->t.irt - IRT_I8) ^ (xb->t.irt - IRT_I8)) == 1))
641 return ALIAS_NO;
642 /* NYI: structural disambiguation. */
643 return aa_cnew(J, basea, baseb); /* Try to disambiguate allocations. */
646 /* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */
647 static IRRef reassoc_trycse(jit_State *J, IROp op, IRRef op1, IRRef op2)
649 IRRef ref = J->chain[op];
650 IRRef lim = op1;
651 if (op2 > lim) { lim = op2; op2 = op1; op1 = lim; }
652 while (ref > lim) {
653 IRIns *ir = IR(ref);
654 if (ir->op1 == op1 && ir->op2 == op2)
655 return ref;
656 ref = ir->prev;
658 return 0;
661 /* Reassociate index references. */
662 static IRRef reassoc_xref(jit_State *J, IRIns *ir)
664 ptrdiff_t ofs = 0;
665 if (ir->o == IR_ADD && irref_isk(ir->op2)) { /* Get constant offset. */
666 IRIns *irk = IR(ir->op2);
667 ofs = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 :
668 (ptrdiff_t)irk->i;
669 ir = IR(ir->op1);
671 if (ir->o == IR_ADD) { /* Add of base + index. */
672 /* Index ref > base ref for loop-carried dependences. Only check op1. */
673 IRIns *ir2, *ir1 = IR(ir->op1);
674 int32_t shift = 0;
675 IRRef idxref;
676 /* Determine index shifts. Don't bother with IR_MUL here. */
677 if (ir1->o == IR_BSHL && irref_isk(ir1->op2))
678 shift = IR(ir1->op2)->i;
679 else if (ir1->o == IR_ADD && ir1->op1 == ir1->op2)
680 shift = 1;
681 else
682 ir1 = ir;
683 ir2 = IR(ir1->op1);
684 /* A non-reassociated add. Must be a loop-carried dependence. */
685 if (ir2->o == IR_ADD && irt_isint(ir2->t) && irref_isk(ir2->op2))
686 ofs += (ptrdiff_t)IR(ir2->op2)->i << shift;
687 else
688 return 0;
689 idxref = ir2->op1;
690 /* Try to CSE the reassociated chain. Give up if not found. */
691 if (ir1 != ir &&
692 !(idxref = reassoc_trycse(J, ir1->o, idxref,
693 ir1->o == IR_BSHL ? ir1->op2 : idxref)))
694 return 0;
695 if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, ir->op2)))
696 return 0;
697 if (ofs != 0) {
698 IRRef refk = tref_ref(lj_ir_kintp(J, ofs));
699 if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, refk)))
700 return 0;
702 return idxref; /* Success, found a reassociated index reference. Phew. */
704 return 0; /* Failure. */
707 /* XLOAD forwarding. */
708 TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J)
710 IRRef xref = fins->op1;
711 IRIns *xr = IR(xref);
712 IRRef lim = xref; /* Search limit. */
713 IRRef ref;
715 if ((fins->op2 & IRXLOAD_READONLY))
716 goto cselim;
717 if ((fins->op2 & IRXLOAD_VOLATILE))
718 goto doemit;
720 /* Search for conflicting stores. */
721 ref = J->chain[IR_XSTORE];
722 retry:
723 if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS];
724 if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR];
725 while (ref > lim) {
726 IRIns *store = IR(ref);
727 switch (aa_xref(J, xr, fins, store)) {
728 case ALIAS_NO: break; /* Continue searching. */
729 case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */
730 case ALIAS_MUST:
731 /* Emit conversion if the loaded type doesn't match the forwarded type. */
732 if (!irt_sametype(fins->t, IR(store->op2)->t)) {
733 IRType st = irt_type(fins->t);
734 if (st == IRT_I8 || st == IRT_I16) { /* Trunc + sign-extend. */
735 st |= IRCONV_SEXT;
736 } else if (st == IRT_U8 || st == IRT_U16) { /* Trunc + zero-extend. */
737 } else if (st == IRT_INT) {
738 st = irt_type(IR(store->op2)->t); /* Needs dummy CONV.int.*. */
739 } else { /* I64/U64 are boxed, U32 is hidden behind a CONV.num.u32. */
740 goto store_fwd;
742 fins->ot = IRTI(IR_CONV);
743 fins->op1 = store->op2;
744 fins->op2 = (IRT_INT<<5)|st;
745 return RETRYFOLD;
747 store_fwd:
748 return store->op2; /* Store forwarding. */
750 ref = store->prev;
753 cselim:
754 /* Try to find a matching load. Below the conflicting store, if any. */
755 ref = J->chain[IR_XLOAD];
756 while (ref > lim) {
757 /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */
758 if (IR(ref)->op1 == xref && irt_sametype(IR(ref)->t, fins->t))
759 return ref;
760 ref = IR(ref)->prev;
763 /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */
764 if (!(fins->op2 & IRXLOAD_READONLY) && J->chain[IR_LOOP] &&
765 xref == fins->op1 && (xref = reassoc_xref(J, xr)) != 0) {
766 ref = J->chain[IR_XSTORE];
767 while (ref > lim) /* Skip stores that have already been checked. */
768 ref = IR(ref)->prev;
769 lim = xref;
770 xr = IR(xref);
771 goto retry; /* Retry with the reassociated reference. */
773 doemit:
774 return EMITFOLD;
777 /* XSTORE elimination. */
778 TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J)
780 IRRef xref = fins->op1;
781 IRIns *xr = IR(xref);
782 IRRef lim = xref; /* Search limit. */
783 IRRef val = fins->op2; /* Stored value reference. */
784 IRRef1 *refp = &J->chain[IR_XSTORE];
785 IRRef ref = *refp;
786 if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS];
787 if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR];
788 while (ref > lim) { /* Search for redundant or conflicting stores. */
789 IRIns *store = IR(ref);
790 switch (aa_xref(J, xr, fins, store)) {
791 case ALIAS_NO:
792 break; /* Continue searching. */
793 case ALIAS_MAY:
794 if (store->op2 != val) /* Conflict if the value is different. */
795 goto doemit;
796 break; /* Otherwise continue searching. */
797 case ALIAS_MUST:
798 if (store->op2 == val) /* Same value: drop the new store. */
799 return DROPFOLD;
800 /* Different value: try to eliminate the redundant store. */
801 if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */
802 IRIns *ir;
803 /* Check for any intervening guards or any XLOADs (no AA performed). */
804 for (ir = IR(J->cur.nins-1); ir > store; ir--)
805 if (irt_isguard(ir->t) || ir->o == IR_XLOAD)
806 goto doemit; /* No elimination possible. */
807 /* Remove redundant store from chain and replace with NOP. */
808 *refp = store->prev;
809 store->o = IR_NOP;
810 store->t.irt = IRT_NIL;
811 store->op1 = store->op2 = 0;
812 store->prev = 0;
813 /* Now emit the new store instead. */
815 goto doemit;
817 ref = *(refp = &store->prev);
819 doemit:
820 return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
823 /* -- Forwarding of lj_tab_len -------------------------------------------- */
825 /* This is rather simplistic right now, but better than nothing. */
826 TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J)
828 IRRef tab = fins->op1; /* Table reference. */
829 IRRef lim = tab; /* Search limit. */
830 IRRef ref;
832 /* Any ASTORE is a conflict and limits the search. */
833 if (J->chain[IR_ASTORE] > lim) lim = J->chain[IR_ASTORE];
835 /* Search for conflicting HSTORE with numeric key. */
836 ref = J->chain[IR_HSTORE];
837 while (ref > lim) {
838 IRIns *store = IR(ref);
839 IRIns *href = IR(store->op1);
840 IRIns *key = IR(href->op2);
841 if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) {
842 lim = ref; /* Conflicting store found, limits search for TLEN. */
843 break;
845 ref = store->prev;
848 /* Try to find a matching load. Below the conflicting store, if any. */
849 return lj_opt_cselim(J, lim);
852 /* -- ASTORE/HSTORE previous type analysis -------------------------------- */
854 /* Check whether the previous value for a table store is non-nil.
855 ** This can be derived either from a previous store or from a previous
856 ** load (because all loads from tables perform a type check).
858 ** The result of the analysis can be used to avoid the metatable check
859 ** and the guard against HREF returning niltv. Both of these are cheap,
860 ** so let's not spend too much effort on the analysis.
862 ** A result of 1 is exact: previous value CANNOT be nil.
863 ** A result of 0 is inexact: previous value MAY be nil.
865 int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref)
867 /* First check stores. */
868 IRRef ref = J->chain[loadop+IRDELTA_L2S];
869 while (ref > xref) {
870 IRIns *store = IR(ref);
871 if (store->op1 == xref) { /* Same xREF. */
872 /* A nil store MAY alias, but a non-nil store MUST alias. */
873 return !irt_isnil(store->t);
874 } else if (irt_isnil(store->t)) { /* Must check any nil store. */
875 IRRef skref = IR(store->op1)->op2;
876 IRRef xkref = IR(xref)->op2;
877 /* Same key type MAY alias. Need ALOAD check due to multiple int types. */
878 if (loadop == IR_ALOAD || irt_sametype(IR(skref)->t, IR(xkref)->t)) {
879 if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref))
880 return 0; /* A nil store with same const key or var key MAY alias. */
881 /* Different const keys CANNOT alias. */
882 } /* Different key types CANNOT alias. */
883 } /* Other non-nil stores MAY alias. */
884 ref = store->prev;
887 /* Check loads since nothing could be derived from stores. */
888 ref = J->chain[loadop];
889 while (ref > xref) {
890 IRIns *load = IR(ref);
891 if (load->op1 == xref) { /* Same xREF. */
892 /* A nil load MAY alias, but a non-nil load MUST alias. */
893 return !irt_isnil(load->t);
894 } /* Other non-nil loads MAY alias. */
895 ref = load->prev;
897 return 0; /* Nothing derived at all, previous value MAY be nil. */
900 /* ------------------------------------------------------------------------ */
902 #undef IR
903 #undef fins
904 #undef fleft
905 #undef fright
907 #endif