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-2022 Mike Pall. See Copyright Notice in luajit.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. */
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). */
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
++)
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
);
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
);
64 return ALIAS_NO
; /* Two different allocations never alias. */
65 if (newb
) { /* At least one allocation? */
66 IRIns
*tmp
= taba
; taba
= tabb
; tabb
= tmp
;
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
)
81 return ALIAS_MUST
; /* Shortcut for same refs. */
83 if (keya
->o
== IR_KSLOT
) { ka
= keya
->op1
; keya
= IR(ka
); }
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
;
89 /* Same key. Check for same table with different ref (NEWREF vs. HREF). */
91 return ALIAS_MUST
; /* Same key, same table. */
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
)) {
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
)) {
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. */
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. */
125 return ALIAS_MAY
; /* Same table, cannot disambiguate keys. */
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. */
137 /* Search for conflicting stores. */
138 ref
= J
->chain
[fins
->o
+IRDELTA_L2S
];
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. */
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
;
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
];
162 IRIns
*newref
= IR(ref2
);
163 if (irt_isnum(IR(newref
->op2
)->t
))
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.
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. */
182 if (ir
->o
== IR_TNEW
&& !irt_isnil(fins
->t
))
183 return 0; /* Type instability in loop-carried dependency. */
184 if (irt_ispri(fins
->t
)) {
185 return TREF_PRI(irt_type(fins
->t
));
186 } else if (irt_isnum(fins
->t
) || (LJ_DUALNUM
&& irt_isint(fins
->t
)) ||
187 irt_isstr(fins
->t
)) {
190 IRIns
*key
= IR(xr
->op2
);
191 if (key
->o
== IR_KSLOT
) key
= IR(key
->op1
);
192 lj_ir_kvalue(J
->L
, &keyv
, key
);
193 tv
= lj_tab_get(J
->L
, ir_ktab(IR(ir
->op1
)), &keyv
);
194 lua_assert(itype2irt(tv
) == irt_type(fins
->t
));
195 if (irt_isnum(fins
->t
))
196 return lj_ir_knum_u64(J
, tv
->u64
);
197 else if (LJ_DUALNUM
&& irt_isint(fins
->t
))
198 return lj_ir_kint(J
, intV(tv
));
200 return lj_ir_kstr(J
, strV(tv
));
202 /* Othwerwise: don't intern as a constant. */
207 /* Try to find a matching load. Below the conflicting store, if any. */
208 ref
= J
->chain
[fins
->o
];
210 IRIns
*load
= IR(ref
);
211 if (load
->op1
== xref
)
212 return ref
; /* Load forwarding. */
215 return 0; /* Conflict or no match. */
218 /* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */
219 static TRef
fwd_aload_reassoc(jit_State
*J
)
221 IRIns
*irx
= IR(fins
->op1
);
222 IRIns
*key
= IR(irx
->op2
);
223 if (key
->o
== IR_ADD
&& irref_isk(key
->op2
)) {
224 IRIns
*add2
= IR(key
->op1
);
225 if (add2
->o
== IR_ADD
&& irref_isk(add2
->op2
) &&
226 IR(key
->op2
)->i
== -IR(add2
->op2
)->i
) {
227 IRRef ref
= J
->chain
[IR_AREF
];
228 IRRef lim
= add2
->op1
;
229 if (irx
->op1
> lim
) lim
= irx
->op1
;
232 if (ir
->op1
== irx
->op1
&& ir
->op2
== add2
->op1
)
233 return fwd_ahload(J
, ref
);
241 /* ALOAD forwarding. */
242 TRef LJ_FASTCALL
lj_opt_fwd_aload(jit_State
*J
)
245 if ((ref
= fwd_ahload(J
, fins
->op1
)) ||
246 (ref
= fwd_aload_reassoc(J
)))
251 /* HLOAD forwarding. */
252 TRef LJ_FASTCALL
lj_opt_fwd_hload(jit_State
*J
)
254 IRRef ref
= fwd_ahload(J
, fins
->op1
);
260 /* HREFK forwarding. */
261 TRef LJ_FASTCALL
lj_opt_fwd_hrefk(jit_State
*J
)
263 IRRef tab
= fleft
->op1
;
264 IRRef ref
= J
->chain
[IR_NEWREF
];
266 IRIns
*newref
= IR(ref
);
267 if (tab
== newref
->op1
) {
268 if (fright
->op1
== newref
->op2
)
269 return ref
; /* Forward from NEWREF. */
272 } else if (aa_table(J
, tab
, newref
->op1
) != ALIAS_NO
) {
277 /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */
278 if (IR(tab
)->o
== IR_TDUP
)
279 fins
->t
.irt
&= ~IRT_GUARD
; /* Drop HREFK guard. */
284 /* Check whether HREF of TNEW/TDUP can be folded to niltv. */
285 int LJ_FASTCALL
lj_opt_fwd_href_nokey(jit_State
*J
)
287 IRRef lim
= fins
->op1
; /* Search limit. */
290 /* The key for an ASTORE may end up in the hash part after a NEWREF. */
291 if (irt_isnum(fright
->t
) && J
->chain
[IR_NEWREF
] > lim
) {
292 ref
= J
->chain
[IR_ASTORE
];
294 if (ref
< J
->chain
[IR_NEWREF
])
295 return 0; /* Conflict. */
300 /* Search for conflicting stores. */
301 ref
= J
->chain
[IR_HSTORE
];
303 IRIns
*store
= IR(ref
);
304 if (aa_ahref(J
, fins
, IR(store
->op1
)) != ALIAS_NO
)
305 return 0; /* Conflict. */
309 return 1; /* No conflict. Can fold to niltv. */
312 /* Check whether there's no aliasing NEWREF for the left operand. */
313 int LJ_FASTCALL
lj_opt_fwd_tptr(jit_State
*J
, IRRef lim
)
315 IRRef ta
= fins
->op1
;
316 IRRef ref
= J
->chain
[IR_NEWREF
];
318 IRIns
*newref
= IR(ref
);
319 if (ta
== newref
->op1
|| aa_table(J
, ta
, newref
->op1
) != ALIAS_NO
)
320 return 0; /* Conflict. */
323 return 1; /* No conflict. Can safely FOLD/CSE. */
326 /* ASTORE/HSTORE elimination. */
327 TRef LJ_FASTCALL
lj_opt_dse_ahstore(jit_State
*J
)
329 IRRef xref
= fins
->op1
; /* xREF reference. */
330 IRRef val
= fins
->op2
; /* Stored value reference. */
331 IRIns
*xr
= IR(xref
);
332 IRRef1
*refp
= &J
->chain
[fins
->o
];
334 while (ref
> xref
) { /* Search for redundant or conflicting stores. */
335 IRIns
*store
= IR(ref
);
336 switch (aa_ahref(J
, xr
, IR(store
->op1
))) {
338 break; /* Continue searching. */
339 case ALIAS_MAY
: /* Store to MAYBE the same location. */
340 if (store
->op2
!= val
) /* Conflict if the value is different. */
342 break; /* Otherwise continue searching. */
343 case ALIAS_MUST
: /* Store to the same location. */
344 if (store
->op2
== val
) /* Same value: drop the new store. */
346 /* Different value: try to eliminate the redundant store. */
347 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
349 /* Check for any intervening guards (includes conflicting loads). */
350 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
351 if (irt_isguard(ir
->t
) || ir
->o
== IR_CALLL
)
352 goto doemit
; /* No elimination possible. */
353 /* Remove redundant store from chain and replace with NOP. */
356 /* Now emit the new store instead. */
360 ref
= *(refp
= &store
->prev
);
363 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
366 /* -- ULOAD forwarding ---------------------------------------------------- */
368 /* The current alias analysis for upvalues is very simplistic. It only
369 ** disambiguates between the unique upvalues of the same function.
370 ** This is good enough for now, since most upvalues are read-only.
372 ** A more precise analysis would be feasible with the help of the parser:
373 ** generate a unique key for every upvalue, even across all prototypes.
374 ** Lacking a realistic use-case, it's unclear whether this is beneficial.
376 static AliasRet
aa_uref(IRIns
*refa
, IRIns
*refb
)
378 if (refa
->o
!= refb
->o
)
379 return ALIAS_NO
; /* Different UREFx type. */
380 if (refa
->op1
== refb
->op1
) { /* Same function. */
381 if (refa
->op2
== refb
->op2
)
382 return ALIAS_MUST
; /* Same function, same upvalue idx. */
384 return ALIAS_NO
; /* Same function, different upvalue idx. */
385 } else { /* Different functions, check disambiguation hash values. */
386 if (((refa
->op2
^ refb
->op2
) & 0xff))
387 return ALIAS_NO
; /* Upvalues with different hash values cannot alias. */
389 return ALIAS_MAY
; /* No conclusion can be drawn for same hash value. */
393 /* ULOAD forwarding. */
394 TRef LJ_FASTCALL
lj_opt_fwd_uload(jit_State
*J
)
396 IRRef uref
= fins
->op1
;
397 IRRef lim
= REF_BASE
; /* Search limit. */
398 IRIns
*xr
= IR(uref
);
401 /* Search for conflicting stores. */
402 ref
= J
->chain
[IR_USTORE
];
404 IRIns
*store
= IR(ref
);
405 switch (aa_uref(xr
, IR(store
->op1
))) {
406 case ALIAS_NO
: break; /* Continue searching. */
407 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
408 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
414 /* Try to find a matching load. Below the conflicting store, if any. */
416 ref
= J
->chain
[IR_ULOAD
];
419 if (ir
->op1
== uref
||
420 (IR(ir
->op1
)->op12
== IR(uref
)->op12
&& IR(ir
->op1
)->o
== IR(uref
)->o
))
421 return ref
; /* Match for identical or equal UREFx (non-CSEable UREFO). */
424 return lj_ir_emit(J
);
427 /* USTORE elimination. */
428 TRef LJ_FASTCALL
lj_opt_dse_ustore(jit_State
*J
)
430 IRRef xref
= fins
->op1
; /* xREF reference. */
431 IRRef val
= fins
->op2
; /* Stored value reference. */
432 IRIns
*xr
= IR(xref
);
433 IRRef1
*refp
= &J
->chain
[IR_USTORE
];
435 while (ref
> xref
) { /* Search for redundant or conflicting stores. */
436 IRIns
*store
= IR(ref
);
437 switch (aa_uref(xr
, IR(store
->op1
))) {
439 break; /* Continue searching. */
440 case ALIAS_MAY
: /* Store to MAYBE the same location. */
441 if (store
->op2
!= val
) /* Conflict if the value is different. */
443 break; /* Otherwise continue searching. */
444 case ALIAS_MUST
: /* Store to the same location. */
445 if (store
->op2
== val
) /* Same value: drop the new store. */
447 /* Different value: try to eliminate the redundant store. */
448 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
450 /* Check for any intervening guards (includes conflicting loads). */
451 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
452 if (irt_isguard(ir
->t
))
453 goto doemit
; /* No elimination possible. */
454 /* Remove redundant store from chain and replace with NOP. */
457 if (ref
+1 < J
->cur
.nins
&&
458 store
[1].o
== IR_OBAR
&& store
[1].op1
== xref
) {
459 IRRef1
*bp
= &J
->chain
[IR_OBAR
];
461 for (obar
= IR(*bp
); *bp
> ref
+1; obar
= IR(*bp
))
463 /* Remove OBAR, too. */
467 /* Now emit the new store instead. */
471 ref
= *(refp
= &store
->prev
);
474 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
477 /* -- FLOAD forwarding and FSTORE elimination ----------------------------- */
479 /* Alias analysis for field access.
480 ** Field loads are cheap and field stores are rare.
481 ** Simple disambiguation based on field types is good enough.
483 static AliasRet
aa_fref(jit_State
*J
, IRIns
*refa
, IRIns
*refb
)
485 if (refa
->op2
!= refb
->op2
)
486 return ALIAS_NO
; /* Different fields. */
487 if (refa
->op1
== refb
->op1
)
488 return ALIAS_MUST
; /* Same field, same object. */
489 else if (refa
->op2
>= IRFL_TAB_META
&& refa
->op2
<= IRFL_TAB_NOMM
)
490 return aa_table(J
, refa
->op1
, refb
->op1
); /* Disambiguate tables. */
492 return ALIAS_MAY
; /* Same field, possibly different object. */
495 /* Only the loads for mutable fields end up here (see FOLD). */
496 TRef LJ_FASTCALL
lj_opt_fwd_fload(jit_State
*J
)
498 IRRef oref
= fins
->op1
; /* Object reference. */
499 IRRef fid
= fins
->op2
; /* Field ID. */
500 IRRef lim
= oref
; /* Search limit. */
503 /* Search for conflicting stores. */
504 ref
= J
->chain
[IR_FSTORE
];
506 IRIns
*store
= IR(ref
);
507 switch (aa_fref(J
, fins
, IR(store
->op1
))) {
508 case ALIAS_NO
: break; /* Continue searching. */
509 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
510 case ALIAS_MUST
: return store
->op2
; /* Store forwarding. */
515 /* No conflicting store: const-fold field loads from allocations. */
516 if (fid
== IRFL_TAB_META
) {
517 IRIns
*ir
= IR(oref
);
518 if (ir
->o
== IR_TNEW
|| ir
->o
== IR_TDUP
)
519 return lj_ir_knull(J
, IRT_TAB
);
523 /* Try to find a matching load. Below the conflicting store, if any. */
524 return lj_opt_cselim(J
, lim
);
527 /* FSTORE elimination. */
528 TRef LJ_FASTCALL
lj_opt_dse_fstore(jit_State
*J
)
530 IRRef fref
= fins
->op1
; /* FREF reference. */
531 IRRef val
= fins
->op2
; /* Stored value reference. */
532 IRIns
*xr
= IR(fref
);
533 IRRef1
*refp
= &J
->chain
[IR_FSTORE
];
535 while (ref
> fref
) { /* Search for redundant or conflicting stores. */
536 IRIns
*store
= IR(ref
);
537 switch (aa_fref(J
, xr
, IR(store
->op1
))) {
539 break; /* Continue searching. */
541 if (store
->op2
!= val
) /* Conflict if the value is different. */
543 break; /* Otherwise continue searching. */
545 if (store
->op2
== val
) /* Same value: drop the new store. */
547 /* Different value: try to eliminate the redundant store. */
548 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
550 /* Check for any intervening guards or conflicting loads. */
551 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
552 if (irt_isguard(ir
->t
) || (ir
->o
== IR_FLOAD
&& ir
->op2
== xr
->op2
))
553 goto doemit
; /* No elimination possible. */
554 /* Remove redundant store from chain and replace with NOP. */
557 /* Now emit the new store instead. */
561 ref
= *(refp
= &store
->prev
);
564 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
567 /* -- XLOAD forwarding and XSTORE elimination ----------------------------- */
569 /* Find cdata allocation for a reference (if any). */
570 static IRIns
*aa_findcnew(jit_State
*J
, IRIns
*ir
)
572 while (ir
->o
== IR_ADD
) {
573 if (!irref_isk(ir
->op1
)) {
574 IRIns
*ir1
= aa_findcnew(J
, IR(ir
->op1
)); /* Left-recursion. */
577 if (irref_isk(ir
->op2
)) return NULL
;
578 ir
= IR(ir
->op2
); /* Flatten right-recursion. */
580 return ir
->o
== IR_CNEW
? ir
: NULL
;
583 /* Alias analysis for two cdata allocations. */
584 static AliasRet
aa_cnew(jit_State
*J
, IRIns
*refa
, IRIns
*refb
)
586 IRIns
*cnewa
= aa_findcnew(J
, refa
);
587 IRIns
*cnewb
= aa_findcnew(J
, refb
);
589 return ALIAS_MAY
; /* Same allocation or neither is an allocation. */
591 return ALIAS_NO
; /* Two different allocations never alias. */
592 if (cnewb
) { cnewa
= cnewb
; refb
= refa
; }
593 return aa_escape(J
, cnewa
, refb
);
596 /* Alias analysis for XLOAD/XSTORE. */
597 static AliasRet
aa_xref(jit_State
*J
, IRIns
*refa
, IRIns
*xa
, IRIns
*xb
)
599 ptrdiff_t ofsa
= 0, ofsb
= 0;
600 IRIns
*refb
= IR(xb
->op1
);
601 IRIns
*basea
= refa
, *baseb
= refb
;
602 if (refa
== refb
&& irt_sametype(xa
->t
, xb
->t
))
603 return ALIAS_MUST
; /* Shortcut for same refs with identical type. */
604 /* Offset-based disambiguation. */
605 if (refa
->o
== IR_ADD
&& irref_isk(refa
->op2
)) {
606 IRIns
*irk
= IR(refa
->op2
);
607 basea
= IR(refa
->op1
);
608 ofsa
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
611 if (refb
->o
== IR_ADD
&& irref_isk(refb
->op2
)) {
612 IRIns
*irk
= IR(refb
->op2
);
613 baseb
= IR(refb
->op1
);
614 ofsb
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
617 /* Treat constified pointers like base vs. base+offset. */
618 if (basea
->o
== IR_KPTR
&& baseb
->o
== IR_KPTR
) {
619 ofsb
+= (char *)ir_kptr(baseb
) - (char *)ir_kptr(basea
);
622 /* This implements (very) strict aliasing rules.
623 ** Different types do NOT alias, except for differences in signedness.
624 ** Type punning through unions is allowed (but forces a reload).
626 if (basea
== baseb
) {
627 ptrdiff_t sza
= irt_size(xa
->t
), szb
= irt_size(xb
->t
);
629 if (sza
== szb
&& irt_isfp(xa
->t
) == irt_isfp(xb
->t
))
630 return ALIAS_MUST
; /* Same-sized, same-kind. May need to convert. */
631 } else if (ofsa
+ sza
<= ofsb
|| ofsb
+ szb
<= ofsa
) {
632 return ALIAS_NO
; /* Non-overlapping base+-o1 vs. base+-o2. */
634 /* NYI: extract, extend or reinterpret bits (int <-> fp). */
635 return ALIAS_MAY
; /* Overlapping or type punning: force reload. */
637 if (!irt_sametype(xa
->t
, xb
->t
) &&
638 !(irt_typerange(xa
->t
, IRT_I8
, IRT_U64
) &&
639 ((xa
->t
.irt
- IRT_I8
) ^ (xb
->t
.irt
- IRT_I8
)) == 1))
641 /* NYI: structural disambiguation. */
642 return aa_cnew(J
, basea
, baseb
); /* Try to disambiguate allocations. */
645 /* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */
646 static IRRef
reassoc_trycse(jit_State
*J
, IROp op
, IRRef op1
, IRRef op2
)
648 IRRef ref
= J
->chain
[op
];
650 if (op2
> lim
) { lim
= op2
; op2
= op1
; op1
= lim
; }
653 if (ir
->op1
== op1
&& ir
->op2
== op2
)
660 /* Reassociate index references. */
661 static IRRef
reassoc_xref(jit_State
*J
, IRIns
*ir
)
664 if (ir
->o
== IR_ADD
&& irref_isk(ir
->op2
)) { /* Get constant offset. */
665 IRIns
*irk
= IR(ir
->op2
);
666 ofs
= (LJ_64
&& irk
->o
== IR_KINT64
) ? (ptrdiff_t)ir_k64(irk
)->u64
:
670 if (ir
->o
== IR_ADD
) { /* Add of base + index. */
671 /* Index ref > base ref for loop-carried dependences. Only check op1. */
672 IRIns
*ir2
, *ir1
= IR(ir
->op1
);
675 /* Determine index shifts. Don't bother with IR_MUL here. */
676 if (ir1
->o
== IR_BSHL
&& irref_isk(ir1
->op2
))
677 shift
= IR(ir1
->op2
)->i
;
678 else if (ir1
->o
== IR_ADD
&& ir1
->op1
== ir1
->op2
)
683 /* A non-reassociated add. Must be a loop-carried dependence. */
684 if (ir2
->o
== IR_ADD
&& irt_isint(ir2
->t
) && irref_isk(ir2
->op2
))
685 ofs
+= (ptrdiff_t)IR(ir2
->op2
)->i
<< shift
;
689 /* Try to CSE the reassociated chain. Give up if not found. */
691 !(idxref
= reassoc_trycse(J
, ir1
->o
, idxref
,
692 ir1
->o
== IR_BSHL
? ir1
->op2
: idxref
)))
694 if (!(idxref
= reassoc_trycse(J
, IR_ADD
, idxref
, ir
->op2
)))
697 IRRef refk
= tref_ref(lj_ir_kintp(J
, ofs
));
698 if (!(idxref
= reassoc_trycse(J
, IR_ADD
, idxref
, refk
)))
701 return idxref
; /* Success, found a reassociated index reference. Phew. */
703 return 0; /* Failure. */
706 /* XLOAD forwarding. */
707 TRef LJ_FASTCALL
lj_opt_fwd_xload(jit_State
*J
)
709 IRRef xref
= fins
->op1
;
710 IRIns
*xr
= IR(xref
);
711 IRRef lim
= xref
; /* Search limit. */
714 if ((fins
->op2
& IRXLOAD_READONLY
))
716 if ((fins
->op2
& IRXLOAD_VOLATILE
))
719 /* Search for conflicting stores. */
720 ref
= J
->chain
[IR_XSTORE
];
722 if (J
->chain
[IR_CALLXS
] > lim
) lim
= J
->chain
[IR_CALLXS
];
723 if (J
->chain
[IR_XBAR
] > lim
) lim
= J
->chain
[IR_XBAR
];
725 IRIns
*store
= IR(ref
);
726 switch (aa_xref(J
, xr
, fins
, store
)) {
727 case ALIAS_NO
: break; /* Continue searching. */
728 case ALIAS_MAY
: lim
= ref
; goto cselim
; /* Limit search for load. */
730 /* Emit conversion if the loaded type doesn't match the forwarded type. */
731 if (!irt_sametype(fins
->t
, IR(store
->op2
)->t
)) {
732 IRType dt
= irt_type(fins
->t
), st
= irt_type(IR(store
->op2
)->t
);
733 if (dt
== IRT_I8
|| dt
== IRT_I16
) { /* Trunc + sign-extend. */
734 st
= dt
| IRCONV_SEXT
;
736 } else if (dt
== IRT_U8
|| dt
== IRT_U16
) { /* Trunc + zero-extend. */
740 fins
->ot
= IRT(IR_CONV
, dt
);
741 fins
->op1
= store
->op2
;
742 fins
->op2
= (dt
<<5)|st
;
745 return store
->op2
; /* Store forwarding. */
751 /* Try to find a matching load. Below the conflicting store, if any. */
752 ref
= J
->chain
[IR_XLOAD
];
754 /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */
755 if (IR(ref
)->op1
== xref
&& irt_sametype(IR(ref
)->t
, fins
->t
))
760 /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */
761 if (!(fins
->op2
& IRXLOAD_READONLY
) && J
->chain
[IR_LOOP
] &&
762 xref
== fins
->op1
&& (xref
= reassoc_xref(J
, xr
)) != 0) {
763 ref
= J
->chain
[IR_XSTORE
];
764 while (ref
> lim
) /* Skip stores that have already been checked. */
768 goto retry
; /* Retry with the reassociated reference. */
774 /* XSTORE elimination. */
775 TRef LJ_FASTCALL
lj_opt_dse_xstore(jit_State
*J
)
777 IRRef xref
= fins
->op1
;
778 IRIns
*xr
= IR(xref
);
779 IRRef lim
= xref
; /* Search limit. */
780 IRRef val
= fins
->op2
; /* Stored value reference. */
781 IRRef1
*refp
= &J
->chain
[IR_XSTORE
];
783 if (J
->chain
[IR_CALLXS
] > lim
) lim
= J
->chain
[IR_CALLXS
];
784 if (J
->chain
[IR_XBAR
] > lim
) lim
= J
->chain
[IR_XBAR
];
785 if (J
->chain
[IR_XSNEW
] > lim
) lim
= J
->chain
[IR_XSNEW
];
786 while (ref
> lim
) { /* Search for redundant or conflicting stores. */
787 IRIns
*store
= IR(ref
);
788 switch (aa_xref(J
, xr
, fins
, store
)) {
790 break; /* Continue searching. */
792 if (store
->op2
!= val
) /* Conflict if the value is different. */
794 break; /* Otherwise continue searching. */
796 if (store
->op2
== val
) /* Same value: drop the new store. */
798 /* Different value: try to eliminate the redundant store. */
799 if (ref
> J
->chain
[IR_LOOP
]) { /* Quick check to avoid crossing LOOP. */
801 /* Check for any intervening guards or any XLOADs (no AA performed). */
802 for (ir
= IR(J
->cur
.nins
-1); ir
> store
; ir
--)
803 if (irt_isguard(ir
->t
) || ir
->o
== IR_XLOAD
)
804 goto doemit
; /* No elimination possible. */
805 /* Remove redundant store from chain and replace with NOP. */
808 /* Now emit the new store instead. */
812 ref
= *(refp
= &store
->prev
);
815 return EMITFOLD
; /* Otherwise we have a conflict or simply no match. */
818 /* -- Forwarding of lj_tab_len -------------------------------------------- */
820 /* This is rather simplistic right now, but better than nothing. */
821 TRef LJ_FASTCALL
lj_opt_fwd_tab_len(jit_State
*J
)
823 IRRef tab
= fins
->op1
; /* Table reference. */
824 IRRef lim
= tab
; /* Search limit. */
827 /* Any ASTORE is a conflict and limits the search. */
828 if (J
->chain
[IR_ASTORE
] > lim
) lim
= J
->chain
[IR_ASTORE
];
830 /* Search for conflicting HSTORE with numeric key. */
831 ref
= J
->chain
[IR_HSTORE
];
833 IRIns
*store
= IR(ref
);
834 IRIns
*href
= IR(store
->op1
);
835 IRIns
*key
= IR(href
->op2
);
836 if (irt_isnum(key
->o
== IR_KSLOT
? IR(key
->op1
)->t
: key
->t
)) {
837 lim
= ref
; /* Conflicting store found, limits search for TLEN. */
843 /* Try to find a matching load. Below the conflicting store, if any. */
844 return lj_opt_cselim(J
, lim
);
847 /* -- ASTORE/HSTORE previous type analysis -------------------------------- */
849 /* Check whether the previous value for a table store is non-nil.
850 ** This can be derived either from a previous store or from a previous
851 ** load (because all loads from tables perform a type check).
853 ** The result of the analysis can be used to avoid the metatable check
854 ** and the guard against HREF returning niltv. Both of these are cheap,
855 ** so let's not spend too much effort on the analysis.
857 ** A result of 1 is exact: previous value CANNOT be nil.
858 ** A result of 0 is inexact: previous value MAY be nil.
860 int lj_opt_fwd_wasnonnil(jit_State
*J
, IROpT loadop
, IRRef xref
)
862 /* First check stores. */
863 IRRef ref
= J
->chain
[loadop
+IRDELTA_L2S
];
865 IRIns
*store
= IR(ref
);
866 if (store
->op1
== xref
) { /* Same xREF. */
867 /* A nil store MAY alias, but a non-nil store MUST alias. */
868 return !irt_isnil(store
->t
);
869 } else if (irt_isnil(store
->t
)) { /* Must check any nil store. */
870 IRRef skref
= IR(store
->op1
)->op2
;
871 IRRef xkref
= IR(xref
)->op2
;
872 /* Same key type MAY alias. Need ALOAD check due to multiple int types. */
873 if (loadop
== IR_ALOAD
|| irt_sametype(IR(skref
)->t
, IR(xkref
)->t
)) {
874 if (skref
== xkref
|| !irref_isk(skref
) || !irref_isk(xkref
))
875 return 0; /* A nil store with same const key or var key MAY alias. */
876 /* Different const keys CANNOT alias. */
877 } /* Different key types CANNOT alias. */
878 } /* Other non-nil stores MAY alias. */
882 /* Check loads since nothing could be derived from stores. */
883 ref
= J
->chain
[loadop
];
885 IRIns
*load
= IR(ref
);
886 if (load
->op1
== xref
) { /* Same xREF. */
887 /* A nil load MAY alias, but a non-nil load MUST alias. */
888 return !irt_isnil(load
->t
);
889 } /* Other non-nil loads MAY alias. */
892 return 0; /* Nothing derived at all, previous value MAY be nil. */
895 /* ------------------------------------------------------------------------ */