Add SPLIT pass to split 64 bit IR instructions for 32 bit CPUs.
[luajit-2.0.git] / src / lj_iropt.h
blobdb99c118137e0dba97f312c3414ee9cf79a1885d
1 /*
2 ** Common header for IR emitter and optimizations.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_IROPT_H
7 #define _LJ_IROPT_H
9 #include <stdarg.h>
11 #include "lj_obj.h"
12 #include "lj_jit.h"
14 #if LJ_HASJIT
15 /* IR emitter. */
16 LJ_FUNC void LJ_FASTCALL lj_ir_growtop(jit_State *J);
17 LJ_FUNC TRef LJ_FASTCALL lj_ir_emit(jit_State *J);
18 LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);
20 /* Save current IR in J->fold.ins, but do not emit it (yet). */
21 static LJ_AINLINE void lj_ir_set_(jit_State *J, uint16_t ot, IRRef1 a, IRRef1 b)
23 J->fold.ins.ot = ot; J->fold.ins.op1 = a; J->fold.ins.op2 = b;
26 #define lj_ir_set(J, ot, a, b) \
27 lj_ir_set_(J, (uint16_t)(ot), (IRRef1)(a), (IRRef1)(b))
29 /* Get ref of next IR instruction and optionally grow IR.
30 ** Note: this may invalidate all IRIns*!
32 static LJ_AINLINE IRRef lj_ir_nextins(jit_State *J)
34 IRRef ref = J->cur.nins;
35 if (LJ_UNLIKELY(ref >= J->irtoplim)) lj_ir_growtop(J);
36 J->cur.nins = ref + 1;
37 return ref;
40 /* Interning of constants. */
41 LJ_FUNC TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k);
42 LJ_FUNC void lj_ir_k64_freeall(jit_State *J);
43 LJ_FUNC TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv);
44 LJ_FUNC cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64);
45 LJ_FUNC TRef lj_ir_knum_u64(jit_State *J, uint64_t u64);
46 LJ_FUNC TRef lj_ir_knumint(jit_State *J, lua_Number n);
47 LJ_FUNC TRef lj_ir_kint64(jit_State *J, uint64_t u64);
48 LJ_FUNC TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t);
49 LJ_FUNC TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr);
50 LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t);
51 LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot);
53 #if LJ_64
54 #define lj_ir_kintp(J, k) lj_ir_kint64(J, (uint64_t)(k))
55 #else
56 #define lj_ir_kintp(J, k) lj_ir_kint(J, (int32_t)(k))
57 #endif
59 static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n)
61 TValue tv;
62 tv.n = n;
63 return lj_ir_knum_u64(J, tv.u64);
66 #define lj_ir_kstr(J, str) lj_ir_kgc(J, obj2gco((str)), IRT_STR)
67 #define lj_ir_ktab(J, tab) lj_ir_kgc(J, obj2gco((tab)), IRT_TAB)
68 #define lj_ir_kfunc(J, func) lj_ir_kgc(J, obj2gco((func)), IRT_FUNC)
69 #define lj_ir_kptr(J, ptr) lj_ir_kptr_(J, IR_KPTR, (ptr))
70 #define lj_ir_kkptr(J, ptr) lj_ir_kptr_(J, IR_KKPTR, (ptr))
72 /* Special FP constants. */
73 #define lj_ir_knum_zero(J) lj_ir_knum_u64(J, U64x(00000000,00000000))
74 #define lj_ir_knum_one(J) lj_ir_knum_u64(J, U64x(3ff00000,00000000))
75 #define lj_ir_knum_tobit(J) lj_ir_knum_u64(J, U64x(43380000,00000000))
77 /* Special 128 bit SIMD constants. */
78 #define lj_ir_knum_abs(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_ABS))
79 #define lj_ir_knum_neg(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_NEG))
81 /* Access to constants. */
82 LJ_FUNC void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir);
84 /* Convert IR operand types. */
85 LJ_FUNC TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr);
86 LJ_FUNC TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr);
87 LJ_FUNC TRef LJ_FASTCALL lj_ir_tobit(jit_State *J, TRef tr);
88 LJ_FUNC TRef LJ_FASTCALL lj_ir_toint(jit_State *J, TRef tr);
90 /* Miscellaneous IR ops. */
91 LJ_FUNC int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op);
92 LJ_FUNC int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op);
93 LJ_FUNC void lj_ir_rollback(jit_State *J, IRRef ref);
95 /* Emit IR instructions with on-the-fly optimizations. */
96 LJ_FUNC TRef LJ_FASTCALL lj_opt_fold(jit_State *J);
97 LJ_FUNC TRef LJ_FASTCALL lj_opt_cse(jit_State *J);
98 LJ_FUNC TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim);
100 /* Special return values for the fold functions. */
101 enum {
102 NEXTFOLD, /* Couldn't fold, pass on. */
103 RETRYFOLD, /* Retry fold with modified fins. */
104 KINTFOLD, /* Return ref for int constant in fins->i. */
105 FAILFOLD, /* Guard would always fail. */
106 DROPFOLD, /* Guard eliminated. */
107 MAX_FOLD
110 #define INTFOLD(k) ((J->fold.ins.i = (k)), (TRef)KINTFOLD)
111 #define INT64FOLD(k) (lj_ir_kint64(J, (k)))
112 #define CONDFOLD(cond) ((TRef)FAILFOLD + (TRef)(cond))
113 #define LEFTFOLD (J->fold.ins.op1)
114 #define RIGHTFOLD (J->fold.ins.op2)
115 #define CSEFOLD (lj_opt_cse(J))
116 #define EMITFOLD (lj_ir_emit(J))
118 /* Load/store forwarding. */
119 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J);
120 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J);
121 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J);
122 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J);
123 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J);
124 LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J);
125 LJ_FUNC int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J);
126 LJ_FUNC int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim);
127 LJ_FUNC int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref);
129 /* Dead-store elimination. */
130 LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J);
131 LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J);
132 LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J);
133 LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J);
135 /* Narrowing. */
136 LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J);
137 LJ_FUNC TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc);
138 LJ_FUNC TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc);
139 LJ_FUNC IRType lj_opt_narrow_forl(cTValue *forbase);
141 /* Optimization passes. */
142 LJ_FUNC void lj_opt_dce(jit_State *J);
143 LJ_FUNC int lj_opt_loop(jit_State *J);
144 #if LJ_HASFFI && LJ_32
145 LJ_FUNC void lj_opt_split(jit_State *J);
146 #else
147 #define lj_opt_split(J) UNUSED(J)
148 #endif
150 #endif
152 #endif