diff options
Diffstat (limited to 'src/lj_opt_split.c')
-rw-r--r-- | src/lj_opt_split.c | 130 |
1 files changed, 128 insertions, 2 deletions
diff --git a/src/lj_opt_split.c b/src/lj_opt_split.c index 5a8c33b9..6ab509eb 100644 --- a/src/lj_opt_split.c +++ b/src/lj_opt_split.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) | 11 | #if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) |
12 | 12 | ||
13 | #include "lj_err.h" | 13 | #include "lj_err.h" |
14 | #include "lj_buf.h" | ||
14 | #include "lj_str.h" | 15 | #include "lj_str.h" |
15 | #include "lj_ir.h" | 16 | #include "lj_ir.h" |
16 | #include "lj_jit.h" | 17 | #include "lj_jit.h" |
@@ -139,6 +140,7 @@ static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir, | |||
139 | ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); | 140 | ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); |
140 | return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); | 141 | return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); |
141 | } | 142 | } |
143 | #endif | ||
142 | 144 | ||
143 | /* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */ | 145 | /* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */ |
144 | static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, | 146 | static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, |
@@ -155,7 +157,6 @@ static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, | |||
155 | ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); | 157 | ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); |
156 | return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); | 158 | return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); |
157 | } | 159 | } |
158 | #endif | ||
159 | 160 | ||
160 | /* Emit a CALLN with two split 64 bit arguments. */ | 161 | /* Emit a CALLN with two split 64 bit arguments. */ |
161 | static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir, | 162 | static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir, |
@@ -195,6 +196,118 @@ static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref) | |||
195 | return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs)); | 196 | return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs)); |
196 | } | 197 | } |
197 | 198 | ||
199 | #if LJ_HASFFI | ||
200 | static IRRef split_bitshift(jit_State *J, IRRef1 *hisubst, | ||
201 | IRIns *oir, IRIns *nir, IRIns *ir) | ||
202 | { | ||
203 | IROp op = ir->o; | ||
204 | IRRef kref = nir->op2; | ||
205 | if (irref_isk(kref)) { /* Optimize constant shifts. */ | ||
206 | int32_t k = (IR(kref)->i & 63); | ||
207 | IRRef lo = nir->op1, hi = hisubst[ir->op1]; | ||
208 | if (op == IR_BROL || op == IR_BROR) { | ||
209 | if (op == IR_BROR) k = (-k & 63); | ||
210 | if (k >= 32) { IRRef t = lo; lo = hi; hi = t; k -= 32; } | ||
211 | if (k == 0) { | ||
212 | passthrough: | ||
213 | J->cur.nins--; | ||
214 | ir->prev = lo; | ||
215 | return hi; | ||
216 | } else { | ||
217 | TRef k1, k2; | ||
218 | IRRef t1, t2, t3, t4; | ||
219 | J->cur.nins--; | ||
220 | k1 = lj_ir_kint(J, k); | ||
221 | k2 = lj_ir_kint(J, (-k & 31)); | ||
222 | t1 = split_emit(J, IRTI(IR_BSHL), lo, k1); | ||
223 | t2 = split_emit(J, IRTI(IR_BSHL), hi, k1); | ||
224 | t3 = split_emit(J, IRTI(IR_BSHR), lo, k2); | ||
225 | t4 = split_emit(J, IRTI(IR_BSHR), hi, k2); | ||
226 | ir->prev = split_emit(J, IRTI(IR_BOR), t1, t4); | ||
227 | return split_emit(J, IRTI(IR_BOR), t2, t3); | ||
228 | } | ||
229 | } else if (k == 0) { | ||
230 | goto passthrough; | ||
231 | } else if (k < 32) { | ||
232 | if (op == IR_BSHL) { | ||
233 | IRRef t1 = split_emit(J, IRTI(IR_BSHL), hi, kref); | ||
234 | IRRef t2 = split_emit(J, IRTI(IR_BSHR), lo, lj_ir_kint(J, (-k&31))); | ||
235 | return split_emit(J, IRTI(IR_BOR), t1, t2); | ||
236 | } else { | ||
237 | IRRef t1 = ir->prev, t2; | ||
238 | lua_assert(op == IR_BSHR || op == IR_BSAR); | ||
239 | nir->o = IR_BSHR; | ||
240 | t2 = split_emit(J, IRTI(IR_BSHL), hi, lj_ir_kint(J, (-k&31))); | ||
241 | ir->prev = split_emit(J, IRTI(IR_BOR), t1, t2); | ||
242 | return split_emit(J, IRTI(op), hi, kref); | ||
243 | } | ||
244 | } else { | ||
245 | if (op == IR_BSHL) { | ||
246 | if (k == 32) | ||
247 | J->cur.nins--; | ||
248 | else | ||
249 | lo = ir->prev; | ||
250 | ir->prev = lj_ir_kint(J, 0); | ||
251 | return lo; | ||
252 | } else { | ||
253 | lua_assert(op == IR_BSHR || op == IR_BSAR); | ||
254 | if (k == 32) { | ||
255 | J->cur.nins--; | ||
256 | ir->prev = hi; | ||
257 | } else { | ||
258 | nir->op1 = hi; | ||
259 | } | ||
260 | if (op == IR_BSHR) | ||
261 | return lj_ir_kint(J, 0); | ||
262 | else | ||
263 | return split_emit(J, IRTI(IR_BSAR), hi, lj_ir_kint(J, 31)); | ||
264 | } | ||
265 | } | ||
266 | } | ||
267 | return split_call_li(J, hisubst, oir, ir, | ||
268 | op - IR_BSHL + IRCALL_lj_carith_shl64); | ||
269 | } | ||
270 | |||
271 | static IRRef split_bitop(jit_State *J, IRRef1 *hisubst, | ||
272 | IRIns *nir, IRIns *ir) | ||
273 | { | ||
274 | IROp op = ir->o; | ||
275 | IRRef hi, kref = nir->op2; | ||
276 | if (irref_isk(kref)) { /* Optimize bit operations with lo constant. */ | ||
277 | int32_t k = IR(kref)->i; | ||
278 | if (k == 0 || k == -1) { | ||
279 | if (op == IR_BAND) k = ~k; | ||
280 | if (k == 0) { | ||
281 | J->cur.nins--; | ||
282 | ir->prev = nir->op1; | ||
283 | } else if (op == IR_BXOR) { | ||
284 | nir->o = IR_BNOT; | ||
285 | nir->op2 = 0; | ||
286 | } else { | ||
287 | J->cur.nins--; | ||
288 | ir->prev = kref; | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | hi = hisubst[ir->op1]; | ||
293 | kref = hisubst[ir->op2]; | ||
294 | if (irref_isk(kref)) { /* Optimize bit operations with hi constant. */ | ||
295 | int32_t k = IR(kref)->i; | ||
296 | if (k == 0 || k == -1) { | ||
297 | if (op == IR_BAND) k = ~k; | ||
298 | if (k == 0) { | ||
299 | return hi; | ||
300 | } else if (op == IR_BXOR) { | ||
301 | return split_emit(J, IRTI(IR_BNOT), hi, 0); | ||
302 | } else { | ||
303 | return kref; | ||
304 | } | ||
305 | } | ||
306 | } | ||
307 | return split_emit(J, IRTI(op), hi, kref); | ||
308 | } | ||
309 | #endif | ||
310 | |||
198 | /* Substitute references of a snapshot. */ | 311 | /* Substitute references of a snapshot. */ |
199 | static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir) | 312 | static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir) |
200 | { | 313 | { |
@@ -214,7 +327,7 @@ static void split_ir(jit_State *J) | |||
214 | IRRef nins = J->cur.nins, nk = J->cur.nk; | 327 | IRRef nins = J->cur.nins, nk = J->cur.nk; |
215 | MSize irlen = nins - nk; | 328 | MSize irlen = nins - nk; |
216 | MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); | 329 | MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); |
217 | IRIns *oir = (IRIns *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, need); | 330 | IRIns *oir = (IRIns *)lj_buf_tmp(J->L, need); |
218 | IRRef1 *hisubst; | 331 | IRRef1 *hisubst; |
219 | IRRef ref, snref; | 332 | IRRef ref, snref; |
220 | SnapShot *snap; | 333 | SnapShot *snap; |
@@ -438,6 +551,19 @@ static void split_ir(jit_State *J) | |||
438 | irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : | 551 | irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : |
439 | IRCALL_lj_carith_powu64); | 552 | IRCALL_lj_carith_powu64); |
440 | break; | 553 | break; |
554 | case IR_BNOT: | ||
555 | hi = split_emit(J, IRTI(IR_BNOT), hiref, 0); | ||
556 | break; | ||
557 | case IR_BSWAP: | ||
558 | ir->prev = split_emit(J, IRTI(IR_BSWAP), hiref, 0); | ||
559 | hi = nref; | ||
560 | break; | ||
561 | case IR_BAND: case IR_BOR: case IR_BXOR: | ||
562 | hi = split_bitop(J, hisubst, nir, ir); | ||
563 | break; | ||
564 | case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR: | ||
565 | hi = split_bitshift(J, hisubst, oir, nir, ir); | ||
566 | break; | ||
441 | case IR_FLOAD: | 567 | case IR_FLOAD: |
442 | lua_assert(ir->op2 == IRFL_CDATA_INT64); | 568 | lua_assert(ir->op2 == IRFL_CDATA_INT64); |
443 | hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4); | 569 | hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4); |