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-2023 Mike Pall. See Copyright Notice in luajit.h
20 #include "lj_ircall.h"
21 #include "lj_dispatch.h"
23 /* Some local macros to save typing. Undef'd at the end. */
24 #define IR(ref) (&J->cur.ir[(ref)])
25 #define fins (&J->fold.ins)
26 #define fleft (J->fold.left)
27 #define fright (J->fold.right)
30 ** Caveat #1: return value is not always a TRef -- only use with tref_ref().
31 ** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold().
34 /* Return values from alias analysis. */
36 ALIAS_NO
, /* The two refs CANNOT alias (exact). */
37 ALIAS_MAY
, /* The two refs MAY alias (inexact). */
38 ALIAS_MUST
/* The two refs MUST alias (exact). */
41 /* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */
43 /* Simplified escape analysis: check for intervening stores. */
44 static AliasRet
aa_escape(jit_State
*J
, IRIns
*ir
, IRIns
*stop
)
46 IRRef ref
= (IRRef
)(ir
- J
->cur
.ir
); /* The ref that might be stored. */
47 for (ir
++; ir
< stop
; ir
++)
49 (ir
->o
== IR_ASTORE
|| ir
->o
== IR_HSTORE
||
50 ir
->o
== IR_USTORE
|| ir
->o
== IR_FSTORE
))
51 return ALIAS_MAY
; /* Reference was stored and might alias. */
52 return ALIAS_NO
; /* Reference was not stored. */
55 /* Alias analysis for two different table references. */
56 static AliasRet
aa_table(jit_State
*J
, IRRef ta
, IRRef tb
)
58 IRIns
*taba
= IR(ta
), *tabb
= IR(tb
);
60 lj_assertJ(ta
!= tb
, "bad usage");
61 lj_assertJ(irt_istab(taba
->t
) && irt_istab(tabb
->t
), "bad usage");
62 /* Disambiguate new allocations. */
63 newa
= (taba
->o
== IR_TNEW
|| taba
->o
== IR_TDUP
);
64 newb
= (tabb
->o
== IR_TNEW
|| tabb
->o
== IR_TDUP
);
66 return ALIAS_NO
; /* Two different allocations never alias. */
67 if (newb
) { /* At least one allocation? */
68 IRIns
*tmp
= taba
; taba
= tabb
; tabb
= tmp
;
70 return ALIAS_MAY
; /* Anything else: we just don't know. */
72 return aa_escape(J
, taba
, tabb
);
75 /* Check whether there's no aliasing table.clear. */
76 static int fwd_aa_tab_clear(jit_State
*J
, IRRef lim
, IRRef ta
)
78 IRRef ref
= J
->chain
[IR_CALLS
];
80 IRIns
*calls
= IR(ref
);
81 if (calls
->op2
== IRCALL_lj_tab_clear
&&
82 (ta
== calls
->op1
|| aa_table(J
, ta
, calls
->op1
) != ALIAS_NO
))
83 return 0; /* Conflict. */
86 return 1; /* No conflict. Can safely FOLD/CSE. */
89 /* Check whether there's no aliasing NEWREF/table.clear for the left operand. */
90 int LJ_FASTCALL
lj_opt_fwd_tptr(jit_State
*J
, IRRef lim
)
93 IRRef ref
= J
->chain
[IR_NEWREF
];
95 IRIns
*newref
= IR(ref
);
96 if (ta
== newref
->op1
|| aa_table(J
, ta
, newref
->op1
) != ALIAS_NO
)
97 return 0; /* Conflict. */
100 return fwd_aa_tab_clear(J
, lim
, ta
);
103 /* Alias analysis for array and hash access using key-based disambiguation. */
104 static AliasRet
aa_ahref(jit_State
*J
, IRIns
*refa
, IRIns
*refb
)
106 IRRef ka
= refa
->op2
;
107 IRRef kb
= refb
->op2
;
111 return ALIAS_MUST
; /* Shortcut for same refs. */
113 if (keya
->o
== IR_KSLOT
) { ka
= keya
->op1
; keya
= IR(ka
); }
115 if (keyb
->o
== IR_KSLOT
) { kb
= keyb
->op1
; keyb
= IR(kb
); }
116 ta
= (refa
->o
==IR_HREFK
|| refa
->o
==IR_AREF
) ? IR(refa
->op1
)->op1
: refa
->op1
;
117 tb
= (refb
->o
==IR_HREFK
|| refb
->o
==IR_AREF
) ? IR(refb
->op1
)->op1
: refb
->op1
;
119 /* Same key. Check for same table with different ref (NEWREF vs. HREF). */
121 return ALIAS_MUST
; /* Same key, same table. */
123 return aa_table(J
, ta
, tb
); /* Same key, possibly different table. */
125 if (irref_isk(ka
) && irref_isk(kb
))
126 return ALIAS_NO
; /* Different constant keys. */
127 if (refa
->o
== IR_AREF
) {
128 /* Disambiguate array references based on index arithmetic. */
129 int32_t ofsa
= 0, ofsb
= 0;
130 IRRef basea
= ka
, baseb
= kb
;
131 lj_assertJ(refb
->o
== IR_AREF
, "expected AREF");
132 /* Gather base and offset from t[base] or t[base+-ofs]. */
133 if (keya
->o
== IR_ADD
&& irref_isk(keya
->op2
)) {
135 ofsa
= IR(keya
->op2
)->i
;
136 if (basea
== kb
&& ofsa
!= 0)
137 return ALIAS_NO
; /* t[base+-ofs] vs. t[base]. */
139 if (keyb
->o
== IR_ADD
&& irref_isk(keyb
->op2
)) {
141 ofsb
= IR(keyb
->op2
)->i
;
142 if (ka
== baseb
&& ofsb
!= 0)
143 return ALIAS_NO
; /* t[base] vs. t[base+-ofs]. */
145 if (basea
== baseb
&& ofsa
!= ofsb
)
146 return ALIAS_NO
; /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */
148 /* Disambiguate hash references based on the type of their keys. */
149 lj_assertJ((refa
->o
==IR_HREF
|| refa
->o
==IR_HREFK
|| refa
->o
==IR_NEWREF
) &&
150 (refb
->o
==IR_HREF
|| refb
->o
==IR_HREFK
|| refb
->o
==IR_NEWREF
),
151 "bad xREF IR op %d or %d", refa
->o
, refb
->o
);
152 if (!irt_sametype(keya
->t
, keyb
->t
))
153 return ALIAS_NO
; /* Different key types. */
156 return ALIAS_MAY
; /* Same table, cannot disambiguate keys. */
158 return aa_table(J
, ta
, tb
); /* Try to disambiguate tables. */
161 /* Array and hash load forwarding. */
162 static TRef
fwd_ahload(jit_State
*J
, IRRef xref
)
164 IRIns
*xr
= IR(xref
);
165 IRRef lim
= xref
; /* Search limit. */
168 /* Search for conflicting stores. */
169 ref
= J
->chain
[fins
->o
+IRDELTA_L2S
];
171 IRIns
*store
= IR(ref
);
172 switch (aa_ahref(J
, xr
, IR(store
->op1
))) {
173 case ALIAS_NO
: break; /* Continue searching. */
174 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
175 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
180 /* No conflicting store (yet): const-fold loads from allocations. */
182 IRIns
*ir
= (xr
->o
== IR_HREFK
|| xr
->o
== IR_AREF
) ? IR(xr
->op1
) : xr
;
185 if ((ir
->o
== IR_TNEW
|| (ir
->o
== IR_TDUP
&& irref_isk(xr
->op2
))) &&
186 fwd_aa_tab_clear(J
, tab
, tab
)) {
187 /* A NEWREF with a number key may end up pointing to the array part.
188 ** But it's referenced from HSTORE and not found in the ASTORE chain.
189 ** Or a NEWREF may rehash the table and move unrelated number keys.
190 ** For now simply consider this a conflict without forwarding anything.
192 if (xr
->o
== IR_AREF
) {
193 IRRef ref2
= J
->chain
[IR_NEWREF
];
195 IRIns
*newref
= IR(ref2
);
196 if (irt_isnum(IR(newref
->op2
)->t
))
201 IRIns
*key
= IR(xr
->op2
);
202 if (key
->o
== IR_KSLOT
) key
= IR(key
->op1
);
203 if (irt_isnum(key
->t
) && J
->chain
[IR_NEWREF
] > tab
)
206 /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF.
207 ** But the above search for conflicting stores was limited by xref.
208 ** So continue searching, limited by the TNEW/TDUP. Store forwarding
209 ** is ok, too. A conflict does NOT limit the search for a matching load.
212 IRIns
*store
= IR(ref
);
213 switch (aa_ahref(J
, xr
, IR(store
->op1
))) {
214 case ALIAS_NO
: break; /* Continue searching. */
215 case ALIAS_MAY
: goto cselim
; /* Conflicting store. */
216 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
220 if (ir
->o
== IR_TNEW
&& !irt_isnil(fins
->t
))
221 return 0; /* Type instability in loop-carried dependency. */
222 if (irt_ispri(fins
->t
)) {
223 return TREF_PRI(irt_type(fins
->t
));
224 } else if (irt_isnum(fins
->t
) || (LJ_DUALNUM
&& irt_isint(fins
->t
)) ||
225 irt_isstr(fins
->t
)) {
228 IRIns
*key
= IR(xr
->op2
);
229 if (key
->o
== IR_KSLOT
) key
= IR(key
->op1
);
230 lj_ir_kvalue(J
->L
, &keyv
, key
);
231 tv
= lj_tab_get(J
->L
, ir_ktab(IR(ir
->op1
)), &keyv
);
232 if (itype2irt(tv
) != irt_type(fins
->t
))
233 return 0; /* Type instability in loop-carried dependency. */
234 if (irt_isnum(fins
->t
))
235 return lj_ir_knum_u64(J
, tv
->u64
);
236 else if (LJ_DUALNUM
&& irt_isint(fins
->t
))
237 return lj_ir_kint(J
, intV(tv
));
239 return lj_ir_kstr(J
, strV(tv
));
241 /* Othwerwise: don't intern as a constant. */
246 /* Try to find a matching load. Below the conflicting store, if any. */
247 ref
= J
->chain
[fins
->o
];
249 IRIns
*load
= IR(ref
);
250 if (load
->op1
== xref
)
251 return ref
; /* Load forwarding. */
254 return 0; /* Conflict or no match. */
257 /* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */
258 static TRef
fwd_aload_reassoc(jit_State
*J
)
260 IRIns
*irx
= IR(fins
->op1
);
261 IRIns
*key
= IR(irx
->op2
);
262 if (key
->o
== IR_ADD
&& irref_isk(key
->op2
)) {
263 IRIns
*add2
= IR(key
->op1
);
264 if (add2
->o
== IR_ADD
&& irref_isk(add2
->op2
) &&
265 IR(key
->op2
)->i
== -IR(add2
->op2
)->i
) {
266 IRRef ref
= J
->chain
[IR_AREF
];
267 IRRef lim
= add2
->op1
;
268 if (irx
->op1
> lim
) lim
= irx
->op1
;
271 if (ir
->op1
== irx
->op1
&& ir
->op2
== add2
->op1
)
272 return fwd_ahload(J
, ref
);
280 /* ALOAD forwarding. */
281 TRef LJ_FASTCALL
lj_opt_fwd_aload(jit_State
*J
)
284 if ((ref
= fwd_ahload(J
, fins
->op1
)) ||
285 (ref
= fwd_aload_reassoc(J
)))
290 /* HLOAD forwarding. */
291 TRef LJ_FASTCALL
lj_opt_fwd_hload(jit_State
*J
)
293 IRRef ref
= fwd_ahload(J
, fins
->op1
);
299 /* HREFK forwarding. */
300 TRef LJ_FASTCALL
lj_opt_fwd_hrefk(jit_State
*J
)
302 IRRef tab
= fleft
->op1
;
303 IRRef ref
= J
->chain
[IR_NEWREF
];
305 IRIns
*newref
= IR(ref
);
306 if (tab
== newref
->op1
) {
307 if (fright
->op1
== newref
->op2
&& fwd_aa_tab_clear(J
, ref
, tab
))
308 return ref
; /* Forward from NEWREF. */
311 } else if (aa_table(J
, tab
, newref
->op1
) != ALIAS_NO
) {
316 /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */
317 if (IR(tab
)->o
== IR_TDUP
&& fwd_aa_tab_clear(J
, tab
, tab
))
318 fins
->t
.irt
&= ~IRT_GUARD
; /* Drop HREFK guard. */
323 /* Check whether HREF of TNEW/TDUP can be folded to niltv. */
324 int LJ_FASTCALL
lj_opt_fwd_href_nokey(jit_State
*J
)
326 IRRef lim
= fins
->op1
; /* Search limit. */
329 /* The key for an ASTORE may end up in the hash part after a NEWREF. */
330 if (irt_isnum(fright
->t
) && J
->chain
[IR_NEWREF
] > lim
) {
331 ref
= J
->chain
[IR_ASTORE
];
333 if (ref
< J
->chain
[IR_NEWREF
])
334 return 0; /* Conflict. */
339 /* Search for conflicting stores. */
340 ref
= J
->chain
[IR_HSTORE
];
342 IRIns
*store
= IR(ref
);
343 if (aa_ahref(J
, fins
, IR(store
->op1
)) != ALIAS_NO
)
344 return 0; /* Conflict. */
348 return 1; /* No conflict. Can fold to niltv. */
351 /* ASTORE/HSTORE elimination. */
352 TRef LJ_FASTCALL
lj_opt_dse_ahstore(jit_State
*J
)
354 IRRef xref
= fins
->op1
; /* xREF reference. */
355 IRRef val
= fins
->op2
; /* Stored value reference. */
356 IRIns
*xr
= IR(xref
);
357 IRRef1
*refp
= &J
->chain
[fins
->o
];
359 while (ref
> xref
) { /* Search for redundant or conflicting stores. */
360 IRIns
*store
= IR(ref
);
361 switch (aa_ahref(J
, xr
, IR(store
->op1
))) {
363 break; /* Continue searching. */
364 case ALIAS_MAY
: /* Store to MAYBE the same location. */
365 if (store
->op2
!= val
) /* Conflict if the value is different. */
367 break; /* Otherwise continue searching. */
368 case ALIAS_MUST
: /* Store to the same location. */
369 if (store
->op2
== val
) /* Same value: drop the new store. */
371 /* Different value: try to eliminate the redundant store. */
372 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
374 /* Check for any intervening guards (includes conflicting loads).
375 ** Note that lj_tab_keyindex and lj_vm_next don't need guards,
376 ** since they are followed by at least one guarded VLOAD.
378 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
379 if (irt_isguard(ir
->t
) || ir
->o
== IR_ALEN
)
380 goto doemit
; /* No elimination possible. */
381 /* Remove redundant store from chain and replace with NOP. */
384 /* Now emit the new store instead. */
388 ref
= *(refp
= &store
->prev
);
391 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
394 /* ALEN forwarding. */
395 TRef LJ_FASTCALL
lj_opt_fwd_alen(jit_State
*J
)
397 IRRef tab
= fins
->op1
; /* Table reference. */
398 IRRef lim
= tab
; /* Search limit. */
401 /* Search for conflicting HSTORE with numeric key. */
402 ref
= J
->chain
[IR_HSTORE
];
404 IRIns
*store
= IR(ref
);
405 IRIns
*href
= IR(store
->op1
);
406 IRIns
*key
= IR(href
->op2
);
407 if (irt_isnum(key
->o
== IR_KSLOT
? IR(key
->op1
)->t
: key
->t
)) {
408 lim
= ref
; /* Conflicting store found, limits search for ALEN. */
414 /* Try to find a matching ALEN. */
415 ref
= J
->chain
[IR_ALEN
];
417 /* CSE for ALEN only depends on the table, not the hint. */
418 if (IR(ref
)->op1
== tab
) {
421 /* Search for aliasing table.clear. */
422 if (!fwd_aa_tab_clear(J
, ref
, tab
))
425 /* Search for hint-forwarding or conflicting store. */
426 sref
= J
->chain
[IR_ASTORE
];
428 IRIns
*store
= IR(sref
);
429 IRIns
*aref
= IR(store
->op1
);
430 IRIns
*fref
= IR(aref
->op1
);
431 if (tab
== fref
->op1
) { /* ASTORE to the same table. */
432 /* Detect t[#t+1] = x idiom for push. */
433 IRIns
*idx
= IR(aref
->op2
);
434 if (!irt_isnil(store
->t
) &&
435 idx
->o
== IR_ADD
&& idx
->op1
== ref
&&
436 IR(idx
->op2
)->o
== IR_KINT
&& IR(idx
->op2
)->i
== 1) {
437 /* Note: this requires an extra PHI check in loop unroll. */
438 fins
->op2
= aref
->op2
; /* Set ALEN hint. */
440 goto doemit
; /* Conflicting store, possibly giving a hint. */
441 } else if (aa_table(J
, tab
, fref
->op1
) != ALIAS_NO
) {
442 goto doemit
; /* Conflicting store. */
447 return ref
; /* Plain ALEN forwarding. */
455 /* -- ULOAD forwarding ---------------------------------------------------- */
457 /* The current alias analysis for upvalues is very simplistic. It only
458 ** disambiguates between the unique upvalues of the same function.
459 ** This is good enough for now, since most upvalues are read-only.
461 ** A more precise analysis would be feasible with the help of the parser:
462 ** generate a unique key for every upvalue, even across all prototypes.
463 ** Lacking a realistic use-case, it's unclear whether this is beneficial.
465 static AliasRet
aa_uref(IRIns
*refa
, IRIns
*refb
)
467 if (refa
->o
!= refb
->o
)
468 return ALIAS_NO
; /* Different UREFx type. */
469 if (refa
->op1
== refb
->op1
) { /* Same function. */
470 if (refa
->op2
== refb
->op2
)
471 return ALIAS_MUST
; /* Same function, same upvalue idx. */
473 return ALIAS_NO
; /* Same function, different upvalue idx. */
474 } else { /* Different functions, check disambiguation hash values. */
475 if (((refa
->op2
^ refb
->op2
) & 0xff))
476 return ALIAS_NO
; /* Upvalues with different hash values cannot alias. */
478 return ALIAS_MAY
; /* No conclusion can be drawn for same hash value. */
482 /* ULOAD forwarding. */
483 TRef LJ_FASTCALL
lj_opt_fwd_uload(jit_State
*J
)
485 IRRef uref
= fins
->op1
;
486 IRRef lim
= REF_BASE
; /* Search limit. */
487 IRIns
*xr
= IR(uref
);
490 /* Search for conflicting stores. */
491 ref
= J
->chain
[IR_USTORE
];
493 IRIns
*store
= IR(ref
);
494 switch (aa_uref(xr
, IR(store
->op1
))) {
495 case ALIAS_NO
: break; /* Continue searching. */
496 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
497 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
503 /* Try to find a matching load. Below the conflicting store, if any. */
504 ref
= J
->chain
[IR_ULOAD
];
507 if (ir
->op1
== uref
||
508 (IR(ir
->op1
)->op12
== IR(uref
)->op12
&& IR(ir
->op1
)->o
== IR(uref
)->o
))
509 return ref
; /* Match for identical or equal UREFx (non-CSEable UREFO). */
512 return lj_ir_emit(J
);
515 /* USTORE elimination. */
516 TRef LJ_FASTCALL
lj_opt_dse_ustore(jit_State
*J
)
518 IRRef xref
= fins
->op1
; /* xREF reference. */
519 IRRef val
= fins
->op2
; /* Stored value reference. */
520 IRIns
*xr
= IR(xref
);
521 IRRef1
*refp
= &J
->chain
[IR_USTORE
];
523 while (ref
> xref
) { /* Search for redundant or conflicting stores. */
524 IRIns
*store
= IR(ref
);
525 switch (aa_uref(xr
, IR(store
->op1
))) {
527 break; /* Continue searching. */
528 case ALIAS_MAY
: /* Store to MAYBE the same location. */
529 if (store
->op2
!= val
) /* Conflict if the value is different. */
531 break; /* Otherwise continue searching. */
532 case ALIAS_MUST
: /* Store to the same location. */
533 if (store
->op2
== val
) /* Same value: drop the new store. */
535 /* Different value: try to eliminate the redundant store. */
536 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
538 /* Check for any intervening guards (includes conflicting loads). */
539 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
540 if (irt_isguard(ir
->t
))
541 goto doemit
; /* No elimination possible. */
542 /* Remove redundant store from chain and replace with NOP. */
545 if (ref
+1 < J
->cur
.nins
&&
546 store
[1].o
== IR_OBAR
&& store
[1].op1
== xref
) {
547 IRRef1
*bp
= &J
->chain
[IR_OBAR
];
549 for (obar
= IR(*bp
); *bp
> ref
+1; obar
= IR(*bp
))
551 /* Remove OBAR, too. */
555 /* Now emit the new store instead. */
559 ref
= *(refp
= &store
->prev
);
562 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
565 /* -- FLOAD forwarding and FSTORE elimination ----------------------------- */
567 /* Alias analysis for field access.
568 ** Field loads are cheap and field stores are rare.
569 ** Simple disambiguation based on field types is good enough.
571 static AliasRet
aa_fref(jit_State
*J
, IRIns
*refa
, IRIns
*refb
)
573 if (refa
->op2
!= refb
->op2
)
574 return ALIAS_NO
; /* Different fields. */
575 if (refa
->op1
== refb
->op1
)
576 return ALIAS_MUST
; /* Same field, same object. */
577 else if (refa
->op2
>= IRFL_TAB_META
&& refa
->op2
<= IRFL_TAB_NOMM
)
578 return aa_table(J
, refa
->op1
, refb
->op1
); /* Disambiguate tables. */
580 return ALIAS_MAY
; /* Same field, possibly different object. */
583 /* Only the loads for mutable fields end up here (see FOLD). */
584 TRef LJ_FASTCALL
lj_opt_fwd_fload(jit_State
*J
)
586 IRRef oref
= fins
->op1
; /* Object reference. */
587 IRRef fid
= fins
->op2
; /* Field ID. */
588 IRRef lim
= oref
; /* Search limit. */
591 /* Search for conflicting stores. */
592 ref
= J
->chain
[IR_FSTORE
];
594 IRIns
*store
= IR(ref
);
595 switch (aa_fref(J
, fins
, IR(store
->op1
))) {
596 case ALIAS_NO
: break; /* Continue searching. */
597 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
598 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
603 /* No conflicting store: const-fold field loads from allocations. */
604 if (fid
== IRFL_TAB_META
) {
605 IRIns
*ir
= IR(oref
);
606 if (ir
->o
== IR_TNEW
|| ir
->o
== IR_TDUP
)
607 return lj_ir_knull(J
, IRT_TAB
);
611 /* Try to find a matching load. Below the conflicting store, if any. */
612 return lj_opt_cselim(J
, lim
);
615 /* FSTORE elimination. */
616 TRef LJ_FASTCALL
lj_opt_dse_fstore(jit_State
*J
)
618 IRRef fref
= fins
->op1
; /* FREF reference. */
619 IRRef val
= fins
->op2
; /* Stored value reference. */
620 IRIns
*xr
= IR(fref
);
621 IRRef1
*refp
= &J
->chain
[IR_FSTORE
];
623 while (ref
> fref
) { /* Search for redundant or conflicting stores. */
624 IRIns
*store
= IR(ref
);
625 switch (aa_fref(J
, xr
, IR(store
->op1
))) {
627 break; /* Continue searching. */
629 if (store
->op2
!= val
) /* Conflict if the value is different. */
631 break; /* Otherwise continue searching. */
633 if (store
->op2
== val
&&
634 !(xr
->op2
>= IRFL_SBUF_W
&& xr
->op2
<= IRFL_SBUF_R
))
635 return DROPFOLD
; /* Same value: drop the new store. */
636 /* Different value: try to eliminate the redundant store. */
637 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
639 /* Check for any intervening guards or conflicting loads. */
640 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
641 if (irt_isguard(ir
->t
) || (ir
->o
== IR_FLOAD
&& ir
->op2
== xr
->op2
))
642 goto doemit
; /* No elimination possible. */
643 /* Remove redundant store from chain and replace with NOP. */
646 /* Now emit the new store instead. */
650 ref
= *(refp
= &store
->prev
);
653 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
656 /* Check whether there's no aliasing buffer op between IRFL_SBUF_*. */
657 int LJ_FASTCALL
lj_opt_fwd_sbuf(jit_State
*J
, IRRef lim
)
660 if (J
->chain
[IR_BUFPUT
] > lim
)
661 return 0; /* Conflict. */
662 ref
= J
->chain
[IR_CALLS
];
665 if (ir
->op2
>= IRCALL_lj_strfmt_putint
&& ir
->op2
< IRCALL_lj_buf_tostr
)
666 return 0; /* Conflict. */
669 ref
= J
->chain
[IR_CALLL
];
672 if (ir
->op2
>= IRCALL_lj_strfmt_putint
&& ir
->op2
< IRCALL_lj_buf_tostr
)
673 return 0; /* Conflict. */
676 return 1; /* No conflict. Can safely FOLD/CSE. */
679 /* -- XLOAD forwarding and XSTORE elimination ----------------------------- */
681 /* Find cdata allocation for a reference (if any). */
682 static IRIns
*aa_findcnew(jit_State
*J
, IRIns
*ir
)
684 while (ir
->o
== IR_ADD
) {
685 if (!irref_isk(ir
->op1
)) {
686 IRIns
*ir1
= aa_findcnew(J
, IR(ir
->op1
)); /* Left-recursion. */
689 if (irref_isk(ir
->op2
)) return NULL
;
690 ir
= IR(ir
->op2
); /* Flatten right-recursion. */
692 return ir
->o
== IR_CNEW
? ir
: NULL
;
695 /* Alias analysis for two cdata allocations. */
696 static AliasRet
aa_cnew(jit_State
*J
, IRIns
*refa
, IRIns
*refb
)
698 IRIns
*cnewa
= aa_findcnew(J
, refa
);
699 IRIns
*cnewb
= aa_findcnew(J
, refb
);
701 return ALIAS_MAY
; /* Same allocation or neither is an allocation. */
703 return ALIAS_NO
; /* Two different allocations never alias. */
704 if (cnewb
) { cnewa
= cnewb
; refb
= refa
; }
705 return aa_escape(J
, cnewa
, refb
);
708 /* Alias analysis for XLOAD/XSTORE. */
709 static AliasRet
aa_xref(jit_State
*J
, IRIns
*refa
, IRIns
*xa
, IRIns
*xb
)
711 ptrdiff_t ofsa
= 0, ofsb
= 0;
712 IRIns
*refb
= IR(xb
->op1
);
713 IRIns
*basea
= refa
, *baseb
= refb
;
714 if (refa
== refb
&& irt_sametype(xa
->t
, xb
->t
))
715 return ALIAS_MUST
; /* Shortcut for same refs with identical type. */
716 /* Offset-based disambiguation. */
717 if (refa
->o
== IR_ADD
&& irref_isk(refa
->op2
)) {
718 IRIns
*irk
= IR(refa
->op2
);
719 basea
= IR(refa
->op1
);
720 ofsa
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
723 if (refb
->o
== IR_ADD
&& irref_isk(refb
->op2
)) {
724 IRIns
*irk
= IR(refb
->op2
);
725 baseb
= IR(refb
->op1
);
726 ofsb
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
729 /* Treat constified pointers like base vs. base+offset. */
730 if (basea
->o
== IR_KPTR
&& baseb
->o
== IR_KPTR
) {
731 ofsb
+= (char *)ir_kptr(baseb
) - (char *)ir_kptr(basea
);
734 /* This implements (very) strict aliasing rules.
735 ** Different types do NOT alias, except for differences in signedness.
736 ** Type punning through unions is allowed (but forces a reload).
738 if (basea
== baseb
) {
739 ptrdiff_t sza
= irt_size(xa
->t
), szb
= irt_size(xb
->t
);
741 if (sza
== szb
&& irt_isfp(xa
->t
) == irt_isfp(xb
->t
))
742 return ALIAS_MUST
; /* Same-sized, same-kind. May need to convert. */
743 } else if (ofsa
+ sza
<= ofsb
|| ofsb
+ szb
<= ofsa
) {
744 return ALIAS_NO
; /* Non-overlapping base+-o1 vs. base+-o2. */
746 /* NYI: extract, extend or reinterpret bits (int <-> fp). */
747 return ALIAS_MAY
; /* Overlapping or type punning: force reload. */
749 if (!irt_sametype(xa
->t
, xb
->t
) &&
750 !(irt_typerange(xa
->t
, IRT_I8
, IRT_U64
) &&
751 ((xa
->t
.irt
- IRT_I8
) ^ (xb
->t
.irt
- IRT_I8
)) == 1))
753 /* NYI: structural disambiguation. */
754 return aa_cnew(J
, basea
, baseb
); /* Try to disambiguate allocations. */
757 /* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */
758 static IRRef
reassoc_trycse(jit_State
*J
, IROp op
, IRRef op1
, IRRef op2
)
760 IRRef ref
= J
->chain
[op
];
762 if (op2
> lim
) { lim
= op2
; op2
= op1
; op1
= lim
; }
765 if (ir
->op1
== op1
&& ir
->op2
== op2
)
772 /* Reassociate index references. */
773 static IRRef
reassoc_xref(jit_State
*J
, IRIns
*ir
)
776 if (ir
->o
== IR_ADD
&& irref_isk(ir
->op2
)) { /* Get constant offset. */
777 IRIns
*irk
= IR(ir
->op2
);
778 ofs
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
782 if (ir
->o
== IR_ADD
) { /* Add of base + index. */
783 /* Index ref > base ref for loop-carried dependences. Only check op1. */
784 IRIns
*ir2
, *ir1
= IR(ir
->op1
);
787 /* Determine index shifts. Don't bother with IR_MUL here. */
788 if (ir1
->o
== IR_BSHL
&& irref_isk(ir1
->op2
))
789 shift
= IR(ir1
->op2
)->i
;
790 else if (ir1
->o
== IR_ADD
&& ir1
->op1
== ir1
->op2
)
795 /* A non-reassociated add. Must be a loop-carried dependence. */
796 if (ir2
->o
== IR_ADD
&& irt_isint(ir2
->t
) && irref_isk(ir2
->op2
))
797 ofs
+= (ptrdiff_t)IR(ir2
->op2
)->i
<< shift
;
801 /* Try to CSE the reassociated chain. Give up if not found. */
803 !(idxref
= reassoc_trycse(J
, ir1
->o
, idxref
,
804 ir1
->o
== IR_BSHL
? ir1
->op2
: idxref
)))
806 if (!(idxref
= reassoc_trycse(J
, IR_ADD
, idxref
, ir
->op2
)))
809 IRRef refk
= tref_ref(lj_ir_kintp(J
, ofs
));
810 if (!(idxref
= reassoc_trycse(J
, IR_ADD
, idxref
, refk
)))
813 return idxref
; /* Success, found a reassociated index reference. Phew. */
815 return 0; /* Failure. */
818 /* XLOAD forwarding. */
819 TRef LJ_FASTCALL
lj_opt_fwd_xload(jit_State
*J
)
821 IRRef xref
= fins
->op1
;
822 IRIns
*xr
= IR(xref
);
823 IRRef lim
= xref
; /* Search limit. */
826 if ((fins
->op2
& IRXLOAD_READONLY
))
828 if ((fins
->op2
& IRXLOAD_VOLATILE
))
831 /* Search for conflicting stores. */
832 ref
= J
->chain
[IR_XSTORE
];
834 if (J
->chain
[IR_CALLXS
] > lim
) lim
= J
->chain
[IR_CALLXS
];
835 if (J
->chain
[IR_XBAR
] > lim
) lim
= J
->chain
[IR_XBAR
];
837 IRIns
*store
= IR(ref
);
838 switch (aa_xref(J
, xr
, fins
, store
)) {
839 case ALIAS_NO
: break; /* Continue searching. */
840 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
842 /* Emit conversion if the loaded type doesn't match the forwarded type. */
843 if (!irt_sametype(fins
->t
, IR(store
->op2
)->t
)) {
844 IRType dt
= irt_type(fins
->t
), st
= irt_type(IR(store
->op2
)->t
);
845 if (dt
== IRT_I8
|| dt
== IRT_I16
) { /* Trunc + sign-extend. */
846 st
= dt
| IRCONV_SEXT
;
848 } else if (dt
== IRT_U8
|| dt
== IRT_U16
) { /* Trunc + zero-extend. */
852 fins
->ot
= IRT(IR_CONV
, dt
);
853 fins
->op1
= store
->op2
;
854 fins
->op2
= (dt
<<5)|st
;
857 return store
->op2
; /* Store forwarding. */
863 /* Try to find a matching load. Below the conflicting store, if any. */
864 ref
= J
->chain
[IR_XLOAD
];
866 /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */
867 if (IR(ref
)->op1
== xref
&& irt_sametype(IR(ref
)->t
, fins
->t
))
872 /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */
873 if (!(fins
->op2
& IRXLOAD_READONLY
) && J
->chain
[IR_LOOP
] &&
874 xref
== fins
->op1
&& (xref
= reassoc_xref(J
, xr
)) != 0) {
875 ref
= J
->chain
[IR_XSTORE
];
876 while (ref
> lim
) /* Skip stores that have already been checked. */
880 goto retry
; /* Retry with the reassociated reference. */
886 /* XSTORE elimination. */
887 TRef LJ_FASTCALL
lj_opt_dse_xstore(jit_State
*J
)
889 IRRef xref
= fins
->op1
;
890 IRIns
*xr
= IR(xref
);
891 IRRef lim
= xref
; /* Search limit. */
892 IRRef val
= fins
->op2
; /* Stored value reference. */
893 IRRef1
*refp
= &J
->chain
[IR_XSTORE
];
895 if (J
->chain
[IR_CALLXS
] > lim
) lim
= J
->chain
[IR_CALLXS
];
896 if (J
->chain
[IR_XBAR
] > lim
) lim
= J
->chain
[IR_XBAR
];
897 if (J
->chain
[IR_XSNEW
] > lim
) lim
= J
->chain
[IR_XSNEW
];
898 while (ref
> lim
) { /* Search for redundant or conflicting stores. */
899 IRIns
*store
= IR(ref
);
900 switch (aa_xref(J
, xr
, fins
, store
)) {
902 break; /* Continue searching. */
904 if (store
->op2
!= val
) /* Conflict if the value is different. */
906 break; /* Otherwise continue searching. */
908 if (store
->op2
== val
) /* Same value: drop the new store. */
910 /* Different value: try to eliminate the redundant store. */
911 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
913 /* Check for any intervening guards or any XLOADs (no AA performed). */
914 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
915 if (irt_isguard(ir
->t
) || ir
->o
== IR_XLOAD
)
916 goto doemit
; /* No elimination possible. */
917 /* Remove redundant store from chain and replace with NOP. */
920 /* Now emit the new store instead. */
924 ref
= *(refp
= &store
->prev
);
927 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
930 /* -- ASTORE/HSTORE previous type analysis -------------------------------- */
932 /* Check whether the previous value for a table store is non-nil.
933 ** This can be derived either from a previous store or from a previous
934 ** load (because all loads from tables perform a type check).
936 ** The result of the analysis can be used to avoid the metatable check
937 ** and the guard against HREF returning niltv. Both of these are cheap,
938 ** so let's not spend too much effort on the analysis.
940 ** A result of 1 is exact: previous value CANNOT be nil.
941 ** A result of 0 is inexact: previous value MAY be nil.
943 int lj_opt_fwd_wasnonnil(jit_State
*J
, IROpT loadop
, IRRef xref
)
945 /* First check stores. */
946 IRRef ref
= J
->chain
[loadop
+IRDELTA_L2S
];
948 IRIns
*store
= IR(ref
);
949 if (store
->op1
== xref
) { /* Same xREF. */
950 /* A nil store MAY alias, but a non-nil store MUST alias. */
951 return !irt_isnil(store
->t
);
952 } else if (irt_isnil(store
->t
)) { /* Must check any nil store. */
953 IRRef skref
= IR(store
->op1
)->op2
;
954 IRRef xkref
= IR(xref
)->op2
;
955 /* Same key type MAY alias. Need ALOAD check due to multiple int types. */
956 if (loadop
== IR_ALOAD
|| irt_sametype(IR(skref
)->t
, IR(xkref
)->t
)) {
957 if (skref
== xkref
|| !irref_isk(skref
) || !irref_isk(xkref
))
958 return 0; /* A nil store with same const key or var key MAY alias. */
959 /* Different const keys CANNOT alias. */
960 } /* Different key types CANNOT alias. */
961 } /* Other non-nil stores MAY alias. */
965 /* Check loads since nothing could be derived from stores. */
966 ref
= J
->chain
[loadop
];
968 IRIns
*load
= IR(ref
);
969 if (load
->op1
== xref
) { /* Same xREF. */
970 /* A nil load MAY alias, but a non-nil load MUST alias. */
971 return !irt_isnil(load
->t
);
972 } /* Other non-nil loads MAY alias. */
975 return 0; /* Nothing derived at all, previous value MAY be nil. */
978 /* ------------------------------------------------------------------------ */