From 0f312c9cb88cb85bd10f2ce79491b3a8fab072bf Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 30 Nov 2026 22:27:03 +0100 Subject: [PATCH 1/3] Update IR IR commit: b8a6cee5bbffea2299756a932b2388345dacd5ba --- ext/opcache/jit/ir/.gitignore | 9 + ext/opcache/jit/ir/dynasm/dasm_arm64.lua | 6 +- ext/opcache/jit/ir/ir.c | 94 ++- ext/opcache/jit/ir/ir.h | 36 +- ext/opcache/jit/ir/ir_aarch64.dasc | 393 +++++++------ ext/opcache/jit/ir/ir_cfg.c | 81 ++- ext/opcache/jit/ir/ir_check.c | 210 ++++++- ext/opcache/jit/ir/ir_dump.c | 2 +- ext/opcache/jit/ir/ir_emit.c | 205 ++++++- ext/opcache/jit/ir/ir_fold.h | 121 +++- ext/opcache/jit/ir/ir_gcm.c | 70 ++- ext/opcache/jit/ir/ir_private.h | 10 + ext/opcache/jit/ir/ir_ra.c | 246 ++++---- ext/opcache/jit/ir/ir_sccp.c | 47 +- ext/opcache/jit/ir/ir_x86.dasc | 695 +++++++++++++++++------ 15 files changed, 1614 insertions(+), 611 deletions(-) diff --git a/ext/opcache/jit/ir/.gitignore b/ext/opcache/jit/ir/.gitignore index 367a68671bc0..435fb5b2db28 100644 --- a/ext/opcache/jit/ir/.gitignore +++ b/ext/opcache/jit/ir/.gitignore @@ -1,4 +1,5 @@ *.o +libir.a *.log *.dot *.pdf @@ -18,8 +19,16 @@ tests/**/*.ir tests/**/*.out tests/**/*.log +llvm_tests/**/*.diff +llvm_tests/**/*.exp +llvm_tests/**/*.ll +llvm_tests/**/*.out + win32/vcpkg win32/build_* fuzz/build/ fuzz/corpus/ + +# macOS debug symbols (clang -g) +*.dSYM/ diff --git a/ext/opcache/jit/ir/dynasm/dasm_arm64.lua b/ext/opcache/jit/ir/dynasm/dasm_arm64.lua index 2d6fe202bcca..1cc33e91ba62 100644 --- a/ext/opcache/jit/ir/dynasm/dasm_arm64.lua +++ b/ext/opcache/jit/ir/dynasm/dasm_arm64.lua @@ -811,8 +811,8 @@ map_op = { ["strh_*"] = "78000000DwL", ["ldrh_*"] = "78400000DwL", ["ldrsh_*"] = "78c00000DwL|78800000DxL", - ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL|3c800000DqL", - ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|9c000000DqB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL|3cc00000DqL", + ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL|3c800000DqL|3c000000DbL|7c000000DhL", + ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|9c000000DqB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL|3cc00000DqL|3c400000DbL|7c400000DhL", ["ldrsw_*"] = "98000000DxB|b8800000DxL", -- NOTE: ldur etc. are handled by ldr et al. @@ -962,7 +962,7 @@ local function parse_template(params, template, nparams, pos) werror("bad register type") end parse_reg_type = false - elseif p == "x" or p == "w" or p == "d" or p == "s" or p == "q" then + elseif p == "x" or p == "w" or p == "d" or p == "s" or p == "q" or p == "h" or p == "b" then if parse_reg_type ~= p then werror("register size mismatch") end diff --git a/ext/opcache/jit/ir/ir.c b/ext/opcache/jit/ir/ir.c index 6cfd2a0007f6..3199e1724c03 100644 --- a/ext/opcache/jit/ir/ir.c +++ b/ext/opcache/jit/ir/ir.c @@ -114,30 +114,24 @@ void ir_print_escaped_str(const char *s, size_t len, FILE *f) } } -static void ir_print_double(double v, FILE *f) +static void ir_print_double(const double *v, FILE *f) { char buf[128]; - if (isnan(v)) { - union { - double d; - long long i; - } u1, u2, u3; - u1.d = v; - u2.d = NAN; - u3.d = -NAN; - if (u1.i == u2.i) { - fprintf(f, "nan"); - } else if (u1.i == u3.i) { + if (isnan(*v)) { + if (*(uint64_t*)(v) == 0xfff8000000000000) { fprintf(f, "-nan"); } else { - fprintf(f, "nan(0x%llx)", u1.i); + fprintf(f, "nan"); + if (*(uint64_t*)(v) & 0x8007ffffffffffff) { + fprintf(f, "(0x%" PRIx64 ")", *(uint64_t*)(v)); + } } } else { - snprintf(buf, sizeof(buf), "%g", v); - if (strtod(buf, NULL) != v) { - snprintf(buf, sizeof(buf), "%.53e", v); - if (strtod(buf, NULL) != v) { + snprintf(buf, sizeof(buf), "%g", *v); + if (strtod(buf, NULL) != *v) { + snprintf(buf, sizeof(buf), "%.53e", *v); + if (strtod(buf, NULL) != *v) { IR_ASSERT(0 && "can't format double"); } } @@ -145,30 +139,24 @@ static void ir_print_double(double v, FILE *f) } } -static void ir_print_float(float v, FILE *f) +static void ir_print_float(const float *v, FILE *f) { char buf[128]; - if (isnan(v)) { - union { - float f; - int i; - } u1, u2, u3; - u1.f = v; - u2.f = NAN; - u3.f = -NAN; - if (u1.i == u2.i) { - fprintf(f, "nan"); - } else if (u1.i == u3.i) { + if (isnan(*v)) { + if (*(uint32_t*)(v) == 0xffc00000) { fprintf(f, "-nan"); } else { - fprintf(f, "nan(0x%x)", u1.i); + fprintf(f, "nan"); + if (*(uint32_t*)(v) & 0x803fffff) { + fprintf(f, "(0x%x)", *(uint32_t*)(v)); + } } } else { - snprintf(buf, sizeof(buf), "%g", v); - if (strtod(buf, NULL) != v) { - snprintf(buf, sizeof(buf), "%.24e", v); - if (strtod(buf, NULL) != v) { + snprintf(buf, sizeof(buf), "%g", *v); + if (strtod(buf, NULL) != *v) { + snprintf(buf, sizeof(buf), "%.24e", *v); + if (strtod(buf, NULL) != *v) { IR_ASSERT(0 && "can't format float"); } } @@ -260,19 +248,19 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted } break; case IR_DOUBLE: - ir_print_double(*(double*)p, f); + ir_print_double((double*)p, f); while (--n) { p = (char*)p + sizeof(double); fprintf(f, ", "); - ir_print_double(*(double*)p, f); + ir_print_double((double*)p, f); } break; case IR_FLOAT: - ir_print_float(*(float*)p, f); + ir_print_float((float*)p, f); while (--n) { p = (char*)p + sizeof(float); fprintf(f, ", "); - ir_print_float(*(float*)p, f); + ir_print_float((float*)p, f); } break; default: @@ -339,10 +327,10 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted fprintf(f, "%" PRIi64, insn->val.i64); break; case IR_DOUBLE: - ir_print_double(insn->val.d, f); + ir_print_double(&insn->val.d, f); break; case IR_FLOAT: - ir_print_float(insn->val.f, f); + ir_print_float(&insn->val.f, f); break; default: IR_ASSERT(0); @@ -2416,7 +2404,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_load_i(const ir_ctx *ctx, ir_ref ref, i while (ref > limit) { insn = &ctx->ir_base[ref]; - if (insn->op == IR_LOAD) { + if (insn->op == IR_LOAD || insn->op == IR_LOAD_v) { if (insn->op2 == addr) { if (insn->type == type) { return ref; /* load forwarding (L2L) */ @@ -2427,7 +2415,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_load_i(const ir_ctx *ctx, ir_ref ref, i return ref; /* partial load forwarding (L2L) */ } } - } else if (insn->op == IR_STORE) { + } else if (insn->op == IR_STORE || insn->op == IR_STORE_v) { ir_type type2 = ctx->ir_base[insn->op3].type; if (insn->op2 == addr) { @@ -2492,7 +2480,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vload_i(const ir_ctx *ctx, ir_ref ref, while (ref > var) { insn = &ctx->ir_base[ref]; - if (insn->op == IR_VLOAD) { + if (insn->op == IR_VLOAD || insn->op == IR_VLOAD_v) { if (insn->op2 == var) { if (insn->type == type) { return ref; /* load forwarding (L2L) */ @@ -2503,7 +2491,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vload_i(const ir_ctx *ctx, ir_ref ref, return ref; /* partial load forwarding (L2L) */ } } - } else if (insn->op == IR_VSTORE) { + } else if (insn->op == IR_VSTORE || insn->op == IR_VSTORE_v) { ir_type type2 = ctx->ir_base[insn->op3].type; if (insn->op2 == var) { @@ -2574,14 +2562,14 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref while (ref > limit) { insn = &ctx->ir_base[ref]; - if (insn->op == IR_STORE) { + if (insn->op == IR_STORE || insn->op == IR_STORE_v) { if (insn->op2 == addr) { if (ctx->ir_base[insn->op3].type == type) { if (insn->op3 == val) { /* dead STORE (store the same value once again) */ return ref; } else { - if (!guarded) { + if (!guarded && insn->op == IR_STORE) { /* the previous STORE is dead (there are no LOADs) */ if (!ctx->use_lists) { if (next) { @@ -2607,7 +2595,8 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref } if (!IR_IS_CONST_REF(insn->op3)) { ir_use_list_remove_one(ctx, insn->op3, ref); - if (ctx->iter_worklist && ctx->use_lists[insn->op3].count == 0) { + if (ctx->iter_worklist && (ctx->use_lists[insn->op3].count == 0 + || ir_is_dead_load(ctx, insn->op3))) { ir_bitqueue_add(ctx->iter_worklist, insn->op3); } } @@ -2624,7 +2613,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref type2 = ctx->ir_base[insn->op3].type; goto check_aliasing; } - } else if (insn->op == IR_LOAD) { + } else if (insn->op == IR_LOAD || insn->op == IR_LOAD_v) { if (insn->op2 == addr) { if (ref == val) { /* dead STORE (store the value that was loaded before) */ @@ -2681,13 +2670,13 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re while (ref > limit) { insn = &ctx->ir_base[ref]; - if (insn->op == IR_VSTORE) { + if (insn->op == IR_VSTORE || insn->op == IR_VSTORE_v) { if (insn->op2 == var) { if (insn->op3 == val) { /* dead VSTORE */ return ref; } else { - if (!guarded) { + if (!guarded && insn->op == IR_VSTORE) { /* the previous VSTORE is dead (there are no VLOADs) */ if (!ctx->use_lists) { if (next) { @@ -2710,7 +2699,8 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re } if (!IR_IS_CONST_REF(insn->op3)) { ir_use_list_remove_one(ctx, insn->op3, ref); - if (ctx->iter_worklist && ctx->use_lists[insn->op3].count == 0) { + if (ctx->iter_worklist && (ctx->use_lists[insn->op3].count == 0 + || ir_is_dead_load(ctx, insn->op3))) { ir_bitqueue_add(ctx->iter_worklist, insn->op3); } } @@ -2721,7 +2711,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re break; } } - } else if (insn->op == IR_VLOAD) { + } else if (insn->op == IR_VLOAD || insn->op == IR_VLOAD_v) { if (insn->op2 == var) { if (ref == val) { /* dead VSTORE */ diff --git a/ext/opcache/jit/ir/ir.h b/ext/opcache/jit/ir/ir.h index 8c2029c03beb..ff9277191c62 100644 --- a/ext/opcache/jit/ir/ir.h +++ b/ext/opcache/jit/ir/ir.h @@ -665,16 +665,7 @@ void ir_strtab_free(ir_strtab *strtab); #define IR_OPT_MEM2SSA (1<<22) #define IR_OPT_CODEGEN (1<<23) #define IR_OPT_TAILCALL (1<<24) - -/* debug related */ -#ifdef IR_DEBUG -# define IR_DEBUG_SCCP (1<<26) -# define IR_DEBUG_GCM (1<<27) -# define IR_DEBUG_GCM_SPLIT (1<<28) -# define IR_DEBUG_SCHEDULE (1<<29) -# define IR_DEBUG_RA (1<<30) -# define IR_DEBUG_BB_SCHEDULE (1U<<31) -#endif +#define IR_NO_OPT (1<<25) /* Calling Conventions */ #define IR_CC_DEFAULT 0x00 @@ -694,6 +685,15 @@ void ir_strtab_free(ir_strtab *strtab); #define IR_BUILTIN_FUNC IR_CC_BUILTIN #define IR_FASTCALL_FUNC IR_CC_FASTCALL +/* Flags for ir_ctx.debug_flags (they work only in "debug" build) */ +#define IR_DEBUG_SCCP (1<<0) +#define IR_DEBUG_GCM (1<<1) +#define IR_DEBUG_GCM_SPLIT (1<<2) +#define IR_DEBUG_SCHEDULE (1<<3) +#define IR_DEBUG_RA (1<<4) +#define IR_DEBUG_BB_SCHEDULE (1<<5) +#define IR_DEBUG_DESSA (1<<6) + typedef struct _ir_ctx ir_ctx; typedef struct _ir_use_list ir_use_list; typedef struct _ir_block ir_block; @@ -738,6 +738,7 @@ struct _ir_ctx { uint32_t flags2; /* IR context private flags (see IR_* defines in ir_private.h) */ ir_type ret_type; /* Function return type */ uint32_t mflags; /* CPU specific flags (see IR_X86_... macros below) */ + uint32_t debug_flags; /* Debug flags (see IR_DEBUG_... macros, work only in DEBUG build) */ int32_t status; /* non-zero error code (see IR_ERROR_... macros), app may use negative codes */ ir_ref fold_cse_limit; /* CSE finds identical insns backward from "insn_count" to "fold_cse_limit" */ ir_insn fold_insn; /* temporary storage for folding engine */ @@ -1087,12 +1088,22 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f); /* IR to C conversion (implementation in ir_emit_c.c) */ int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f); void ir_emit_c_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f); -void ir_emit_c_sym_decl(const char *name, uint32_t flags, FILE *f); +void ir_emit_c_sym_decl(const char *name, uint32_t flags, size_t size, FILE *f); +void ir_emit_c_sym_data_str(size_t pos, const char *str, size_t len, FILE *f); +void ir_emit_c_sym_data(size_t pos, const void *data, size_t size, FILE *f); +void ir_emit_c_sym_data_pad(size_t pos, size_t size, FILE *f); +void ir_emit_c_sym_data_ref(size_t pos, ir_op op, const char *ref, uintptr_t offset, FILE *f); +void ir_emit_c_sym_data_end(uint32_t flags, FILE *f); /* IR to LLVM conversion (implementation in ir_emit_llvm.c) */ int ir_emit_llvm(ir_ctx *ctx, const char *name, FILE *f); void ir_emit_llvm_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f); -void ir_emit_llvm_sym_decl(const char *name, uint32_t flags, FILE *f); +void ir_emit_llvm_sym_decl(const char *name, uint32_t flags, size_t size, FILE *f); +void ir_emit_llvm_sym_data_str(size_t pos, const char *str, size_t len, FILE *f); +void ir_emit_llvm_sym_data(size_t pos, const void *data, size_t size, FILE *f); +void ir_emit_llvm_sym_data_pad(size_t pos, size_t size, FILE *f); +void ir_emit_llvm_sym_data_ref(size_t pos, ir_op op, const char *ref, uintptr_t offset, FILE *f); +void ir_emit_llvm_sym_data_end(uint32_t flags, FILE *f); /* IR verification API (implementation in ir_check.c) */ bool ir_check(const ir_ctx *ctx); @@ -1130,6 +1141,7 @@ IR_ALWAYS_INLINE void *ir_jit_compile(ir_ctx *ctx, int opt_level, size_t *size) return NULL; } ctx->flags &= ~(IR_OPT_CFG | IR_OPT_CODEGEN | IR_OPT_TAILCALL); + ctx->flags |= IR_NO_OPT; ir_build_def_use_lists(ctx); diff --git a/ext/opcache/jit/ir/ir_aarch64.dasc b/ext/opcache/jit/ir/ir_aarch64.dasc index 5f7c3a42e8b2..8bded7604788 100644 --- a/ext/opcache/jit/ir/ir_aarch64.dasc +++ b/ext/opcache/jit/ir/ir_aarch64.dasc @@ -523,6 +523,8 @@ int ir_get_target_constraints(ir_ctx *ctx, ir_ref ref, ir_target_constraints *co if (insn->op == IR_MOD) { constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF); n++; + } else if (insn->op == IR_SUB_OV && ir_type_size[insn->type] < 4) { + flags |= IR_DEF_CONFLICTS_WITH_INPUT_REGS; } else if (insn->op == IR_MUL_OV && ir_type_size[insn->type] != 4) { constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); n++; @@ -550,6 +552,9 @@ int ir_get_target_constraints(ir_ctx *ctx, ir_ref ref, ir_target_constraints *co if (IR_IS_CONST_REF(insn->op1)) { constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF); n++; + } else if (ir_rule(ctx, insn->op1) == IR_STATIC_ALLOCA) { + constraints->tmp_regs[n] = IR_TMP_REG(1, IR_ADDR, IR_LOAD_SUB_REF, IR_DEF_SUB_REF); + n++; } if (rule == IR_SHIFT_CONST && (insn->op == IR_ROL || insn->op == IR_ROR) @@ -1607,6 +1612,10 @@ binop_fp: case IR_PARAM: return ctx->use_lists[ref].count > 0 ? IR_PARAM : IR_SKIPPED | IR_PARAM; case IR_ALLOCA: + if (ctx->use_lists[ref].count == 1) { + /* dead ALLOCA */ + return IR_NOP | IR_SKIPPED; + } if (ctx->flags & IR_FUNCTION) { if (IR_IS_CONST_REF(insn->op2) && ctx->cfg_map[ref] == 1) { ir_insn *val = &ctx->ir_base[insn->op2]; @@ -1619,8 +1628,21 @@ binop_fp: ctx->flags2 |= IR_HAS_ALLOCA; } return IR_ALLOCA; + case IR_BLOCK_BEGIN: + case IR_TLS_ADDR: + case IR_VLOAD: + case IR_VLOAD_v: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } + return insn->op; case IR_LOAD: case IR_LOAD_v: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } if (ctx->ir_base[insn->op2].op == IR_TLS_ADDR && ir_may_fuse_tls_addr(ctx, insn->op2)) { ctx->rules[insn->op2] = IR_FUSED | IR_SIMPLE | IR_TLS_ADDR; return IR_TLS_LOAD; @@ -1646,6 +1668,10 @@ binop_fp: } break; case IR_RLOAD: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } if (IR_REGSET_IN(IR_REGSET_UNION((ir_regset)ctx->fixed_regset, IR_REGSET_FIXED), insn->op2)) { return IR_SKIPPED | IR_RLOAD; } @@ -1820,6 +1846,12 @@ binop_fp: fprintf(stderr, "ERROR: IR_ASM is not implemented yet\n"); exit(1); return IR_SKIPPED | IR_NOP; + case IR_PHI: + if (ctx->use_lists[ref].count == 0) { + /* Dead PHI */ + return IR_SKIPPED | IR_NOP; + } + return insn->op; default: break; } @@ -2170,11 +2202,9 @@ static void ir_emit_load_mem_fp(ir_ctx *ctx, ir_type type, ir_reg reg, ir_mem me } else if (width == 4) { | ldr Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else if (width == 2) { - | ldrh Rw(IR_REG_INT_TMP), [Rx(base_reg), #offset] - | fmov Rs(reg-IR_REG_FP_FIRST), Rw(IR_REG_INT_TMP) + | ldr Rh(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else if (width == 1) { - | ldrb Rw(IR_REG_INT_TMP), [Rx(base_reg), #offset] - | fmov Rs(reg-IR_REG_FP_FIRST), Rw(IR_REG_INT_TMP) + | ldr Rb(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else { IR_ASSERT(0 && "unsupported vector width"); } @@ -2218,11 +2248,9 @@ static void ir_emit_load_mem_fp(ir_ctx *ctx, ir_type type, ir_reg reg, ir_mem me } else if (width == 4) { | ldr Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else if (width == 2) { - | ldrh Rw(IR_REG_INT_TMP), [Rx(base_reg), Rx(index_reg)] - | fmov Rs(reg-IR_REG_FP_FIRST), Rw(IR_REG_INT_TMP) + | ldr Rh(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else if (width == 1) { - | ldrb Rw(IR_REG_INT_TMP), [Rx(base_reg), Rx(index_reg)] - | fmov Rs(reg-IR_REG_FP_FIRST), Rw(IR_REG_INT_TMP) + | ldr Rb(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else { IR_ASSERT(0 && "unsupported vector width"); } @@ -2407,11 +2435,9 @@ static void ir_emit_store_mem_fp(ir_ctx *ctx, ir_type type, ir_mem mem, ir_reg r } else if (width == 4) { | str Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else if (width == 2) { - | fmov Rw(IR_REG_INT_TMP), Rs(reg-IR_REG_FP_FIRST) - | strh Rw(IR_REG_INT_TMP), [Rx(base_reg), #offset] + | str Rh(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else if (width == 1) { - | fmov Rw(IR_REG_INT_TMP), Rs(reg-IR_REG_FP_FIRST) - | strb Rw(IR_REG_INT_TMP), [Rx(base_reg), #offset] + | str Rb(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset] } else { IR_ASSERT(0 && "unsupported vector width"); } @@ -2455,13 +2481,9 @@ static void ir_emit_store_mem_fp(ir_ctx *ctx, ir_type type, ir_mem mem, ir_reg r } else if (width == 4) { | str Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else if (width == 2) { - IR_ASSERT(index_reg != IR_REG_INT_TMP); - | fmov Rw(IR_REG_INT_TMP), Rs(reg-IR_REG_FP_FIRST) - | strh Rw(IR_REG_INT_TMP), [Rx(base_reg), Rx(index_reg)] + | str Rh(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else if (width == 1) { - IR_ASSERT(index_reg != IR_REG_INT_TMP); - | fmov Rw(IR_REG_INT_TMP), Rs(reg-IR_REG_FP_FIRST) - | strb Rw(IR_REG_INT_TMP), [Rx(base_reg), Rx(index_reg)] + | str Rb(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(index_reg)] } else { IR_ASSERT(0 && "unsupported vector width"); } @@ -2802,9 +2824,9 @@ static void ir_emit_epilogue(ir_ctx *ctx) | ldr Rd(i-IR_REG_FP_FIRST), [Rx(fp), #(offset+8)] } else { ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, offset); - | ldr Rx(prev-IR_REG_FP_FIRST), [Rx(fp), Rx(IR_REG_INT_TMP)] + | ldr Rd(prev-IR_REG_FP_FIRST), [Rx(fp), Rx(IR_REG_INT_TMP)] | add Rx(IR_REG_INT_TMP), Rx(IR_REG_INT_TMP), #8 - | ldr Rx(i-IR_REG_FP_FIRST), [Rx(fp), Rx(IR_REG_INT_TMP)] + | ldr Rd(i-IR_REG_FP_FIRST), [Rx(fp), Rx(IR_REG_INT_TMP)] } } prev = IR_REG_NONE; @@ -2932,9 +2954,7 @@ static void ir_emit_binop_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (ir_type_size[type] >= 4) { | ASM_REG_REG_REG_OP subs, type, def_reg, op1_reg, op2_reg } else { - if (def_reg == op2_reg) { - SWAP_REGS(op1_reg, op2_reg); - } + IR_ASSERT(def_reg != op2_reg); if (ir_type_size[type] == 2) { if (IR_IS_TYPE_SIGNED(type)) { | sxth Rw(def_reg), Rw(op1_reg) @@ -3210,6 +3230,109 @@ static void ir_emit_bic(ir_ctx *ctx, ir_ref def, ir_insn *insn) } } +static void ir_emit_cmp_int_common(ir_ctx *ctx, ir_op op, ir_type type, ir_reg op1_reg, ir_ref op1, ir_reg op2_reg, ir_ref op2) +{ + ir_backend_data *data = ctx->data; + dasm_State **Dst = &data->dasm_state; + + IR_ASSERT(op1_reg != IR_REG_NONE); + if (op2_reg == IR_REG_NONE && op1 == op2) { + op2_reg = op1_reg; + } + + if (ir_type_size[type] < 4) { + /* Short signed comparison */ + if (ir_type_size[type] == 2) { + if (op >= IR_LT && op <= IR_GT) { + | sxth Rw(IR_REG_INT_TMP), Rw(op1_reg) + } else { + | uxth Rw(IR_REG_INT_TMP), Rw(op1_reg) + } + op1_reg = IR_REG_INT_TMP; + } else if (ir_type_size[type] == 1) { + if (op >= IR_LT && op <= IR_GT) { + | sxtb Rw(IR_REG_INT_TMP), Rw(op1_reg) + } else { + | uxtb Rw(IR_REG_INT_TMP), Rw(op1_reg) + } + op1_reg = IR_REG_INT_TMP; + } else { + IR_ASSERT(0); + } + } + if (op2_reg != IR_REG_NONE) { + if (ir_type_size[type] == 8) { + | cmp Rx(op1_reg), Rx(op2_reg) + } else if (ir_type_size[type] == 4) { + | cmp Rw(op1_reg), Rw(op2_reg) + } else if (ir_type_size[type] == 2) { + if (op >= IR_LT && op <= IR_GT) { + | cmp Rw(op1_reg), Rw(op2_reg), sxth + } else { + | cmp Rw(op1_reg), Rw(op2_reg), uxth + } + } else if (ir_type_size[type] == 1) { + if (op >= IR_LT && op <= IR_GT) { + | cmp Rw(op1_reg), Rw(op2_reg), sxtb + } else { + | cmp Rw(op1_reg), Rw(op2_reg), uxtb + } + } else { + IR_ASSERT(0); + } + } else { + IR_ASSERT(IR_IS_CONST_REF(op2)); + IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[op2].op)); + int32_t val = ctx->ir_base[op2].val.i32; + + if (ir_type_size[type] == 8) { + | cmp Rx(op1_reg), #val + } else if (ir_type_size[type] == 4) { + | cmp Rw(op1_reg), #val + } else if (ir_type_size[type] == 2) { + if (op >= IR_LT && op <= IR_GT) { + val = (int32_t)(int16_t)(val & 0xffff); + if (val < 0) { + val = -val; + | cmn Rw(op1_reg), #val + } else { + | cmp Rw(op1_reg), #val + } + } else { + val = val & 0xffff; + | cmp Rw(op1_reg), #val + } + } else if (ir_type_size[type] == 1) { + if (op >= IR_LT && op <= IR_GT) { + val = (int32_t)(int8_t)(val & 0xffff); + if (val < 0) { + val = -val; + | cmn Rw(op1_reg), #val + } else { + | cmp Rw(op1_reg), #val + } + } else { + val = val & 0xff; + | cmp Rw(op1_reg), #val + } + } + } +} + +/* Test the low 8/16 bits for zero; upper bits are undefined */ +static void ir_emit_tst_narrow(ir_ctx *ctx, ir_type type, ir_reg reg) +{ + ir_backend_data *data = ctx->data; + dasm_State **Dst = &data->dasm_state; + + if (ir_type_size[type] == 2) { + | tst Rw(reg), #0xffff + } else { + IR_ASSERT(ir_type_size[type] == 1); + | tst Rw(reg), #0xff + } +} + static void ir_emit_min_max_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) { ir_backend_data *data = ctx->data; @@ -3241,8 +3364,8 @@ static void ir_emit_min_max_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_emit_load(ctx, type, op2_reg, op2); } + ir_emit_cmp_int_common(ctx, IR_IS_TYPE_SIGNED(type) ? IR_LT : IR_ULT, type, op1_reg, op1, op2_reg, op2); if (ir_type_size[type] == 8) { - | cmp Rx(op1_reg), Rx(op2_reg) if (insn->op == IR_MIN) { if (IR_IS_TYPE_SIGNED(type)) { | csel Rx(def_reg), Rx(op1_reg), Rx(op2_reg), le @@ -3258,7 +3381,6 @@ static void ir_emit_min_max_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) } } } else { - | cmp Rw(op1_reg), Rw(op2_reg) if (insn->op == IR_MIN) { if (IR_IS_TYPE_SIGNED(type)) { | csel Rw(def_reg), Rw(op1_reg), Rw(op2_reg), le @@ -3803,8 +3925,12 @@ static void ir_emit_op_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_emit_load(ctx, src_type, op1_reg, op1); } if (insn->op == IR_NOT) { - if (src_type == IR_BOOL) { - | ASM_REG_IMM_OP cmp, src_type, op1_reg, 0 + if (insn->type == IR_BOOL) { + if (ir_type_size[src_type] < 4) { + ir_emit_tst_narrow(ctx, src_type, op1_reg); + } else { + | ASM_REG_IMM_OP cmp, src_type, op1_reg, 0 + } | cset Rw(def_reg), eq } else { | ASM_REG_REG_OP mvn, src_type, def_reg, op1_reg @@ -3815,9 +3941,14 @@ static void ir_emit_op_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (ir_type_size[src_type] == 8) { | cmp Rx(op1_reg), #0 | cneg Rx(def_reg), Rx(op1_reg), lt - } else { + } else if (ir_type_size[src_type] == 4) { | cmp Rw(op1_reg), #0 | cneg Rw(def_reg), Rw(op1_reg), lt + } else { + /* upper bits of narrow operands are undefined */ + uint32_t sign_bit = 1U << (ir_type_size[src_type] * 8 - 1); + | tst Rw(op1_reg), #sign_bit + | cneg Rw(def_reg), Rw(op1_reg), ne } } else if (insn->op == IR_CTLZ) { if (ir_type_size[src_type] == 1) { @@ -4010,95 +4141,6 @@ static void ir_emit_fix_type(ir_ctx *ctx, ir_type type, ir_reg op1_reg) } } -static void ir_emit_cmp_int_common(ir_ctx *ctx, ir_op op, ir_type type, ir_reg op1_reg, ir_ref op1, ir_reg op2_reg, ir_ref op2) -{ - ir_backend_data *data = ctx->data; - dasm_State **Dst = &data->dasm_state; - - IR_ASSERT(op1_reg != IR_REG_NONE); - if (op2_reg == IR_REG_NONE && op1 == op2) { - op2_reg = op1_reg; - } - - if (ir_type_size[type] < 4) { - /* Short signed comparison */ - if (ir_type_size[type] == 2) { - if (op >= IR_LT && op <= IR_GT) { - | sxth Rw(IR_REG_INT_TMP), Rw(op1_reg) - } else { - | uxth Rw(IR_REG_INT_TMP), Rw(op1_reg) - } - op1_reg = IR_REG_INT_TMP; - } else if (ir_type_size[type] == 1) { - if (op >= IR_LT && op <= IR_GT) { - | sxtb Rw(IR_REG_INT_TMP), Rw(op1_reg) - } else { - | uxtb Rw(IR_REG_INT_TMP), Rw(op1_reg) - } - op1_reg = IR_REG_INT_TMP; - } else { - IR_ASSERT(0); - } - } - if (op2_reg != IR_REG_NONE) { - if (ir_type_size[type] == 8) { - | cmp Rx(op1_reg), Rx(op2_reg) - } else if (ir_type_size[type] == 4) { - | cmp Rw(op1_reg), Rw(op2_reg) - } else if (ir_type_size[type] == 2) { - if (op >= IR_LT && op <= IR_GT) { - | cmp Rw(op1_reg), Rw(op2_reg), sxth - } else { - | cmp Rw(op1_reg), Rw(op2_reg), uxth - } - } else if (ir_type_size[type] == 1) { - if (op >= IR_LT && op <= IR_GT) { - | cmp Rw(op1_reg), Rw(op2_reg), sxtb - } else { - | cmp Rw(op1_reg), Rw(op2_reg), uxtb - } - } else { - IR_ASSERT(0); - } - } else { - IR_ASSERT(IR_IS_CONST_REF(op2)); - IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[op2].op)); - int32_t val = ctx->ir_base[op2].val.i32; - - if (ir_type_size[type] == 8) { - | cmp Rx(op1_reg), #val - } else if (ir_type_size[type] == 4) { - | cmp Rw(op1_reg), #val - } else if (ir_type_size[type] == 2) { - if (op >= IR_LT && op <= IR_GT) { - val = (int32_t)(int16_t)(val & 0xffff); - if (val < 0) { - val = -val; - | cmn Rw(op1_reg), #val - } else { - | cmp Rw(op1_reg), #val - } - } else { - val = val & 0xffff; - | cmp Rw(op1_reg), #val - } - } else if (ir_type_size[type] == 1) { - if (op >= IR_LT && op <= IR_GT) { - val = (int32_t)(int8_t)(val & 0xffff); - if (val < 0) { - val = -val; - | cmn Rw(op1_reg), #val - } else { - | cmp Rw(op1_reg), #val - } - } else { - val = val & 0xff; - | cmp Rw(op1_reg), #val - } - } - } -} - static void ir_emit_cmp_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) { ir_backend_data *data = ctx->data; @@ -4323,7 +4365,15 @@ static void ir_emit_jz(ir_ctx *ctx, uint32_t b, uint32_t next_block, uint8_t op, false_block = 0; } - if (op == IR_EQ) { + if (ir_type_size[type] < 4 && type != IR_BOOL) { + ir_emit_tst_narrow(ctx, type, reg); + if (op == IR_EQ) { + | beq =>true_block + } else { + IR_ASSERT(op == IR_NE); + | bne =>true_block + } + } else if (op == IR_EQ) { if (ir_type_size[type] == 8) { | cbz Rx(reg), =>true_block } else { @@ -4580,7 +4630,11 @@ static void ir_emit_cond(ir_ctx *ctx, ir_ref def, ir_insn *insn) } if (IR_IS_TYPE_INT(op1_type)) { - | ASM_REG_IMM_OP cmp, op1_type, op1_reg, 0 + if (ir_type_size[op1_type] < 4) { + ir_emit_tst_narrow(ctx, op1_type, op1_reg); + } else { + | ASM_REG_IMM_OP cmp, op1_type, op1_reg, 0 + } } else{ | ASM_FP_REG_IMM_OP fcmp, op1_type, op1_reg, 0.0 } @@ -5229,10 +5283,6 @@ static void ir_emit_vload(ir_ctx *ctx, ir_ref def, ir_insn *insn) int32_t slot, offset; ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(ctx->ir_base[insn->op2].op == IR_VAR); fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER; slot = ir_local_slot(ctx, insn->op2); @@ -5317,10 +5367,6 @@ static void ir_emit_load_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(def_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { @@ -5357,10 +5403,6 @@ static void ir_emit_load_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(def_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { @@ -5636,10 +5678,6 @@ static void ir_emit_block_begin(ir_ctx *ctx, ir_ref def, ir_insn *insn) dasm_State **Dst = &data->dasm_state; ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } | mov Rx(def_reg), sp if (IR_REG_SPILLED(ctx->regs[def][0])) { @@ -5931,7 +5969,7 @@ static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg tmp_reg = ctx->regs[def][3]; int32_t offset; - IR_ASSERT((def_reg != IR_REG_NONE || ctx->use_lists[def].count == 1) && tmp_reg != IR_REG_NONE); + IR_ASSERT((def_reg != IR_REG_NONE || ctx->vregs[def] == 0) && tmp_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { op2_reg = IR_REG_NUM(op2_reg); @@ -5959,7 +5997,7 @@ static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg tmp_reg = ctx->regs[def][3]; int32_t offset; - IR_ASSERT((def_reg != IR_REG_NONE || ctx->use_lists[def].count == 1) && tmp_reg != IR_REG_NONE); + IR_ASSERT((def_reg != IR_REG_NONE || ctx->vregs[def] == 0) && tmp_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { op2_reg = IR_REG_NUM(op2_reg); @@ -6095,6 +6133,7 @@ static void ir_emit_switch(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn) op2_reg = IR_REG_NUM(op2_reg); ir_emit_load(ctx, type, op2_reg, insn->op2); } + ir_emit_fix_type(ctx, type, op2_reg); /* Generate a table jmp or a sequence of calls */ if (!has_case_range && count > 2 && (max.u64 - min.u64) < (uint64_t)count * 8) { @@ -6338,7 +6377,12 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i | stp x29, x30, [sp, # (-(ctx->stack_frame_size+16))]! | mov x29, sp } - | sub sp, sp, #used_stack + if (aarch64_may_encode_imm12(used_stack)) { + | sub sp, sp, #used_stack + } else { + ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, used_stack); + | sub sp, sp, Rx(IR_REG_INT_TMP) + } } } } @@ -6454,7 +6498,11 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i } } else { /* Pass register arguments to stack (REG->MEM moves) */ - if (!IR_IS_CONST_REF(arg) && src_reg != IR_REG_NONE && !IR_REG_SPILLED(src_reg)) { + if (!IR_IS_CONST_REF(arg) && src_reg != IR_REG_NONE) { + if (IR_REG_SPILLED(src_reg)) { + src_reg = IR_REG_NUM(src_reg); + ir_emit_load(ctx, type, src_reg, arg); + } ir_emit_store_mem(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), src_reg); } else { do_pass3 = 1; @@ -6541,20 +6589,12 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i type = IR_ADDR; //TODO: ??? } ir_emit_store_mem_int(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), tmp_reg); - } else if (IR_REG_SPILLED(src_reg)) { - src_reg = IR_REG_NUM(src_reg); - ir_emit_load(ctx, type, src_reg, arg); - ir_emit_store_mem_int(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), src_reg); } } else { if (src_reg == IR_REG_NONE) { IR_ASSERT(tmp_fp_reg != IR_REG_NONE); ir_emit_load(ctx, type, tmp_fp_reg, arg); ir_emit_store_mem_fp(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), tmp_fp_reg); - } else if (IR_REG_SPILLED(src_reg)) { - src_reg = IR_REG_NUM(src_reg); - ir_emit_load(ctx, type, src_reg, arg); - ir_emit_store_mem_fp(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), src_reg); } } stack_offset += IR_MAX(sizeof(void*), ir_type_size[type]); @@ -6591,7 +6631,12 @@ static void ir_emit_call_ex(ir_ctx *ctx, ir_ref def, ir_insn *insn, const ir_cal } if (used_stack) { - | add sp, sp, #used_stack + if (aarch64_may_encode_imm12(used_stack)) { + | add sp, sp, #used_stack + } else { + ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, used_stack); + | add sp, sp, Rx(IR_REG_INT_TMP) + } ctx->call_stack_size -= used_stack; } @@ -6648,19 +6693,23 @@ static void ir_emit_tailcall(ir_ctx *ctx, ir_ref def, ir_insn *insn) } /* Move op2 to a tmp register before epilogue if it's in - * used_preserved_regs, because it will be overridden. */ + * used_preserved_regs, because it will be overridden. + * The epilogue of a larger frame may use IR_REG_INT_TMP, so use IP0 then. */ ir_reg op2_reg = IR_REG_NONE; if (!IR_IS_CONST_REF(insn->op2)) { + ir_reg tmp_reg = (ctx->stack_frame_size + ctx->call_stack_size + 16 <= 0xfff) ? + IR_REG_INT_TMP : IR_REG_IP0; + op2_reg = ctx->regs[def][2]; IR_ASSERT(op2_reg != IR_REG_NONE); if (IR_REG_SPILLED(op2_reg)) { - op2_reg = IR_REG_INT_TMP; + op2_reg = tmp_reg; ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2); } else if (IR_REGSET_IN((ir_regset)ctx->used_preserved_regs, IR_REG_NUM(op2_reg))) { ir_reg orig_op2_reg = op2_reg; - op2_reg = IR_REG_INT_TMP; + op2_reg = tmp_reg; ir_type type = ctx->ir_base[insn->op2].type; | ASM_REG_REG_OP mov, type, op2_reg, IR_REG_NUM(orig_op2_reg) @@ -6784,7 +6833,14 @@ static void ir_emit_guard(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (IR_IS_CONST_REF(insn->op3)) { void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]); - if (insn->op == IR_GUARD) { + if (ir_type_size[type] < 4 && type != IR_BOOL) { + ir_emit_tst_narrow(ctx, type, op2_reg); + if (insn->op == IR_GUARD) { + | beq &addr + } else { + | bne &addr + } + } else if (insn->op == IR_GUARD) { if (ir_type_size[type] == 8) { | cbz Rx(op2_reg), &addr } else { @@ -6807,7 +6863,15 @@ static void ir_emit_guard_jz(ir_ctx *ctx, uint8_t op, void *addr, ir_type type, ir_backend_data *data = ctx->data; dasm_State **Dst = &data->dasm_state; - if (op == IR_EQ) { + if (ir_type_size[type] < 4 && type != IR_BOOL) { + ir_emit_tst_narrow(ctx, type, reg); + if (op == IR_EQ) { + | bne &addr + } else { + IR_ASSERT(op == IR_NE); + | beq &addr + } + } else if (op == IR_EQ) { if (ir_type_size[type] == 8) { | cbnz Rx(reg), &addr } else { @@ -7092,11 +7156,6 @@ static void ir_emit_tls_load(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_insn *addr_insn = &ctx->ir_base[insn->op2]; ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } - IR_ASSERT(def_reg != IR_REG_NONE && reg != IR_REG_NONE); IR_ASSERT(addr_insn->op == IR_TLS_ADDR); ir_emit_tls_base_addr(ctx, reg, addr_insn->op2); @@ -8786,7 +8845,7 @@ static void ir_emit_vector_fp2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE); if (IR_REG_SPILLED(op1_reg)) { op1_reg = IR_REG_NUM(op1_reg); - ir_emit_load(ctx, src_type, op1_reg, insn->op1); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); } if (src_type == dst_type) { if (op1_reg != def_reg) { @@ -8810,7 +8869,7 @@ static void ir_emit_vector_fp2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) | .long code if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } @@ -8840,6 +8899,11 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_FP(src_type) && IR_IS_TYPE_INT(dst_type)); IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE); + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); + } + if (src_type == IR_DOUBLE) { if (IR_IS_TYPE_SIGNED(dst_type)) { /* fcvtzs Rv(def_ref-IR_REG_FP_FIRST).2d, Rv(op1_ref-IR_REG_FP_FIRST).2d */ @@ -8913,7 +8977,7 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | .long code if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } @@ -8943,6 +9007,11 @@ static void ir_emit_vector_int2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_INT(src_type) && IR_IS_TYPE_FP(dst_type)); IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE); + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); + } + if (dst_type == IR_DOUBLE) { if (IR_IS_TYPE_SIGNED(src_type)) { if (src_size == 1) { @@ -9088,7 +9157,7 @@ static void ir_emit_vector_int2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) | .long code if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } #endif diff --git a/ext/opcache/jit/ir/ir_cfg.c b/ext/opcache/jit/ir/ir_cfg.c index 621f02a2372d..346108c65faf 100644 --- a/ext/opcache/jit/ir/ir_cfg.c +++ b/ext/opcache/jit/ir/ir_cfg.c @@ -800,15 +800,15 @@ int ir_build_dominators_tree(ir_ctx *ctx) ir_list_push(&worklist, idom); while (1) { k--; + if (UNEXPECTED(k == 0)) { + // IR_ASSERT(0 && "Wrong blocks order: BB is before all its predecessors"); + goto slow_case; + } p++; idom = *p; if (idom < b) { break; } - if (UNEXPECTED(k == 0)) { - // IR_ASSERT(0 && "Wrong blocks order: BB is before all its predecessors"); - goto slow_case; - } ir_list_push(&worklist, idom); } } @@ -1597,7 +1597,7 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) { uint32_t max_edges_count = ctx->cfg_edges_count / 2; uint32_t edges_count = 0; - uint32_t b, i, loop_depth; + uint32_t b, i, n, tail, end, loop_depth; float *bb_freq, freq; ir_block *bb; ir_edge_info *edges, *e; @@ -1858,7 +1858,7 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) qsort(edges, edges_count, sizeof(ir_edge_info), ir_edge_info_cmp); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_BB_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_BB_SCHEDULE) { ir_dump_edges(ctx, edges_count, edges); } #endif @@ -1907,7 +1907,7 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) } #if IR_DEBUG_BB_SCHEDULE_GRAPH - if (ctx->flags & IR_DEBUG_BB_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_BB_SCHEDULE) { ir_dump_cfg_freq_graph(ctx, bb_freq, edges_count, edges, chains); } #endif @@ -1915,7 +1915,7 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) ir_mem_free(bb_freq); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_BB_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_BB_SCHEDULE) { ir_dump_chains(ctx, chains); } #endif @@ -1942,19 +1942,21 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_BB_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_BB_SCHEDULE) { ir_dump_chains(ctx, chains); } #endif } /* 5. Align loop headers */ - for (b = 1; b <= ctx->cfg_blocks_count; b++) { - if (chains[b].head == b) { - bb = &ctx->cfg_blocks[b]; - if (bb->loop_depth) { - if (ir_should_align_loop(ctx, chains, b, bb)) { - bb->flags |= IR_BB_ALIGN_LOOP; + if (ctx->flags2 & IR_CFG_HAS_LOOPS) { + for (b = 1; b <= ctx->cfg_blocks_count; b++) { + if (chains[b].head == b) { + bb = &ctx->cfg_blocks[b]; + if (bb->loop_depth) { + if (ir_should_align_loop(ctx, chains, b, bb)) { + bb->flags |= IR_BB_ALIGN_LOOP; + } } } } @@ -1970,35 +1972,52 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx) uint32_t dst = ir_chain_head(chains, e->to); if (src != dst) { if (dst == 1) { - ir_join_chains(chains, dst, src); - } else { - ir_join_chains(chains, src, dst); + dst = src; + src = 1; } + ir_join_chains(chains, src, dst); } } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_BB_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_BB_SCHEDULE) { ir_dump_chains(ctx, chains); } #endif /* 7. Form a final BB order */ - count = 0; - for (b = 1; b <= ctx->cfg_blocks_count; b++) { - if (chains[b].head == b) { - uint32_t tail = chains[b].tail; - uint32_t i = b; - while (1) { - count++; - ctx->cfg_schedule[count] = i; - if (i == tail) break; - i = chains[i].next; + n = 0; + IR_ASSERT(chains[1].head == 1); + tail = chains[1].tail; + i = 1; + while (1) { + n++; + ctx->cfg_schedule[n] = i; + if (i == tail) break; + i = chains[i].next; + } + end = schedule_end - ctx->cfg_schedule; + if (n != end) { + ir_chain *chain; + + count = ctx->cfg_blocks_count; + for (b = 2, chain = &chains[2]; b <= count; chain++, b++) { + if (chain->head == b) { + tail = chain->tail; + i = b; + while (1) { + n++; + ctx->cfg_schedule[n] = i; + if (i == tail) break; + i = chains[i].next; + } + if (n == end) { + break; + } } } + IR_ASSERT(n == end); } - - IR_ASSERT(ctx->cfg_schedule + count == schedule_end); ctx->cfg_schedule[ctx->cfg_blocks_count + 1] = 0; ir_mem_free(edges); diff --git a/ext/opcache/jit/ir/ir_check.c b/ext/opcache/jit/ir/ir_check.c index fd7ba4478abe..cc0e5d55dfc1 100644 --- a/ext/opcache/jit/ir/ir_check.c +++ b/ext/opcache/jit/ir/ir_check.c @@ -147,6 +147,8 @@ bool ir_check(const ir_ctx *ctx) uint32_t flags; bool ok = 1; ir_check_ctx check_ctx; + ir_ref last_bb_start = IR_UNUSED; + ir_ref last_control = IR_UNUSED; if (ctx->insns_count < 1 || ctx->ir_base[1].op != IR_START) { fprintf(stderr, "ir_base[1].op invalid opcode (%d)\n", @@ -165,6 +167,212 @@ bool ir_check(const ir_ctx *ctx) break; } flags = ir_op_flags[insn->op]; + + if (flags & IR_OP_FLAG_DATA) { + IR_ASSERT((flags & (IR_OP_FLAG_MEM|IR_OP_FLAG_CONTROL)) == 0); + if (insn->type == IR_VOID) { + fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } else { + switch (insn->op) { + case IR_EQ: + case IR_NE: + case IR_LT: + case IR_GE: + case IR_LE: + case IR_GT: + case IR_ULT: + case IR_UGE: + case IR_ULE: + case IR_UGT: + case IR_ORDERED: + case IR_UNORDERED: + if (insn->type != IR_BOOL && !IR_IS_TYPE_VECTOR(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be bool or vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_SEXT: + case IR_ZEXT: + case IR_TRUNC: + case IR_FP2INT: + if (!IR_IS_TYPE_INT(insn->type) + && (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_INT(IR_VECTOR_BASE_TYPE(insn->type)))) { + fprintf(stderr, "ir_base[%d].type must be integer or vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_INT2FP: + case IR_FP2FP: + if (!IR_IS_TYPE_FP(insn->type) + && (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_FP(IR_VECTOR_BASE_TYPE(insn->type)))) { + fprintf(stderr, "ir_base[%d].type must be real number or vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_PROTO: + case IR_VADDR: + case IR_FRAME_ADDR: + case IR_ARGVAL: + if (insn->type != IR_ADDR) { + fprintf(stderr, "ir_base[%d].type must be uintptr_t (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_ADD_OV: + case IR_SUB_OV: + case IR_MUL_OV: + if (!IR_IS_TYPE_INT(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_OVERFLOW: + if (insn->type != IR_BOOL) { + fprintf(stderr, "ir_base[%d].type must be bool (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_NOT: + if (insn->type != IR_BOOL && !IR_IS_TYPE_INT(insn->type) && !IR_IS_TYPE_VECTOR(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be bool, integer or vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_OR: + case IR_AND: + case IR_XOR: + case IR_SHL: + case IR_SHR: + case IR_SAR: + if (!IR_IS_TYPE_INT(insn->type) + && (!IR_IS_TYPE_VECTOR(insn->type) || !IR_IS_TYPE_INT(IR_VECTOR_BASE_TYPE(insn->type)))) { + fprintf(stderr, "ir_base[%d].type must be integer or vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_ROL: + case IR_ROR: + case IR_BSWAP: + if (!IR_IS_TYPE_INT(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_CTPOP: + case IR_CTLZ: + case IR_CTTZ: + if (!IR_IS_TYPE_INT(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be integer (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } else if (ir_type_size[insn->type] > sizeof(void*)) { + fprintf(stderr, "ir_base[%d].type size must fit into a %d-bit integer (instruction %s)\n", i, (int)sizeof(void*) * 8, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_EXTRACT: + if (!IR_IS_TYPE_INT(insn->type) && !IR_IS_TYPE_FP(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be integer or real (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + case IR_REPLACE: + case IR_SPLAT: + case IR_SHUFFLE: + if (!IR_IS_TYPE_VECTOR(insn->type)) { + fprintf(stderr, "ir_base[%d].type must be vector (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + break; + default: + break; + } + } + } else if (flags & IR_OP_FLAG_MEM) { + IR_ASSERT(flags & IR_OP_FLAG_CONTROL); + if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_LOAD) { + if (insn->type == IR_VOID) { + fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + } else if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_ALLOC) { + if (insn->type == IR_VOID) { + fprintf(stderr, "ir_base[%d].type must be set (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } else if (insn->type != IR_ADDR) { + fprintf(stderr, "ir_base[%d].type must be uintptr_t (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + } else if ((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_STORE) { + if (insn->type != IR_VOID) { + fprintf(stderr, "ir_base[%d].type must be empty (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + } else { + IR_ASSERT((flags & IR_OP_FLAG_MEM_MASK) == IR_OP_FLAG_MEM_CALL); + } + } else if (flags & IR_OP_FLAG_CONTROL) { + if (insn->type != IR_VOID) { + fprintf(stderr, "ir_base[%d].type must be empty (instruction %s)\n", i, ir_op_name[insn->op]); + ok = 0; + } + } + + /* check the linear instruction order */ + if ((ctx->flags2 & IR_LINEAR) || (ctx->flags & IR_NO_OPT)) { + if (flags & IR_OP_FLAG_BB_START) { + if (last_bb_start) { + fprintf(stderr, "ir_base[%d] starts basic block before %d ends\n", i, last_bb_start); + ok = 0; + } + last_bb_start = i; + last_control = i; + } else if (flags & IR_OP_FLAG_CONTROL) { + if (!last_bb_start) { + fprintf(stderr, "ir_base[%d] instruction between basic blocks\n", i); + ok = 0; + } else if (insn->op1 != last_control) { + if ((ctx->flags & IR_NO_OPT) + && ((insn->op == IR_ALLOCA && IR_IS_CONST_REF(insn->op2)) + || (ctx->ir_base[insn->op1].op == IR_ALLOCA && IR_IS_CONST_REF(ctx->ir_base[insn->op1].op2)) + || (ctx->ir_base[last_control].op == IR_ALLOCA && IR_IS_CONST_REF(ctx->ir_base[last_control].op2)))) { + /* When compile with -O0, STATIC_ALLOCA may be anywere */ + } else { + fprintf(stderr, "ir_base[%d].op1 incorrect control order (%d insted of %d)\n", i, insn->op1, last_control); + ok = 0; + } + } else if ((ctx->flags2 & IR_LINEAR) && ctx->cfg_map[i] != ctx->cfg_map[last_bb_start]) { + fprintf(stderr, "ir_base[%d] incorrect placement/mapping (%d insted of %d)\n", i, ctx->cfg_map[i], ctx->cfg_map[last_bb_start]); + ok = 0; + } + if (flags & IR_OP_FLAG_BB_END) { + last_bb_start = IR_UNUSED; + last_control = IR_UNUSED; + } else { + last_control = i; + } + } else { + if (!last_bb_start) { + if ((ctx->flags & IR_NO_OPT) && insn->op == IR_NOP) { + /* When compile with -O0, NOP may be anywere */ + } else { + fprintf(stderr, "ir_base[%d] instruction between basic blocks\n", i); + ok = 0; + } + } else if ((flags & IR_OP_FLAG_PINNED) && insn->op1 != last_bb_start) { + if ((ctx->flags & IR_NO_OPT) && insn->op == IR_VAR) { + /* When compile with -O0, VAR may be anywere */ + } else { + fprintf(stderr, "ir_base[%d].op1 incorrect pinning (%d insted of %d)\n", i, insn->op1, last_bb_start); + ok = 0; + } + } else if ((ctx->flags2 & IR_LINEAR) && ctx->cfg_map[i] != ctx->cfg_map[last_bb_start]) { + fprintf(stderr, "ir_base[%d] incorrect placement/mapping (%d insted of %d)\n", i, ctx->cfg_map[i], ctx->cfg_map[last_bb_start]); + ok = 0; + } + } + } + n = ir_input_edges_count(ctx, insn); for (j = 1, p = insn->ops + 1; j <= n; j++, p++) { use = *p; @@ -243,7 +451,7 @@ bool ir_check(const ir_ctx *ctx) /* second argument of SHIFT may be incompatible with result */ break; } - if (insn->op == IR_NOT && insn->type == IR_BOOL) { + if (insn->op == IR_NOT && insn->type == IR_BOOL && IR_IS_TYPE_INT(use_insn->type)) { /* boolean not */ break; } diff --git a/ext/opcache/jit/ir/ir_dump.c b/ext/opcache/jit/ir/ir_dump.c index ac751328411f..8c92b04e84f1 100644 --- a/ext/opcache/jit/ir/ir_dump.c +++ b/ext/opcache/jit/ir/ir_dump.c @@ -614,7 +614,7 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f) b = _b; } bb = &ctx->cfg_blocks[b]; - if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) { + if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY|IR_BB_HAS_PHI)) == IR_BB_EMPTY) { continue; } diff --git a/ext/opcache/jit/ir/ir_emit.c b/ext/opcache/jit/ir/ir_emit.c index c53372d0ba3d..c57b7b4e599b 100644 --- a/ext/opcache/jit/ir/ir_emit.c +++ b/ext/opcache/jit/ir/ir_emit.c @@ -693,6 +693,69 @@ static int ir_parallel_copy(ir_ctx *ctx, ir_copy *copies, int count, ir_reg tmp_ return 1; } +#if IR_DEBUG +# define DESSA_STACK ((ir_ref)(0x80000000)) + +static void _print_dessa_operand(ir_ctx *ctx, ir_mem *mem_slots, ir_ref from, ir_type type) +{ + if (from == DESSA_STACK) { + fprintf(stderr, "STACK"); + } else if (from < 0) { + if (-from < ctx->consts_count) { + ir_print_const(ctx, &ctx->ir_base[from], stderr, 1); + } else { + ir_reg base = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER; + int32_t offset = ir_local_offset(ctx, -from - ctx->consts_count); + if (offset >= 0) { + fprintf(stderr, "([%%%s+0x%x])", _ir_reg_name[base], offset); + } else { + fprintf(stderr, "([%%%s-0x%x])", _ir_reg_name[base], -offset); + } + } + } else if (from < IR_REG_NUM) { +#if IR_X86_I64 + if (type == IR_U32_HI) { + type = IR_U32; + } +#endif + fprintf(stderr, "%%%s", ir_reg_name(from, type)); + } else { + ir_mem mem = mem_slots[from - IR_REG_NUM]; + if (IR_MEM_OFFSET(mem) >= 0) { + fprintf(stderr, "[%%%s+0x%x]", _ir_reg_name[IR_MEM_BASE(mem)], IR_MEM_OFFSET(mem)); + } else { + fprintf(stderr, "[%%%s-0x%x]", _ir_reg_name[IR_MEM_BASE(mem)], -IR_MEM_OFFSET(mem)); + } + } +} + +static void _print_dessa_op(ir_ctx *ctx, ir_mem *mem_slots, const char *op, ir_ref from, const char *sep, ir_ref to, ir_type type, const char *comments) +{ + fprintf(stderr, "\t%s ", op); + _print_dessa_operand(ctx, mem_slots, from, type); + fprintf(stderr, " %s ", sep); + _print_dessa_operand(ctx, mem_slots, to, type); + if (comments) { + fprintf(stderr, " ; %s", comments); + } + fprintf(stderr, "\n"); +} + +# define DESSA_LOG(op, from, sep, to, type) do { \ + if (ctx->debug_flags & IR_DEBUG_DESSA) { \ + _print_dessa_op(ctx, mem_slots, op, from, sep, to, type, NULL); \ + } \ + } while (0) +# define DESSA_LOG_EX(op, from, sep, to, type, comments) do { \ + if (ctx->debug_flags & IR_DEBUG_DESSA) { \ + _print_dessa_op(ctx, mem_slots, op, from, sep, to, type, comments); \ + } \ + } while (0) +#else +# define DESSA_LOG(op, from, sep, to, type) +# define DESSA_LOG_EX(op, from, sep, to, type, comments) +#endif + static void ir_emit_dessa_move(ir_ctx *ctx, ir_mem *mem_slots, ir_type type, ir_ref to, ir_ref from, ir_reg tmp_reg, ir_reg tmp_fp_reg) @@ -703,24 +766,33 @@ static void ir_emit_dessa_move(ir_ctx *ctx, ir_mem *mem_slots, if (to < IR_REG_NUM) { if (IR_IS_CONST_REF(from)) { if (-from < ctx->consts_count) { + /* constant reference */ #if IR_X86_I64 if (type == IR_U32_HI) { + DESSA_LOG_EX("MOVE", from, "->", to, type, "move high bits of 64-bit"); ir_emit_load_i64_hi(ctx, to, from); - } else -#endif - /* constant reference */ + } else { + DESSA_LOG("MOVE", from, "->", to, type); + ir_emit_load(ctx, type, to, from); + } +#else + DESSA_LOG("MOVE", from, "->", to, type); ir_emit_load(ctx, type, to, from); +#endif } else { /* local variable address */ + DESSA_LOG("MOVE", from, "->", to, type); ir_load_local_addr(ctx, to, -from - ctx->consts_count); } } else if (from < IR_REG_NUM) { + DESSA_LOG("MOVE", from, "->", to, type); if (IR_IS_TYPE_INT(type)) { ir_emit_mov(ctx, type, to, from); } else { ir_emit_fp_mov(ctx, type, to, from); } } else { + DESSA_LOG("MOVE", from, "->", to, type); mem_from = mem_slots[from - IR_REG_NUM]; ir_emit_load_mem(ctx, type, to, mem_from); } @@ -732,9 +804,12 @@ static void ir_emit_dessa_move(ir_ctx *ctx, ir_mem *mem_slots, #if IR_X86_I64 if (type == IR_U32_HI) { #if defined(IR_TARGET_X86) || defined(IR_TARGET_X64) + DESSA_LOG_EX("MOVE", from, "->", to, type, "move high bits of 64-bit"); ir_emit_store_mem_imm(ctx, IR_U32, mem_to, ctx->ir_base[from].val.u32_hi); #else IR_ASSERT(tmp_reg != IR_REG_NONE); + DESSA_LOG_EX("MOVE", from, "->", tmp_reg, type, "load high bits of 64-bit into TMP"); + DESSA_LOG_EX("MOVE", tmp_tmp, "->", to, type, "move high bits of 64-bit"); ir_emit_load_i64_hi(ctx, tmp_reg, from); ir_emit_store_mem(ctx, IR_U32, mem_to, tmp_reg); #endif @@ -745,28 +820,36 @@ static void ir_emit_dessa_move(ir_ctx *ctx, ir_mem *mem_slots, if (IR_IS_TYPE_INT(type) && !IR_IS_SYM_CONST(ctx->ir_base[from].op) && (ir_type_size[type] != 8 || IR_IS_SIGNED_32BIT(ctx->ir_base[from].val.i64))) { + DESSA_LOG("MOVE", from, "->", to, type); ir_emit_store_mem_imm(ctx, type, mem_to, ctx->ir_base[from].val.i32); return; } #endif ir_reg tmp = IR_IS_TYPE_INT(type) ? tmp_reg : tmp_fp_reg; IR_ASSERT(tmp != IR_REG_NONE); + DESSA_LOG_EX("MOVE", from, "->", tmp, type, "load into TMP"); + DESSA_LOG("MOVE", tmp, "->", to, type); ir_emit_load(ctx, type, tmp, from); ir_emit_store_mem(ctx, type, mem_to, tmp); } else { /* local variable address */ IR_ASSERT(IR_IS_TYPE_INT(type)); IR_ASSERT(tmp_reg != IR_REG_NONE); + DESSA_LOG_EX("MOVE", from, "->", tmp_reg, type, "load into TMP"); + DESSA_LOG("MOVE", tmp_reg, "->", to, type); ir_load_local_addr(ctx, tmp_reg, -from - ctx->consts_count); ir_emit_store_mem(ctx, type, mem_to, tmp_reg); } } else if (from < IR_REG_NUM) { + DESSA_LOG("MOVE", from, "->", to, type); ir_emit_store_mem(ctx, type, mem_to, from); } else { mem_from = mem_slots[from - IR_REG_NUM]; IR_ASSERT(IR_MEM_VAL(mem_to) != IR_MEM_VAL(mem_from)); ir_reg tmp = IR_IS_TYPE_INT(type) ? tmp_reg : tmp_fp_reg; IR_ASSERT(tmp != IR_REG_NONE); + DESSA_LOG_EX("MOVE", from, "->", tmp, type, "load into TMP"); + DESSA_LOG("MOVE", tmp, "->", to, type); ir_emit_load_mem(ctx, type, tmp, mem_from); ir_emit_store_mem(ctx, type, mem_to, tmp); } @@ -790,6 +873,18 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int IR_ASSERT(from != to); IR_ASSERT(loc[from] == from); +#if IR_DEBUG + if (ctx->debug_flags & IR_DEBUG_DESSA) { + fprintf(stderr, "\t; cycle: "); + do { + _print_dessa_operand(ctx, mem_slots, to, types[pred[to]]); + fprintf(stderr, " -> "); + to = pred[to]; + } while (to != root); + fprintf(stderr, "\n"); + } +#endif + if (IR_IS_TYPE_INT(type)) { #ifdef IR_HAVE_SWAP_INT if (pred[from] == to && to < IR_REG_NUM && from < IR_REG_NUM) { @@ -797,6 +892,7 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int if (ir_type_size[types[to]] > ir_type_size[type]) { type = types[to]; } + DESSA_LOG("SWAP", from, "<->", to, type); ir_emit_swap(ctx, type, to, from); ir_bitset_excl(todo, from); ir_bitset_excl(todo, to); @@ -808,6 +904,7 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int IR_ASSERT(tmp_reg != IR_REG_NONE); IR_ASSERT(tmp_reg >= IR_REG_GP_FIRST && tmp_reg <= IR_REG_GP_LAST); loc[to] = tmp_reg; + DESSA_LOG_EX("MOVE", to, "->", tmp_reg, type, "breake cycle, move root into TMP"); if (to < IR_REG_NUM) { ir_emit_mov(ctx, type, tmp_reg, to); } else { @@ -817,6 +914,7 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int #ifdef IR_HAVE_SWAP_FP if (pred[from] == to && to < IR_REG_NUM && from < IR_REG_NUM && types[to] == type) { /* a simple cycle from 2 elements */ + DESSA_LOG("SWAP", from, "<->", to, type); ir_emit_swap_fp(ctx, type, to, from); IR_REGSET_EXCL(todo, from); IR_REGSET_EXCL(todo, to); @@ -828,6 +926,7 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int IR_ASSERT(tmp_fp_reg != IR_REG_NONE); IR_ASSERT(tmp_fp_reg >= IR_REG_FP_FIRST && tmp_fp_reg <= IR_REG_FP_LAST); loc[to] = tmp_fp_reg; + DESSA_LOG_EX("MOVE", to, "->", tmp_fp_reg, type, "break cycle, move into TMP"); if (to < IR_REG_NUM) { ir_emit_fp_mov(ctx, type, tmp_fp_reg, to); } else { @@ -850,6 +949,7 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int if (!IR_MEM_VAL(tmp_spill_slot)) { /* Free a register, saving it in a temporary spill slot */ + DESSA_LOG_EX("MOVE", tmp, "->", DESSA_STACK, type, "free TMP"); tmp_spill_slot = IR_MEM_BO(IR_REG_STACK_POINTER, -16); ir_emit_store_mem(ctx, type, tmp_spill_slot, tmp); } @@ -864,7 +964,9 @@ IR_ALWAYS_INLINE void ir_dessa_resolve_cycle(ir_ctx *ctx, ir_mem *mem_slots, int type = types[to]; if (IR_MEM_VAL(tmp_spill_slot)) { - ir_emit_load_mem(ctx, type, IR_IS_TYPE_INT(type) ? tmp_reg : tmp_fp_reg, tmp_spill_slot); + ir_reg reg = IR_IS_TYPE_INT(type) ? tmp_reg : tmp_fp_reg; + DESSA_LOG_EX("MOVE", DESSA_STACK, "->", reg, type, "restore TMP"); + ir_emit_load_mem(ctx, type, reg, tmp_spill_slot); } ir_emit_dessa_move(ctx, mem_slots, type, to, loc[from], IR_REG_NONE, IR_REG_NONE); ir_bitset_excl(todo, to); @@ -955,7 +1057,10 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, to = pred[to]; while (!IR_IS_CONST_REF(to) && ir_bitset_in(ready, to)) { to = pred[to]; - IR_ASSERT(!IR_IS_CONST_REF(to)); + if (IR_IS_CONST_REF(to)) { + /* not a cycle: the chain ends in a constant */ + break; + } if (ir_bitset_in(visited, to)) { /* We found a cycle. Resolve it. */ ir_bitset_incl(visited, to); @@ -968,12 +1073,18 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, } /* finally we resolve remaining "windmill blades" - trees that set temporary registers */ - if (tmp_reg != IR_REG_NONE && ir_bitset_in(todo, tmp_reg)) { + if (tmp_fp_reg != IR_REG_NONE && ir_bitset_in(todo, tmp_fp_reg)) { ir_mem tmp_spill_slot; ir_type tmp_type; +#if IR_DEBUG + if (ctx->debug_flags & IR_DEBUG_DESSA) { + fprintf(stderr, "\t; final tmp_fp_reg blade:\n"); + } +#endif + ir_bitset_clear(ready, len); - to = tmp_reg; + to = tmp_fp_reg; tmp_type = types[to]; IR_MEM_VAL(tmp_spill_slot) = 0; do { @@ -981,12 +1092,13 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, type = types[to]; from = pred[to]; if (IR_IS_CONST_REF(from)) { + /* Immediate to memory move inside a "blocked" blade requres an additional temporary register */ if (to >= IR_REG_NUM) { - /* Immediate to memory move inside a "blocked" blade requres an additional temporary register */ if (!IR_MEM_VAL(tmp_spill_slot)) { /* Free a register, saving it in a temporary spill slot */ + DESSA_LOG_EX("MOVE", tmp_fp_reg, "->", DESSA_STACK, tmp_type, "free TMP"); tmp_spill_slot = IR_MEM_BO(IR_REG_STACK_POINTER, -16); - ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_reg); + ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_fp_reg); } } ir_emit_dessa_move(ctx, mem_slots, type, to, from, tmp_reg, tmp_fp_reg); @@ -997,8 +1109,9 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, if (to >= IR_REG_NUM && r >= IR_REG_NUM) { if (!IR_MEM_VAL(tmp_spill_slot)) { /* Free a register, saving it in a temporary spill slot */ + DESSA_LOG_EX("MOVE", tmp_fp_reg, "->", DESSA_STACK, tmp_type, "frss TMP"); tmp_spill_slot = IR_MEM_BO(IR_REG_STACK_POINTER, -16); - ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_reg); + ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_fp_reg); } } ir_emit_dessa_move(ctx, mem_slots, type, to, r, tmp_reg, tmp_fp_reg); @@ -1010,16 +1123,23 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, } while ((to = ir_bitset_pop_first(ready, len)) >= 0); if (IR_MEM_VAL(tmp_spill_slot)) { - ir_emit_load_mem(ctx, tmp_type, tmp_reg, tmp_spill_slot); + DESSA_LOG_EX("MOVE", DESSA_STACK, "->", tmp_fp_reg, tmp_type, "restore TMP"); + ir_emit_load_mem(ctx, tmp_type, tmp_fp_reg, tmp_spill_slot); } } - if (tmp_fp_reg != IR_REG_NONE && ir_bitset_in(todo, tmp_fp_reg)) { + if (tmp_reg != IR_REG_NONE && ir_bitset_in(todo, tmp_reg)) { ir_mem tmp_spill_slot; ir_type tmp_type; +#if IR_DEBUG + if (ctx->debug_flags & IR_DEBUG_DESSA) { + fprintf(stderr, "\t; final tmp_reg blade:\n"); + } +#endif + ir_bitset_clear(ready, len); - to = tmp_fp_reg; + to = tmp_reg; tmp_type = types[to]; IR_MEM_VAL(tmp_spill_slot) = 0; do { @@ -1027,12 +1147,13 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, type = types[to]; from = pred[to]; if (IR_IS_CONST_REF(from)) { - /* Immediate to memory move inside a "blocked" blade requres an additional temporary register */ if (to >= IR_REG_NUM) { + /* Immediate to memory move inside a "blocked" blade requres an additional temporary register */ if (!IR_MEM_VAL(tmp_spill_slot)) { /* Free a register, saving it in a temporary spill slot */ + DESSA_LOG_EX("MOVE", tmp_reg, "->", DESSA_STACK, tmp_type, "free TMP"); tmp_spill_slot = IR_MEM_BO(IR_REG_STACK_POINTER, -16); - ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_fp_reg); + ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_reg); } } ir_emit_dessa_move(ctx, mem_slots, type, to, from, tmp_reg, tmp_fp_reg); @@ -1043,8 +1164,9 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, if (to >= IR_REG_NUM && r >= IR_REG_NUM) { if (!IR_MEM_VAL(tmp_spill_slot)) { /* Free a register, saving it in a temporary spill slot */ + DESSA_LOG_EX("MOVE", tmp_reg, "->", DESSA_STACK, tmp_type, "free TMP"); tmp_spill_slot = IR_MEM_BO(IR_REG_STACK_POINTER, -16); - ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_fp_reg); + ir_emit_store_mem(ctx, tmp_type, tmp_spill_slot, tmp_reg); } } ir_emit_dessa_move(ctx, mem_slots, type, to, r, tmp_reg, tmp_fp_reg); @@ -1056,7 +1178,8 @@ static int ir_dessa_parallel_copy(ir_ctx *ctx, ir_dessa_copy *copies, int count, } while ((to = ir_bitset_pop_first(ready, len)) >= 0); if (IR_MEM_VAL(tmp_spill_slot)) { - ir_emit_load_mem(ctx, tmp_type, tmp_fp_reg, tmp_spill_slot); + DESSA_LOG_EX("MOVE", DESSA_STACK, "->", tmp_reg, tmp_type, "restore TMP"); + ir_emit_load_mem(ctx, tmp_type, tmp_reg, tmp_spill_slot); } } @@ -1232,7 +1355,32 @@ static void ir_emit_dessa_moves(ir_ctx *ctx, int b, ir_block *bb) } if (n > 0) { +#ifdef IR_DEBUG + if (ctx->debug_flags & IR_DEBUG_DESSA) { + uint32_t i; + + fprintf(stderr, "DESSA: BB%d -> BB%d (copy_count=%d", b, succ, n); + if (tmp_reg != IR_REG_NONE) { + fprintf(stderr, " tmp_reg=%%%s", _ir_reg_name[tmp_reg]); + } + if (tmp_fp_reg != IR_REG_NONE) { + fprintf(stderr, " tmp_fp_reg=%%%s", _ir_reg_name[tmp_fp_reg]); + } + fprintf(stderr, ") {\n"); + for (i = 0; i < n; i++) { + _print_dessa_op(ctx, mem_slots, "COPY", copies[i].from, "=>", copies[i].to, copies[i].type, NULL); + } + fprintf(stderr, "====\n"); + } +#endif + ir_dessa_parallel_copy(ctx, copies, n, mem_slots, mem_slots_count, tmp_reg, tmp_fp_reg); + +#ifdef IR_DEBUG + if (ctx->debug_flags & IR_DEBUG_DESSA) { + fprintf(stderr, "}\n"); + } +#endif } } @@ -1532,8 +1680,10 @@ int ir_match(ir_ctx *ctx) ctx->rules[ref] = insn->op; } ref = prev_ref[ref]; - if (ref == start && ctx->cfg_edges[bb->successors] != b) { - if (EXPECTED(!(bb->flags & IR_BB_ENTRY))) { + if (ref == start) { + if (ctx->cfg_edges[bb->successors] == b) { + /* pass */ + } else if (EXPECTED(!(bb->flags & IR_BB_ENTRY))) { bb->flags |= IR_BB_EMPTY; } else if (ctx->flags & IR_MERGE_EMPTY_ENTRIES) { bb->flags |= IR_BB_EMPTY; @@ -1542,6 +1692,9 @@ int ir_match(ir_ctx *ctx) } } continue; + } else if ((bb->flags & IR_BB_HAS_PHI) && ctx->ir_base[ref].op == IR_PHI) { + IR_ASSERT(ctx->cfg_edges[bb->successors] != b); + bb->flags |= IR_BB_EMPTY; } } } @@ -1559,6 +1712,20 @@ int ir_match(ir_ctx *ctx) } } + if (ctx->flags & IR_FUNCTION) { + /* Set rules for static "unreachable" ALLOCAs (they are in the control chain followed the START) */ + ref = ir_next_control(ctx, 1); + while (ctx->ir_base[ref].op == IR_ALLOCA && IR_IS_CONST_REF(ctx->ir_base[ref].op2)) { + if (!ctx->rules[ref]) { + IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[ctx->ir_base[ref].op2].op)); + ctx->rules[ref] = IR_STATIC_ALLOCA; + ctx->flags |= IR_USE_FRAME_POINTER; + ctx->flags2 |= IR_HAS_ALLOCA; + } + ref = ir_next_control(ctx, ref); + } + } + if (ctx->entries_count) { ctx->entries_count = entries_count; if (!entries_count) { diff --git a/ext/opcache/jit/ir/ir_fold.h b/ext/opcache/jit/ir/ir_fold.h index 962668b9bc6c..f350e60edeb7 100644 --- a/ext/opcache/jit/ir/ir_fold.h +++ b/ext/opcache/jit/ir/ir_fold.h @@ -1013,6 +1013,12 @@ IR_FOLD(OVERFLOW(_)) if (op1_insn->op != IR_ADD_OV && op1_insn->op != IR_SUB_OV && op1_insn->op != IR_MUL_OV) { IR_FOLD_COPY(IR_FALSE); } + if (IR_IS_CONST_REF(op1_insn->op2) + && !IR_IS_SYM_CONST(ctx->ir_base[op1_insn->op2].op) + && ctx->ir_base[op1_insn->op2].val.u64 == 0) { + /* x +/- 0 and x * 0 never overflow */ + IR_FOLD_COPY(IR_FALSE); + } IR_FOLD_NEXT; } @@ -1025,36 +1031,54 @@ IR_FOLD(NOT(C_BOOL)) IR_FOLD(NOT(C_U8)) IR_FOLD(NOT(C_CHAR)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.u8); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_U(~op1_insn->val.u8); } IR_FOLD(NOT(C_I8)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.i8); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_I(~op1_insn->val.i8); } IR_FOLD(NOT(C_U16)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.u16); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_U(~op1_insn->val.u16); } IR_FOLD(NOT(C_I16)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.i16); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_I(~op1_insn->val.i16); } IR_FOLD(NOT(C_U32)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.u32); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_U(~op1_insn->val.u32); } IR_FOLD(NOT(C_I32)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.i32); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_I(~op1_insn->val.i32); } @@ -1062,10 +1086,21 @@ IR_FOLD(NOT(C_I32)) IR_FOLD(NOT(C_U64)) IR_FOLD(NOT(C_I64)) { + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.u64); + } IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); IR_FOLD_CONST_U(~op1_insn->val.u64); } +IR_FOLD(NOT(C_ADDR)) +{ + if (IR_OPT_TYPE(opt) == IR_BOOL) { + IR_FOLD_BOOL(!op1_insn->val.u64); + } + IR_FOLD_NEXT; +} + IR_FOLD(OR(C_BOOL, C_BOOL)) { IR_ASSERT(IR_OPT_TYPE(opt) == op1_insn->type); @@ -2038,13 +2073,31 @@ IR_FOLD(ABS(NEG)) } IR_FOLD(NEG(NEG)) -IR_FOLD(NOT(NOT)) IR_FOLD(BSWAP(BSWAP)) { /* f(f(y)) => y */ IR_FOLD_COPY(op1_insn->op1); } +IR_FOLD(NOT(NOT)) +{ + ir_type type = ctx->ir_base[op1_insn->op1].type; + + if (type == IR_OPT_TYPE(opt)) { + IR_FOLD_COPY(op1_insn->op1); + } else if (IR_OPT_TYPE(opt) == IR_BOOL && op1_insn->type == IR_BOOL && IR_IS_TYPE_INT(type)) { + /* !!y => y != 0 */ + ir_val val; + + val.u64 = 0; + opt = IR_OPT(IR_NE, IR_BOOL); + op1 = op1_insn->op1; + op2 = ir_const(ctx, val, type); + IR_FOLD_RESTART; + } + IR_FOLD_NEXT; +} + IR_FOLD(EQ(_, C_BOOL)) { if (op2 == IR_TRUE) { @@ -2225,22 +2278,25 @@ IR_FOLD(GT(SEXT, C_ADDR)) IR_FOLD_COPY(op1_insn->op1); } } - if ((op2_insn->val.u64 >> (ir_type_size[type] * 8)) != 0 - && (op1_insn->op != IR_SEXT || (op2_insn->val.i64 >> (ir_type_size[type] * 8)) != -1)) { - if ((opt & IR_OPT_OP_MASK) == IR_EQ - || (opt & IR_OPT_OP_MASK) == IR_UGT - || (opt & IR_OPT_OP_MASK) == IR_UGE) { - IR_FOLD_COPY(IR_FALSE); - } else if ((opt & IR_OPT_OP_MASK) == IR_NE - || (opt & IR_OPT_OP_MASK) == IR_ULT - || (opt & IR_OPT_OP_MASK) == IR_ULE) { - IR_FOLD_COPY(IR_TRUE); - } else if ((opt & IR_OPT_OP_MASK) == IR_GT || (opt & IR_OPT_OP_MASK) == IR_GE) { - IR_FOLD_COPY(op2_insn->val.i64 >= 0 ? IR_FALSE : IR_TRUE); - } else if ((opt & IR_OPT_OP_MASK) == IR_LT || (opt & IR_OPT_OP_MASK) == IR_LE) { - IR_FOLD_COPY(op2_insn->val.i64 >= 0 ? IR_TRUE : IR_FALSE); + if ((op2_insn->val.u64 >> (ir_type_size[type] * 8 - (op1_insn->op == IR_SEXT))) != 0) { + int64_t sval = ir_fix_signed(op2_insn->type, op2_insn->val.u64); + + if (op1_insn->op != IR_SEXT || (sval >> (ir_type_size[type] * 8 - 1)) != -1) { + if ((opt & IR_OPT_OP_MASK) == IR_EQ + || (opt & IR_OPT_OP_MASK) == IR_UGT + || (opt & IR_OPT_OP_MASK) == IR_UGE) { + IR_FOLD_COPY(IR_FALSE); + } else if ((opt & IR_OPT_OP_MASK) == IR_NE + || (opt & IR_OPT_OP_MASK) == IR_ULT + || (opt & IR_OPT_OP_MASK) == IR_ULE) { + IR_FOLD_COPY(IR_TRUE); + } else if ((opt & IR_OPT_OP_MASK) == IR_GT || (opt & IR_OPT_OP_MASK) == IR_GE) { + IR_FOLD_COPY(sval >= 0 ? IR_FALSE : IR_TRUE); + } else if ((opt & IR_OPT_OP_MASK) == IR_LT || (opt & IR_OPT_OP_MASK) == IR_LE) { + IR_FOLD_COPY(sval >= 0 ? IR_TRUE : IR_FALSE); + } + IR_FOLD_NEXT; } - IR_FOLD_NEXT; } if (IR_IS_TYPE_SIGNED(type)) { switch (ir_type_size[type]) { @@ -2351,6 +2407,33 @@ IR_FOLD(NE(SUB, C_ADDR)) IR_FOLD_NEXT; } +IR_FOLD(SUB(CTLZ, C_U8)) +IR_FOLD(SUB(CTLZ, C_U16)) +IR_FOLD(SUB(CTLZ, C_U32)) +IR_FOLD(SUB(CTLZ, C_U64)) +IR_FOLD(SUB(CTLZ, C_I8)) +IR_FOLD(SUB(CTLZ, C_I16)) +IR_FOLD(SUB(CTLZ, C_I32)) +IR_FOLD(SUB(CTLZ, C_I64)) +{ + const ir_insn *zext_insn = &ctx->ir_base[op1_insn->op1]; + + if (zext_insn->op == IR_ZEXT) { + ir_ref x = zext_insn->op1; + ir_type src_type = ctx->ir_base[x].type; + + if (src_type != IR_BOOL + && op2_insn->val.u64 == (uint64_t)(ir_type_size[zext_insn->type] - ir_type_size[src_type]) * 8) { + /* CTLZ result type may differ from its operand type */ + opt = IR_OPT(IR_CTLZ, IR_OPT_TYPE(opt)); + op1 = x; + op2 = IR_UNUSED; + IR_FOLD_RESTART; + } + } + IR_FOLD_NEXT; +} + IR_FOLD(ADD(_, C_U8)) IR_FOLD(ADD(_, C_U16)) IR_FOLD(ADD(_, C_U32)) @@ -2390,6 +2473,10 @@ IR_FOLD(SUB_OV(_, C_ADDR)) { if (op2_insn->val.u64 == 0) { /* a +/- 0 => a */ + if (op1_insn->op >= IR_ADD_OV && op1_insn->op <= IR_MUL_OV) { + /* OVERFLOW() of the result would test op1's overflow flag */ + IR_FOLD_NEXT; + } if (op1_insn->type != IR_OPT_TYPE(opt)) { opt = IR_BITCAST | (opt & IR_OPT_TYPE_MASK); op2 = IR_UNUSED; @@ -2672,7 +2759,7 @@ IR_FOLD(MUL(NEG, C_I32)) IR_FOLD(MUL(NEG, C_I64)) { op1 = op1_insn->op1; - val.u64 = (0 - op2_insn->val.u64); + val.u64 = ir_fix_signed(IR_OPT_TYPE(opt), 0 - op2_insn->val.u64); op2 = ir_const(ctx, val, op2_insn->type); IR_FOLD_RESTART; } diff --git a/ext/opcache/jit/ir/ir_gcm.c b/ext/opcache/jit/ir/ir_gcm.c index 54c894f27cba..37d6f83f57d2 100644 --- a/ext/opcache/jit/ir/ir_gcm.c +++ b/ext/opcache/jit/ir/ir_gcm.c @@ -26,8 +26,31 @@ static uint32_t ir_gcm_schedule_early(ir_ctx *ctx, ir_ref ref, ir_list *queue_la insn = &ctx->ir_base[ref]; - IR_ASSERT(insn->op != IR_PARAM && insn->op != IR_VAR); - IR_ASSERT(insn->op != IR_PHI && insn->op != IR_PI); + IR_ASSERT(insn->op != IR_PARAM); + if (ir_op_flags[insn->op] & IR_OP_FLAG_PINNED) { + /* Pinned instructions are handled especially */ + b = ctx->cfg_map[ctx->ir_base[ref].op1]; + IR_ASSERT(b && !IR_GCM_IS_SCHEDULED_EARLY(b)); + ctx->cfg_map[ref] = b; + + if (insn->op == IR_PHI) { + ctx->cfg_blocks[b].flags |= IR_BB_HAS_PHI; + n = insn->inputs_count - 1; + for (p = insn->ops + 2; n > 0; p++, n--) { + ref = *p; + if (ref > 0 && ctx->cfg_map[ref] == 0) { + ir_gcm_schedule_early(ctx, ref, queue_late); + } + } + } else if (insn->op == IR_PI) { + ctx->cfg_blocks[b].flags |= IR_BB_HAS_PI; + ir_gcm_schedule_early(ctx, insn->op2, queue_late); + } else { + IR_ASSERT(insn->op == IR_VAR); + ctx->cfg_blocks[b].flags |= IR_BB_HAS_VAR; + } + return b; + } result = 1; dom_depth = 0; @@ -246,7 +269,7 @@ static bool ir_split_partially_dead_node(ir_ctx *ctx, ir_ref ref, uint32_t b) } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM_SPLIT) { + if (ctx->debug_flags & IR_DEBUG_GCM_SPLIT) { bool first = 1; fprintf(stderr, "*** Split partially dead node d_%d scheduled to BB%d\n", ref, b); IR_SPARSE_SET_FOREACH(&data->totally_useful, i) { @@ -288,7 +311,7 @@ static bool ir_split_partially_dead_node(ir_ctx *ctx, ir_ref ref, uint32_t b) IR_ASSERT(!ir_sparse_set_in(&data->totally_useful, b)); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM_SPLIT) { + if (ctx->debug_flags & IR_DEBUG_GCM_SPLIT) { bool first = 1; IR_SPARSE_SET_FOREACH(&data->totally_useful, i) { if (first) { @@ -426,7 +449,7 @@ static bool ir_split_partially_dead_node(ir_ctx *ctx, ir_ref ref, uint32_t b) // (similar to SSAPRE) #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM_SPLIT) { + if (ctx->debug_flags & IR_DEBUG_GCM_SPLIT) { for (i = 0; i < clones_count; i++) { uint32_t u = clones[i].use; @@ -518,7 +541,7 @@ static bool ir_split_partially_dead_node(ir_ctx *ctx, ir_ref ref, uint32_t b) ir_hashtab_free(&hash); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM_SPLIT) { + if (ctx->debug_flags & IR_DEBUG_GCM_SPLIT) { ir_check(ctx); } #endif @@ -701,24 +724,25 @@ int ir_gcm(ir_ctx *ctx) /* process the first instruction of the block */ _blocks[ref] = b; /* pin to block */ - use_list = &ctx->use_lists[ref]; + /* Disparate from the original C.Click algorithm described in "Global code motion, global value numbering", + * PHI instructions are not treated as "pinned" at this point (the same for PI and VAR). + * These instruction are going to be "pinned" by special handling in ir_gcm_schedule_early(). + * As result GCM removes "dead" cycles (and arbitrary "dead" SCCs) created through PHIs. + */ + } + + if (ctx->flags & IR_FUNCTION) { + /* pin function parameters to the first block */ + use_list = &ctx->use_lists[1]; n = use_list->count; if (n > 1) { + bb = &ctx->cfg_blocks[1]; for (p = &ctx->use_edges[use_list->refs]; n > 0; n--, p++) { ref = *p; use_insn = &ctx->ir_base[ref]; - if (use_insn->op == IR_PHI || use_insn->op == IR_PI) { - bb->flags |= (use_insn->op == IR_PHI) ? IR_BB_HAS_PHI : IR_BB_HAS_PI; - if (EXPECTED(ctx->use_lists[ref].count != 0)) { - _blocks[ref] = b; /* pin to block */ - ir_list_push_unchecked(&queue_early, ref); - } - } else if (use_insn->op == IR_PARAM) { + if (use_insn->op == IR_PARAM) { bb->flags |= IR_BB_HAS_PARAM; - _blocks[ref] = b; /* pin to block */ - } else if (use_insn->op == IR_VAR) { - bb->flags |= IR_BB_HAS_VAR; - _blocks[ref] = b; /* pin to block */ + _blocks[ref] = 1; /* pin to block */ } } } @@ -739,7 +763,7 @@ int ir_gcm(ir_ctx *ctx) } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM) { + if (ctx->debug_flags & IR_DEBUG_GCM) { fprintf(stderr, "GCM Schedule Early\n"); for (n = 1; n < ctx->insns_count; n++) { fprintf(stderr, "%d -> %d\n", n, ctx->cfg_map[n]); @@ -775,7 +799,7 @@ int ir_gcm(ir_ctx *ctx) ir_list_free(&queue_late); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_GCM) { + if (ctx->debug_flags & IR_DEBUG_GCM) { fprintf(stderr, "GCM Schedule Late\n"); for (n = 1; n < ctx->insns_count; n++) { fprintf(stderr, "%d -> %d\n", n, ctx->cfg_map[n]); @@ -1085,7 +1109,7 @@ static void ir_schedule_topsort(const ir_ctx *ctx, uint32_t b, const ir_block *b if (ctx->cfg_map[input] == b) { /* "input" should be before "i" to satisfy dependency */ #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_SCHEDULE) { fprintf(stderr, "Wrong dependency %d:%d -> %d\n", b, input, i); } #endif @@ -1186,7 +1210,7 @@ int ir_schedule(ir_ctx *ctx) ir_ref start; #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_SCHEDULE) { fprintf(stderr, "BB%d\n", b); ir_schedule_print_list(ctx, b, _next, bb->start, bb->end, "INITIAL"); } @@ -1298,7 +1322,7 @@ int ir_schedule(ir_ctx *ctx) } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_SCHEDULE) { + if (ctx->debug_flags & IR_DEBUG_SCHEDULE) { ir_schedule_print_list(ctx, b, _next, start, bb->end, " FINAL"); } #endif diff --git a/ext/opcache/jit/ir/ir_private.h b/ext/opcache/jit/ir/ir_private.h index 5bca99a50c9d..ba24c26d4f5e 100644 --- a/ext/opcache/jit/ir/ir_private.h +++ b/ext/opcache/jit/ir/ir_private.h @@ -1183,6 +1183,14 @@ void ir_iter_replace(ir_ctx *ctx, ir_ref ref, ir_ref new_ref, ir_bitqueue *workl void ir_iter_opt(ir_ctx *ctx, ir_bitqueue *worklist); void ir_iter_cleanup(ir_ctx *ctx); +IR_ALWAYS_INLINE bool ir_is_dead_load(const ir_ctx *ctx, ir_ref ref) +{ + if (ctx->use_lists[ref].count == 1) { + return (ir_op_flags[ctx->ir_base[ref].op] & IR_OP_FLAG_NO_SIDE_EFFECT) != 0; + } + return 0; +} + /*** IR Basic Blocks info ***/ #define IR_IS_BB_START(op) \ ((ir_op_flags[op] & IR_OP_FLAG_BB_START) != 0) @@ -1319,6 +1327,8 @@ typedef struct _ir_use_pos ir_use_pos; #define IR_FUSED_USE (1<<6) #define IR_PHI_USE (1<<7) +#define IR_CALL_WITH_ARGS (1<<8) + #define IR_OP1_MUST_BE_IN_REG (1<<16) #define IR_OP1_SHOULD_BE_IN_REG (1<<17) #define IR_OP1_HINT_TWO_REGS (1<<18) diff --git a/ext/opcache/jit/ir/ir_ra.c b/ext/opcache/jit/ir/ir_ra.c index 886413ecf846..8295e3b5cc4f 100644 --- a/ext/opcache/jit/ir/ir_ra.c +++ b/ext/opcache/jit/ir/ir_ra.c @@ -62,7 +62,7 @@ static int ir_assign_virtual_registers_slow(ir_ctx *ctx) insn += n; while (i < bb->end) { flags = ir_op_flags[insn->op]; - if (((flags & IR_OP_FLAG_DATA) && insn->op != IR_VAR && (insn->op != IR_PARAM || ctx->use_lists[i].count > 0)) + if (((flags & IR_OP_FLAG_DATA) && insn->op != IR_VAR && ((insn->op != IR_PARAM && insn->op != IR_PHI) || ctx->use_lists[i].count > 0)) || ((flags & IR_OP_FLAG_MEM) && ctx->use_lists[i].count > 1)) { if (!ctx->rules || !(ctx->rules[i] & (IR_FUSED|IR_SKIPPED|IR_NO_REG))) { vregs[i] = ++vregs_count; @@ -602,9 +602,7 @@ int ir_compute_live_ranges(ir_ctx *ctx) ir_bitqueue queue; ir_live_interval *ival; - if (!(ctx->flags2 & IR_LINEAR) || !ctx->vregs) { - return 0; - } + IR_ASSERT((ctx->flags2 & IR_LINEAR) && ctx->vregs); if (ctx->rules) { ctx->regs = ir_mem_malloc(sizeof(ir_regs) * ctx->insns_count); @@ -727,16 +725,6 @@ int ir_compute_live_ranges(ir_ctx *ctx) if (ctx->rules) { int n; -#if IR_X86_I64 - if (ctx->rules[ref] & IR_TWO_REGS) { - v = ctx->vregs[ref]; - if (v > 0) { - IR_ASSERT(ctx->live_intervals[v]); - ctx->live_intervals[v]->flags |= IR_LIVE_INTERVAL_TWO_REGS; - } - } -#endif - if (ctx->rules[ref] & (IR_FUSED|IR_SKIPPED)) { if (((ctx->rules[ref] & IR_RULE_MASK) == IR_ALLOCA) && ctx->use_lists[ref].count > 0) { @@ -792,11 +780,15 @@ int ir_compute_live_ranges(ir_ctx *ctx) if (!IR_IS_CONST_REF(insn->op1) && ctx->vregs[insn->op1] > 0) { hint_ref = insn->op1; } - def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); + if (def_flags & IR_DEF_CONFLICTS_WITH_INPUT_REGS) { + def_pos = IR_USE_LIVE_POS_FROM_REF(ref); + } else { + def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); + } } else if (def_flags & IR_DEF_CONFLICTS_WITH_INPUT_REGS) { def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } else { - if (insn->op == IR_PARAM) { + if (insn->op == IR_PARAM && ctx->live_intervals[v]) { /* We may reuse parameter stack slot for spilling */ ir_live_interval *param_ival = ctx->live_intervals[v]; @@ -825,11 +817,34 @@ int ir_compute_live_ranges(ir_ctx *ctx) } /* live.remove(opd) */ ir_bitset_excl(live, v); - /* intervals[opd].setFrom(op.id) */ - ival = ir_fix_live_range(ctx, v, - IR_START_LIVE_POS_FROM_REF(bb->start), def_pos); - ival->type = insn->type; - ir_add_use(ctx, ival, 0, def_pos, reg, def_flags, hint_ref); + ival = ctx->live_intervals[v]; + if (!ival) { + if (ctx->rules) { + if (IR_IS_FOLDABLE_OP(insn->op) || (ir_op_flags[insn->op] & IR_OP_FLAG_NO_SIDE_EFFECT)) { + /* lightweigh dead code elimination */ + ctx->rules[ref] = IR_SKIPPED | IR_NOP; + ctx->vregs[ref] = 0; + continue; + } else if (insn->op == IR_PARAM) { + /* unused PARAM */ + ctx->rules[ref] = IR_SKIPPED | IR_PARAM; + ctx->vregs[ref] = 0; + continue; + } + } + ctx->vregs[ref] = 0; + } else { + /* intervals[opd].setFrom(op.id) */ + ival = ir_fix_live_range(ctx, v, + IR_START_LIVE_POS_FROM_REF(bb->start), def_pos); +#if IR_X86_I64 + if (insn->type == IR_I64 || insn->type == IR_U64) { + ival->flags |= IR_LIVE_INTERVAL_TWO_REGS; + } +#endif + ival->type = insn->type; + ir_add_use(ctx, ival, 0, def_pos, reg, def_flags, hint_ref); + } } else { /* live.remove(opd) */ ir_bitset_excl(live, v); @@ -839,6 +854,11 @@ int ir_compute_live_ranges(ir_ctx *ctx) /* Dead PHI */ ival = ir_add_live_range(ctx, v, IR_START_LIVE_POS_FROM_REF(bb->start), IR_SAVE_LIVE_POS_FROM_REF(ref)); } +#if IR_X86_I64 + if (insn->type == IR_I64 || insn->type == IR_U64) { + ival->flags |= IR_LIVE_INTERVAL_TWO_REGS; + } +#endif ival->type = insn->type; ir_add_use(ctx, ival, 0, IR_DEF_LIVE_POS_FROM_REF(ref), IR_REG_NONE, IR_USE_SHOULD_BE_IN_REG, 0); continue; @@ -915,13 +935,19 @@ int ir_compute_live_ranges(ir_ctx *ctx) } } else if (def_flags & IR_DEF_REUSES_OP1_REG) { if (j == 1) { - use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); + if (def_flags & IR_DEF_CONFLICTS_WITH_INPUT_REGS) { + use_pos = IR_USE_LIVE_POS_FROM_REF(ref); + } else { + use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); + } IR_ASSERT(ctx->vregs[ref] > 0); hint_ref = ref; } else if (input == insn->op1) { /* Input is the same as "op1" */ use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } + } else if ((def_flags & IR_CALL_WITH_ARGS) && j > 2) { + use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } if (!ir_bitset_in(live, v)) { /* live.add(opd) */ @@ -1444,16 +1470,6 @@ int ir_compute_live_ranges(ir_ctx *ctx) if (ctx->rules) { int n; -#if IR_X86_I64 - if (ctx->rules[ref] & IR_TWO_REGS) { - v = ctx->vregs[ref]; - if (v > 0) { - IR_ASSERT(ctx->live_intervals[v]); - ctx->live_intervals[v]->flags |= IR_LIVE_INTERVAL_TWO_REGS; - } - } -#endif - if (ctx->rules[ref] & (IR_FUSED|IR_SKIPPED)) { if (((ctx->rules[ref] & IR_RULE_MASK) == IR_ALLOCA) && ctx->use_lists[ref].count > 0) { @@ -1517,7 +1533,7 @@ int ir_compute_live_ranges(ir_ctx *ctx) } else if (def_flags & IR_DEF_CONFLICTS_WITH_INPUT_REGS) { def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } else { - if (insn->op == IR_PARAM) { + if (insn->op == IR_PARAM && ctx->live_intervals[v]) { /* We may reuse parameter stack slot for spilling */ ir_live_interval *param_ival = ctx->live_intervals[v]; @@ -1544,11 +1560,34 @@ int ir_compute_live_ranges(ir_ctx *ctx) } def_pos = IR_DEF_LIVE_POS_FROM_REF(ref); } - /* intervals[opd].setFrom(op.id) */ - ival = ir_fix_live_range(ctx, v, - IR_START_LIVE_POS_FROM_REF(bb->start), def_pos); - ival->type = insn->type; - ir_add_use(ctx, ival, 0, def_pos, reg, def_flags, hint_ref); + ival = ctx->live_intervals[v]; + if (!ival) { + if (ctx->rules) { + if (IR_IS_FOLDABLE_OP(insn->op) || (ir_op_flags[insn->op] & IR_OP_FLAG_NO_SIDE_EFFECT)) { + /* lightweigh dead code elimination */ + ctx->rules[ref] = IR_SKIPPED | IR_NOP; + ctx->vregs[ref] = 0; + continue; + } else if (insn->op == IR_PARAM) { + /* unused PARAM */ + ctx->rules[ref] = IR_SKIPPED | IR_PARAM; + ctx->vregs[ref] = 0; + continue; + } + } + ctx->vregs[ref] = 0; + } else { + /* intervals[opd].setFrom(op.id) */ + ival = ir_fix_live_range(ctx, v, + IR_START_LIVE_POS_FROM_REF(bb->start), def_pos); +#if IR_X86_I64 + if (insn->type == IR_I64 || insn->type == IR_U64) { + ival->flags |= IR_LIVE_INTERVAL_TWO_REGS; + } +#endif + ival->type = insn->type; + ir_add_use(ctx, ival, 0, def_pos, reg, def_flags, hint_ref); + } } else { /* PHIs inputs must not be processed */ ival = ctx->live_intervals[v]; @@ -1556,6 +1595,11 @@ int ir_compute_live_ranges(ir_ctx *ctx) /* Dead PHI */ ival = ir_add_live_range(ctx, v, IR_START_LIVE_POS_FROM_REF(bb->start), IR_SAVE_LIVE_POS_FROM_REF(ref)); } +#if IR_X86_I64 + if (insn->type == IR_I64 || insn->type == IR_U64) { + ival->flags |= IR_LIVE_INTERVAL_TWO_REGS; + } +#endif ival->type = insn->type; ir_add_use(ctx, ival, 0, IR_DEF_LIVE_POS_FROM_REF(ref), IR_REG_NONE, IR_USE_SHOULD_BE_IN_REG, 0); continue; @@ -1588,6 +1632,9 @@ int ir_compute_live_ranges(ir_ctx *ctx) } } continue; + } else if (insn->op == IR_PHI) { + /* dead PHI */ + continue; } IR_ASSERT(insn->op != IR_PHI && (!ctx->rules || !(ctx->rules[ref] & (IR_FUSED|IR_SKIPPED)))); @@ -1643,6 +1690,8 @@ int ir_compute_live_ranges(ir_ctx *ctx) /* Input is the same as "op1" */ use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } + } else if ((def_flags & IR_CALL_WITH_ARGS) && j > 2) { + use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref); } if (!IS_LIVE_IN_BLOCK(v, b)) { /* live.add(opd) */ @@ -2121,7 +2170,8 @@ int ir_coalesce(ir_ctx *ctx) for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) { use = *p; insn = &ctx->ir_base[use]; - if (insn->op == IR_PHI) { + /* dead PHIs have no vreg */ + if (insn->op == IR_PHI && ctx->vregs[use] > 0) { input = ir_insn_op(insn, k); if (input > 0 && ctx->vregs[input] > 0) { uint32_t v1 = ctx->vregs[input]; @@ -2286,7 +2336,9 @@ int ir_compute_dessa_moves(ir_ctx *ctx) insn = &ctx->ir_base[use]; if (insn->op == IR_PHI) { if (ctx->use_lists[use].count == 0) { - ctx->rules[use] = IR_SKIPPED | IR_NOP; + if (ctx->rules) { + ctx->rules[use] = IR_SKIPPED | IR_NOP; + } ctx->vregs[use] = 0; continue; } @@ -2371,7 +2423,8 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat for (i = use_list->count, p = &ctx->use_edges[use_list->refs]; i > 0; p++, i--) { ref = *p; insn = &ctx->ir_base[ref]; - if (insn->op == IR_PHI) { + /* dead PHIs have no vreg */ + if (insn->op == IR_PHI && ctx->vregs[ref] > 0) { input = ir_insn_op(insn, k); if (IR_IS_CONST_REF(input) || !(ctx->vregs[input] > 0)) { have_constants_or_addresses = 1; @@ -2440,7 +2493,7 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat for (i = use_list->count, p = &ctx->use_edges[use_list->refs]; i > 0; p++, i--) { ref = *p; insn = &ctx->ir_base[ref]; - if (insn->op == IR_PHI) { + if (insn->op == IR_PHI && ctx->vregs[ref] > 0) { input = ir_insn_op(insn, k); if (IR_IS_CONST_REF(input) || !(ctx->vregs[input] > 0)) { emit_copy(ctx, insn->type, input, ref, data); @@ -2468,7 +2521,7 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat # define IR_REG_NAME_VAL(_ival) ir_reg_name((_ival)->reg, (_ival)->type) # endif # define IR_LOG_LSRA(action, ival, comment) do { \ - if (ctx->flags & IR_DEBUG_RA) { \ + if (ctx->debug_flags & IR_DEBUG_RA) { \ ir_live_interval *_ival = (ival); \ ir_live_pos _start = _ival->range.start; \ ir_live_pos _end = _ival->end; \ @@ -2479,7 +2532,7 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat } \ } while (0) # define IR_LOG_LSRA_ASSIGN(action, ival, comment) do { \ - if (ctx->flags & IR_DEBUG_RA) { \ + if (ctx->debug_flags & IR_DEBUG_RA) { \ ir_live_interval *_ival = (ival); \ ir_live_pos _start = _ival->range.start; \ ir_live_pos _end = _ival->end; \ @@ -2491,7 +2544,7 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat } \ } while (0) # define IR_LOG_LSRA_SPLIT(ival, pos) do { \ - if (ctx->flags & IR_DEBUG_RA) { \ + if (ctx->debug_flags & IR_DEBUG_RA) { \ ir_live_interval *_ival = (ival); \ ir_live_pos _start = _ival->range.start; \ ir_live_pos _end = _ival->end; \ @@ -2504,7 +2557,7 @@ int ir_gen_dessa_moves(ir_ctx *ctx, uint32_t b, emit_copy_t emit_copy, void *dat } \ } while (0) # define IR_LOG_LSRA_CONFLICT(action, ival, pos) do { \ - if (ctx->flags & IR_DEBUG_RA) { \ + if (ctx->debug_flags & IR_DEBUG_RA) { \ ir_live_interval *_ival = (ival); \ ir_live_pos _start = _ival->range.start; \ ir_live_pos _end = _ival->end; \ @@ -3175,13 +3228,7 @@ static ir_reg ir_try_allocate_free_reg(ir_ctx *ctx, ir_live_interval *ival, ir_l ir_live_interval *other; ir_regset available, overlapped, scratch; - if (IR_IS_TYPE_FP(ival->type) || IR_IS_TYPE_VECTOR(ival->type)) { - available = IR_REGSET_FP; - /* set freeUntilPos of all physical registers to maxInt */ - for (i = IR_REG_FP_FIRST; i <= IR_REG_FP_LAST; i++) { - freeUntilPos[i] = 0x7fffffff; - } - } else { + if (IR_IS_TYPE_INT(ival->type)) { available = IR_REGSET_GP; if (ctx->flags & IR_USE_FRAME_POINTER) { IR_REGSET_EXCL(available, IR_REG_FRAME_POINTER); @@ -3198,9 +3245,17 @@ static ir_reg ir_try_allocate_free_reg(ir_ctx *ctx, ir_live_interval *ival, ir_l for (i = IR_REG_GP_FIRST; i <= IR_REG_GP_LAST; i++) { freeUntilPos[i] = 0x7fffffff; } + } else { + IR_ASSERT(IR_IS_TYPE_FP(ival->type) || IR_IS_TYPE_VECTOR(ival->type)); + available = IR_REGSET_FP; + /* set freeUntilPos of all physical registers to maxInt */ + for (i = IR_REG_FP_FIRST; i <= IR_REG_FP_LAST; i++) { + freeUntilPos[i] = 0x7fffffff; + } } available = IR_REGSET_DIFFERENCE(available, (ir_regset)ctx->fixed_regset); + IR_ASSERT(!IR_REGSET_IS_EMPTY(available) && "No registers available. Allocation is not possible"); /* for each interval it in active */ other = *active; @@ -3513,15 +3568,7 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li next_use_pos = ival->range.end; } - if (IR_IS_TYPE_FP(ival->type) || IR_IS_TYPE_VECTOR(ival->type)) { - available = IR_REGSET_FP; - /* set nextUsePos of all physical registers to maxInt */ - for (i = IR_REG_FP_FIRST; i <= IR_REG_FP_LAST; i++) { - nextUsePos[i] = 0x7fffffff; - blockPos[i] = 0x7fffffff; - scores[i] = 0; - } - } else { + if (IR_IS_TYPE_INT(ival->type)) { available = IR_REGSET_GP; if (ctx->flags & IR_USE_FRAME_POINTER) { IR_REGSET_EXCL(available, IR_REG_FRAME_POINTER); @@ -3540,6 +3587,15 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li blockPos[i] = 0x7fffffff; scores[i] = 0; } + } else { + IR_ASSERT(IR_IS_TYPE_FP(ival->type) || IR_IS_TYPE_VECTOR(ival->type)); + available = IR_REGSET_FP; + /* set nextUsePos of all physical registers to maxInt */ + for (i = IR_REG_FP_FIRST; i <= IR_REG_FP_LAST; i++) { + nextUsePos[i] = 0x7fffffff; + blockPos[i] = 0x7fffffff; + scores[i] = 0; + } } available = IR_REGSET_DIFFERENCE(available, (ir_regset)ctx->fixed_regset); @@ -3604,13 +3660,13 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li if (overlap) { ir_regset regset = IR_REGSET_INTERSECTION(available, ir_scratch_regset[reg - IR_REG_NUM]); IR_REGSET_FOREACH(regset, reg) { - if (overlap < nextUsePos[reg]) { - nextUsePos[reg] = overlap; - scores[reg] = 0; - } if (overlap < blockPos[reg]) { blockPos[reg] = overlap; } + if (blockPos[reg] < nextUsePos[reg]) { + nextUsePos[reg] = blockPos[reg]; + scores[reg] = 0; + } } IR_REGSET_FOREACH_END(); } } else if (IR_REGSET_IN(available, reg)) { @@ -3618,13 +3674,13 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li if (overlap) { if (other->flags & (IR_LIVE_INTERVAL_FIXED|IR_LIVE_INTERVAL_TEMP)) { - if (overlap < nextUsePos[reg]) { - nextUsePos[reg] = overlap; - scores[reg] = 0; - } if (overlap < blockPos[reg]) { blockPos[reg] = overlap; } + if (blockPos[reg] < nextUsePos[reg]) { + nextUsePos[reg] = blockPos[reg]; + scores[reg] = 0; + } } else { pos = ir_first_use_pos_after(other, ival->range.start, IR_USE_MUST_BE_IN_REG | IR_USE_SHOULD_BE_IN_REG); @@ -3645,13 +3701,13 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li if (overlap) { if (other->flags & (IR_LIVE_INTERVAL_FIXED|IR_LIVE_INTERVAL_TEMP)) { - if (overlap < nextUsePos[reg]) { - nextUsePos[reg] = overlap; - scores[reg] = 0; - } if (overlap < blockPos[reg]) { blockPos[reg] = overlap; } + if (blockPos[reg] < nextUsePos[reg]) { + nextUsePos[reg] = blockPos[reg]; + scores[reg] = 0; + } } else { pos = ir_first_use_pos_after(other, ival->range.start, IR_USE_MUST_BE_IN_REG | IR_USE_SHOULD_BE_IN_REG); @@ -3787,20 +3843,7 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li IR_LOG_LSRA(" ---- Queue", other, ""); return IR_REG_NONE; } - if (split_pos >= block_pos) { -try_next_available_register: - IR_REGSET_EXCL(available, reg); -#if IR_X86_I64 - if (reg_hi != IR_REG_NONE) IR_REGSET_EXCL(available, reg_hi); -#endif - if (IR_REGSET_IS_EMPTY(available)) { - fprintf(stderr, "LSRA Internal Error: Unsolvable conflict. Allocation is not possible\n"); - IR_ASSERT(0); - exit(-1); - } - IR_LOG_LSRA(" ---- Restart", ival, ""); - goto select_register; - } + // ??? IR_ASSERT(split_pos != block_pos); split_pos = ir_find_optimal_split_position(ctx, ival, split_pos, block_pos, 1); other = ir_split_interval_at(ctx, ival, split_pos); ir_add_to_unhandled(unhandled, other); @@ -3840,7 +3883,18 @@ static ir_reg ir_allocate_blocked_reg(ir_ctx *ctx, ir_live_interval *ival, ir_li } IR_LOG_LSRA(" ---- Finish", other, ""); } else { - goto try_next_available_register; +try_next_available_register: + IR_REGSET_EXCL(available, reg); +#if IR_X86_I64 + if (reg_hi != IR_REG_NONE) IR_REGSET_EXCL(available, reg_hi); +#endif + if (IR_REGSET_IS_EMPTY(available)) { + fprintf(stderr, "LSRA Internal Error: Unsolvable conflict. Allocation is not possible\n"); + IR_ASSERT(0); + exit(-1); + } + IR_LOG_LSRA(" ---- Restart", ival, ""); + goto select_register; } } else { child = other; @@ -3992,9 +4046,7 @@ static int ir_linear_scan(ir_ctx *ctx, ir_ref vars) ir_live_pos position; ir_reg reg; - if (!ctx->live_intervals) { - return 0; - } + IR_ASSERT(ctx->live_intervals); if (ctx->flags2 & IR_LR_HAVE_DESSA_MOVES) { /* Add fixed intervals for temporary registers used for DESSA moves */ @@ -4095,7 +4147,7 @@ static int ir_linear_scan(ir_ctx *ctx, ir_ref vars) ctx->flags2 &= ~(IR_RA_HAVE_SPLITS|IR_RA_HAVE_SPILLS); #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_RA) { + if (ctx->debug_flags & IR_DEBUG_RA) { fprintf(stderr, "----\n"); ir_dump_live_ranges(ctx, stderr); fprintf(stderr, "---- Start LSRA\n"); @@ -4325,7 +4377,7 @@ static int ir_linear_scan(ir_ctx *ctx, ir_ref vars) #endif #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_RA) { + if (ctx->debug_flags & IR_DEBUG_RA) { fprintf(stderr, "---- Finish LSRA\n"); ir_dump_live_ranges(ctx, stderr); fprintf(stderr, "----\n"); @@ -4548,7 +4600,11 @@ static void assign_regs(ir_ctx *ctx) if (use_pos->hint_ref < 0) { if (use_pos->flags & IR_PHI_USE) { IR_ASSERT(use_pos->hint_ref < 0); - IR_ASSERT(ctx->vregs[-use_pos->hint_ref] > 0); + if (ctx->vregs[-use_pos->hint_ref] == 0) { + /* dead PHI */ + use_pos = use_pos->next; + continue; + } IR_ASSERT(ctx->live_intervals[ctx->vregs[-use_pos->hint_ref]]); if (ctx->live_intervals[ctx->vregs[-use_pos->hint_ref]]->flags & IR_LIVE_INTERVAL_SPILLED) { /* Spilled PHI var is passed through memory */ diff --git a/ext/opcache/jit/ir/ir_sccp.c b/ext/opcache/jit/ir/ir_sccp.c index 8818a667f6fd..196117146585 100644 --- a/ext/opcache/jit/ir/ir_sccp.c +++ b/ext/opcache/jit/ir/ir_sccp.c @@ -523,14 +523,6 @@ static bool ir_is_dead_load_ex(const ir_ctx *ctx, ir_ref ref, uint32_t flags, co return (flags & IR_OP_FLAG_NO_SIDE_EFFECT) && ctx->use_lists[ref].count == 1; } -static bool ir_is_dead_load(const ir_ctx *ctx, ir_ref ref) -{ - if (ctx->use_lists[ref].count == 1) { - return (ir_op_flags[ctx->ir_base[ref].op] & IR_OP_FLAG_NO_SIDE_EFFECT) != 0; - } - return 0; -} - static bool ir_is_dead(const ir_ctx *ctx, ir_ref ref) { if (ctx->use_lists[ref].count == 0) { @@ -866,7 +858,7 @@ static IR_NEVER_INLINE void ir_sccp_analyze(const ir_ctx *ctx, ir_sccp_val *_val } #ifdef IR_DEBUG - if (ctx->flags & IR_DEBUG_SCCP) { + if (ctx->debug_flags & IR_DEBUG_SCCP) { for (i = 1; i < ctx->insns_count; i++) { if (IR_IS_CONST_OP(_values[i].op) || IR_IS_SYM_CONST(_values[i].op)) { fprintf(stderr, "%d. CONST(", i); @@ -1115,6 +1107,9 @@ static bool ir_sccp_remove_unfeasible_merge_inputs(ir_ctx *ctx, ir_ref ref, ir_i k++; } else if (input > 0) { ir_use_list_remove_one(ctx, input, use); + if (ir_is_dead(ctx, input)) { + ir_bitqueue_add(worklist, input); + } } } while (k <= old_merge_inputs) { @@ -1820,15 +1815,12 @@ static bool ir_may_promote_trunc(const ir_ctx *ctx, ir_type type, ir_ref ref) case IR_TRUNC: return ctx->ir_base[insn->op1].type == type || ctx->use_lists[ref].count == 1; case IR_NEG: - case IR_ABS: case IR_NOT: return ctx->use_lists[ref].count == 1 && ir_may_promote_trunc(ctx, type, insn->op1); case IR_ADD: case IR_SUB: case IR_MUL: - case IR_MIN: - case IR_MAX: case IR_OR: case IR_AND: case IR_XOR: @@ -1951,7 +1943,6 @@ static ir_ref ir_promote_i2i(ir_ctx *ctx, ir_type type, ir_ref ref, ir_ref use) } return insn->op1; case IR_NEG: - case IR_ABS: case IR_NOT: op = ir_promote_i2i(ctx, type, insn->op1, ref); insn = &ctx->ir_base[ref]; @@ -1961,8 +1952,6 @@ static ir_ref ir_promote_i2i(ir_ctx *ctx, ir_type type, ir_ref ref, ir_ref use) case IR_ADD: case IR_SUB: case IR_MUL: - case IR_MIN: - case IR_MAX: case IR_OR: case IR_AND: case IR_XOR: @@ -2323,7 +2312,9 @@ static bool ir_try_promote_induction_var_ext(ir_ctx *ctx, ir_ref ext_ref, ir_ref if (use_insn->op1 != phi_ref) { if (IR_IS_CONST_REF(use_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[use_insn->op1].op)) { - ctx->ir_base[use].op1 = ir_ext_const(ctx, &ctx->ir_base[use_insn->op1], op, type); + ir_ref tmp = ir_ext_const(ctx, &ctx->ir_base[use_insn->op1], op, type); + use_insn = &ctx->ir_base[use]; + use_insn->op1 = tmp; } else { ir_ref tmp = ir_ext_ref(ctx, use, use_insn->op1, op, type); use_insn = &ctx->ir_base[use]; @@ -2334,7 +2325,9 @@ static bool ir_try_promote_induction_var_ext(ir_ctx *ctx, ir_ref ext_ref, ir_ref if (use_insn->op2 != phi_ref) { if (IR_IS_CONST_REF(use_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[use_insn->op2].op)) { - ctx->ir_base[use].op2 = ir_ext_const(ctx, &ctx->ir_base[use_insn->op2], op, type); + ir_ref tmp = ir_ext_const(ctx, &ctx->ir_base[use_insn->op2], op, type); + use_insn = &ctx->ir_base[use]; + use_insn->op2 = tmp; } else { ir_ref tmp = ir_ext_ref(ctx, use, use_insn->op2, op, type); use_insn = &ctx->ir_base[use]; @@ -2364,7 +2357,9 @@ static bool ir_try_promote_induction_var_ext(ir_ctx *ctx, ir_ref ext_ref, ir_ref if (use_insn->op1 != op_ref) { if (IR_IS_CONST_REF(use_insn->op1) && !IR_IS_SYM_CONST(ctx->ir_base[use_insn->op1].op)) { - ctx->ir_base[use].op1 = ir_ext_const(ctx, &ctx->ir_base[use_insn->op1], op, type); + ir_ref tmp = ir_ext_const(ctx, &ctx->ir_base[use_insn->op1], op, type); + use_insn = &ctx->ir_base[use]; + use_insn->op1 = tmp; } else { ir_ref tmp = ir_ext_ref(ctx, use, use_insn->op1, op, type); use_insn = &ctx->ir_base[use]; @@ -2375,7 +2370,9 @@ static bool ir_try_promote_induction_var_ext(ir_ctx *ctx, ir_ref ext_ref, ir_ref if (use_insn->op2 != op_ref) { if (IR_IS_CONST_REF(use_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[use_insn->op2].op)) { - ctx->ir_base[use].op2 = ir_ext_const(ctx, &ctx->ir_base[use_insn->op2], op, type); + ir_ref tmp = ir_ext_const(ctx, &ctx->ir_base[use_insn->op2], op, type); + use_insn = &ctx->ir_base[use]; + use_insn->op2 = tmp; } else { ir_ref tmp = ir_ext_ref(ctx, use, use_insn->op2, op, type); use_insn = &ctx->ir_base[use]; @@ -2399,7 +2396,8 @@ static bool ir_try_promote_induction_var_ext(ir_ctx *ctx, ir_ref ext_ref, ir_ref phi_insn->type = type; if (IR_IS_CONST_REF(phi_insn->op2) && !IR_IS_SYM_CONST(ctx->ir_base[phi_insn->op2].op)) { - ctx->ir_base[phi_ref].op2 = ir_ext_const(ctx, &ctx->ir_base[phi_insn->op2], op, type); + ir_ref tmp = ir_ext_const(ctx, &ctx->ir_base[phi_insn->op2], op, type); + ctx->ir_base[phi_ref].op2 = tmp; } else { ir_ref tmp = ir_ext_ref(ctx, phi_ref, phi_insn->op2, op, type); ctx->ir_base[phi_ref].op2 = tmp; @@ -2456,10 +2454,15 @@ static bool ir_try_promote_ext(ir_ctx *ctx, ir_ref ext_ref, ir_insn *insn) ir_op op = ctx->ir_base[ext_ref].op; ir_type type = ctx->ir_base[ext_ref].type; + ir_ref tmp; insn->type = type; - insn->op2 = ir_ext_const(ctx, &ctx->ir_base[insn->op2], op, type);; - insn->op3 = ir_ext_const(ctx, &ctx->ir_base[insn->op3], op, type);; + tmp = ir_ext_const(ctx, &ctx->ir_base[insn->op2], op, type); + insn = &ctx->ir_base[ref]; + insn->op2 = tmp; + tmp = ir_ext_const(ctx, &ctx->ir_base[insn->op3], op, type); + insn = &ctx->ir_base[ref]; + insn->op3 = tmp; ir_iter_replace_insn(ctx, ext_ref, ref); } diff --git a/ext/opcache/jit/ir/ir_x86.dasc b/ext/opcache/jit/ir/ir_x86.dasc index aa4e6e3da52d..699e25b68e1e 100644 --- a/ext/opcache/jit/ir/ir_x86.dasc +++ b/ext/opcache/jit/ir/ir_x86.dasc @@ -1316,8 +1316,13 @@ typedef struct _ir_x86_64_sysv_va_list { void *reg_save_area; } ir_x86_64_sysv_va_list; -#define IR_GP_REG_NAME(code, name64, name32, name16, name8, name8h) \ +#ifdef IR_TARGET_X64 +# define IR_GP_REG_NAME(code, name64, name32, name16, name8, name8h) \ #name64, +#else +# define IR_GP_REG_NAME(code, name64, name32, name16, name8, name8h) \ + #name32, +#endif #define IR_GP_REG_NAME32(code, name64, name32, name16, name8, name8h) \ #name32, #define IR_GP_REG_NAME16(code, name64, name32, name16, name8, name8h) \ @@ -1717,6 +1722,7 @@ const ir_call_conv_dsc ir_call_conv_x86_fastcall = { _(UMUL_OV_I64) \ _(BINOP_HELPER_I64) \ _(OP_I64) \ + _(BOOL_NOT_I64) \ _(SHIFT_I64) \ _(SHIFT_CONST_I64) \ _(SEXT_I64) \ @@ -1732,6 +1738,7 @@ const ir_call_conv_dsc ir_call_conv_x86_fastcall = { _(GUARD_I64) \ _(GUARD_CMP_I64) \ _(COND_I64) \ + _(COND_TEST_I64) \ _(COND_I64_CMP_INT) \ _(COND_I64_CMP_FP) \ _(COND_CMP_I64) \ @@ -2181,8 +2188,10 @@ get_arg_hints: constraints->tmp_regs[n] = IR_TMP_REG(1, IR_ADDR, IR_LOAD_SUB_REF, IR_USE_SUB_REF); n++; } + flags = IR_USE_SHOULD_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_OP3_SHOULD_BE_IN_REG | IR_CALL_WITH_ARGS; + } else { + flags = IR_USE_SHOULD_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_OP3_SHOULD_BE_IN_REG; } - flags = IR_USE_SHOULD_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_OP3_SHOULD_BE_IN_REG; #if IR_X86_I64 if (insn->op == IR_CALL && (insn->type == IR_I64 || insn->type == IR_U64)) { flags |= IR_HINT_TWO_REGS; @@ -2466,7 +2475,7 @@ get_arg_hints: flags = IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_DEF_CONFLICTS_WITH_INPUT_REGS; insn = &ctx->ir_base[ref]; if (insn->op == IR_EQ || insn->op == IR_NE) { - constraints->tmp_regs[0] = IR_TMP_REG(3, IR_U32, IR_USE_SUB_REF, IR_DEF_SUB_REF); + constraints->tmp_regs[0] = IR_TMP_REG(3, IR_U32, IR_LOAD_SUB_REF, IR_DEF_SUB_REF); n = 1; break; } @@ -2475,6 +2484,9 @@ get_arg_hints: case IR_OP_I64: flags = IR_USE_MUST_BE_IN_REG | IR_DEF_REUSES_OP1_REG | IR_OP1_SHOULD_BE_IN_REG; break; + case IR_BOOL_NOT_I64: + flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG; + break; case IR_SEXT_I64: flags = IR_USE_SHOULD_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG | IR_HINT_TWO_REGS; constraints->def_reg = IR_REG_I64_PAIR(IR_REG_RAX, IR_REG_RDX); @@ -2593,6 +2605,26 @@ get_arg_hints: n = 1; } break; + case IR_COND_TEST_I64: + insn = &ctx->ir_base[ref]; + if (IR_IS_TYPE_INT(insn->type)) { + if (IR_IS_CONST_REF(insn->op3) || ir_rule(ctx, insn->op3) == IR_STATIC_ALLOCA) { + flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG; + constraints->tmp_regs[0] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); + n = 1; + } else if (IR_IS_CONST_REF(insn->op2) || ir_rule(ctx, insn->op2) == IR_STATIC_ALLOCA) { + flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG; + constraints->tmp_regs[0] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); + n = 1; + } else { + flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG; + } + } else { + flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_OP3_SHOULD_BE_IN_REG; + } + constraints->tmp_regs[n] = IR_TMP_REG(4, IR_U32, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); + n++; + break; case IR_COND_I64_CMP_INT: insn = &ctx->ir_base[ref]; insn = &ctx->ir_base[insn->op1]; @@ -2746,8 +2778,10 @@ get_arg_hints: constraints->tmp_regs[n] = IR_TMP_REG(2, IR_DOUBLE, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); n++; } - constraints->tmp_regs[n] = IR_TMP_REG(4, IR_DOUBLE, IR_USE_SUB_REF, IR_SAVE_SUB_REF); - n++; + if (!(ctx->mflags & IR_X86_AVX)) { + constraints->tmp_regs[n] = IR_TMP_REG(4, IR_DOUBLE, IR_USE_SUB_REF, IR_SAVE_SUB_REF); + n++; + } break; case IR_SHUFPS_2_12: flags = IR_DEF_CONFLICTS_WITH_INPUT_REGS | IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG | IR_OP2_MUST_BE_IN_REG; @@ -2760,8 +2794,10 @@ get_arg_hints: constraints->tmp_regs[n] = IR_TMP_REG(2, IR_DOUBLE, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF); n++; } - constraints->tmp_regs[n] = IR_TMP_REG(4, IR_DOUBLE, IR_USE_SUB_REF, IR_SAVE_SUB_REF); - n++; + if (!(ctx->mflags & IR_X86_AVX)) { + constraints->tmp_regs[n] = IR_TMP_REG(4, IR_DOUBLE, IR_USE_SUB_REF, IR_SAVE_SUB_REF); + n++; + } break; case IR_SHUFFLE: flags = IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG; @@ -4247,6 +4283,12 @@ binop_fp: return IR_BOOL_NOT; } else { IR_ASSERT(IR_IS_TYPE_INT(ctx->ir_base[insn->op1].type)); +#if IR_X86_I64 + if (ctx->ir_base[insn->op1].type == IR_I64 || ctx->ir_base[insn->op1].type == IR_U64) { + ir_match_fuse_load(ctx, insn->op1, ref); + return IR_BOOL_NOT_I64; + } +#endif return IR_BOOL_NOT_INT; } #if IR_SIMD @@ -4600,6 +4642,10 @@ binop_fp: #endif return ctx->use_lists[ref].count > 0 ? IR_PARAM : IR_SKIPPED | IR_PARAM; case IR_ALLOCA: + if (ctx->use_lists[ref].count == 1) { + /* dead ALLOCA */ + return IR_NOP | IR_SKIPPED; + } /* alloca() may be used only in functions */ if (ctx->flags & IR_FUNCTION) { if (IR_IS_CONST_REF(insn->op2) && ctx->cfg_map[ref] == 1) { @@ -4752,8 +4798,30 @@ store_int: return IR_VSTORE_FP; } break; + case IR_BLOCK_BEGIN: + case IR_TLS_ADDR: + if (ctx->use_lists[ref].count == 1) { + return IR_NOP | IR_SKIPPED; + } + return insn->op; + case IR_VLOAD: + case IR_VLOAD_v: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } +#if IR_X86_I64 + if (insn->type == IR_I64 || insn->type == IR_U64) { + return IR_TWO_REGS | insn->op; + } +#endif + return insn->op; case IR_LOAD: case IR_LOAD_v: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } if (ctx->ir_base[insn->op2].op == IR_TLS_ADDR && ir_may_fuse_tls_addr(ctx, insn->op2)) { ctx->rules[insn->op2] = IR_FUSED | IR_SIMPLE | IR_TLS_ADDR; return IR_TLS_LOAD; @@ -4797,6 +4865,10 @@ store_int: } break; case IR_RLOAD: + if (ctx->use_lists[ref].count == 1) { + /* dead load */ + return IR_NOP | IR_SKIPPED; + } if (IR_REGSET_IN(IR_REGSET_UNION((ir_regset)ctx->fixed_regset, IR_REGSET_FIXED), insn->op2)) { return IR_SKIPPED | IR_RLOAD; } @@ -5105,6 +5177,8 @@ store_int: #if IR_X86_I64 if (insn->type == IR_I64 || insn->type == IR_U64) { return IR_TWO_REGS | IR_COND_I64; + } else if (ctx->ir_base[insn->op1].type == IR_I64 || ctx->ir_base[insn->op1].type == IR_U64) { + return IR_COND_TEST_I64; } #endif return IR_COND; @@ -5424,10 +5498,13 @@ store_int: case IR_SHUFFLE: return ir_match_shuffle(ctx, insn); #endif -#if IR_X86_I64 case IR_PHI: - case IR_VLOAD: - case IR_VLOAD_v: + if (ctx->use_lists[ref].count == 0) { + /* Dead PHI */ + return IR_SKIPPED | IR_NOP; + } + IR_FALLTHROUGH; +#if IR_X86_I64 case IR_VA_ARG: case IR_EXTRACT: if (insn->type == IR_I64 || insn->type == IR_U64) { @@ -6292,6 +6369,25 @@ static void ir_emit_swap(ir_ctx *ctx, ir_type type, ir_reg dst, ir_reg src) | ASM_REG_REG_OP xchg, type, dst, src } +#if IR_X86_I64 +/* Move a 64-bit value between register pairs without overwriting a source half before it is read */ +static void ir_emit_mov_i64_pair(ir_ctx *ctx, ir_reg dst_lo, ir_reg dst_hi, ir_reg src_lo, ir_reg src_hi) +{ + if (dst_lo == src_hi) { + IR_ASSERT(dst_hi != src_lo); + ir_emit_mov(ctx, IR_U32, dst_hi, src_hi); + ir_emit_mov(ctx, IR_U32, dst_lo, src_lo); + } else { + if (dst_lo != src_lo) { + ir_emit_mov(ctx, IR_U32, dst_lo, src_lo); + } + if (dst_hi != src_hi) { + ir_emit_mov(ctx, IR_U32, dst_hi, src_hi); + } + } +} +#endif + static void ir_emit_mov_ext(ir_ctx *ctx, ir_type type, ir_reg dst, ir_reg src) { ir_backend_data *data = ctx->data; @@ -7798,13 +7894,6 @@ static void ir_emit_mem_shift(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg op2_reg = ctx->regs[insn->op3][2]; ir_mem mem; - if (insn->op == IR_STORE) { - mem = ir_fuse_mem(ctx, def, def, insn, ctx->regs[def][2]); - } else { - IR_ASSERT(insn->op == IR_VSTORE); - mem = ir_var_spill_slot(ctx, insn->op2); - } - if (op2_reg != IR_REG_NONE && IR_REG_SPILLED(op2_reg)) { op2_reg = IR_REG_NUM(op2_reg); ir_emit_load(ctx, type, op2_reg, op2); @@ -7816,6 +7905,15 @@ static void ir_emit_mem_shift(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_emit_load(ctx, type, IR_REG_RCX, op2); } } + + /* Set up the address after the count is in RCX: the address may be loaded into the count's register */ + if (insn->op == IR_STORE) { + mem = ir_fuse_mem(ctx, def, def, insn, ctx->regs[def][2]); + } else { + IR_ASSERT(insn->op == IR_VSTORE); + mem = ir_var_spill_slot(ctx, insn->op2); + } + switch (op_insn->op) { default: IR_ASSERT(0); @@ -7990,9 +8088,27 @@ static void ir_emit_bit_count(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_ref op1 = insn->op1; ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); ir_reg op1_reg = ctx->regs[def][1]; + bool zero_extended = 0; IR_ASSERT(def_reg != IR_REG_NONE); + if (op1_reg == IR_REG_NONE && ir_type_size[src_type] < 4) { + ir_mem mem; + + if (ir_rule(ctx, op1) & IR_FUSED) { + mem = ir_fuse_load(ctx, def, op1); + } else { + mem = ir_ref_spill_slot(ctx, op1); + } + if (ir_type_size[src_type] == 1) { + | ASM_TXT_TMEM_OP movzx, Rd(def_reg), byte, mem + } else { + | ASM_TXT_TMEM_OP movzx, Rd(def_reg), word, mem + } + op1_reg = def_reg; + zero_extended = 1; + } + if (op1_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op1_reg)) { op1_reg = IR_REG_NUM(op1_reg); @@ -8001,34 +8117,24 @@ static void ir_emit_bit_count(ir_ctx *ctx, ir_ref def, ir_insn *insn) switch (ir_type_size[src_type]) { default: IR_ASSERT(0); + case 1: case 2: - if (insn->op == IR_CTLZ) { - if (ctx->mflags & IR_X86_BMI1) { - | lzcnt Rw(def_reg), Rw(op1_reg) - } else { - | bsr Rw(def_reg), Rw(op1_reg) - | xor Rw(def_reg), 0xf - } - } else if (insn->op == IR_CTTZ) { - if (ctx->mflags & IR_X86_BMI1) { - | tzcnt Rw(def_reg), Rw(op1_reg) + /* upper bits don't affect CTTZ of a non-zero value */ + if (insn->op != IR_CTTZ && !zero_extended) { + if (ir_type_size[src_type] == 1) { + | movzx Rd(def_reg), Rb(op1_reg) } else { - | bsf Rw(def_reg), Rw(op1_reg) + | movzx Rd(def_reg), Rw(op1_reg) } - } else { - IR_ASSERT(insn->op == IR_CTPOP); - | popcnt Rw(def_reg), Rw(op1_reg) + op1_reg = def_reg; } - break; - case 1: - | movzx Rd(op1_reg), Rb(op1_reg) if (insn->op == IR_CTLZ) { if (ctx->mflags & IR_X86_BMI1) { | lzcnt Rd(def_reg), Rd(op1_reg) - | sub Rd(def_reg), 24 + | sub Rd(def_reg), (32 - ir_type_size[src_type] * 8) } else { | bsr Rd(def_reg), Rd(op1_reg) - | xor Rw(def_reg), 0x7 + | xor Rd(def_reg), (ir_type_size[src_type] * 8 - 1) } break; } @@ -8085,24 +8191,6 @@ static void ir_emit_bit_count(ir_ctx *ctx, ir_ref def, ir_insn *insn) switch (ir_type_size[src_type]) { default: IR_ASSERT(0); - case 2: - if (insn->op == IR_CTLZ) { - if (ctx->mflags & IR_X86_BMI1) { - | ASM_TXT_TMEM_OP lzcnt, Rw(def_reg), word, mem - } else { - | ASM_TXT_TMEM_OP bsr, Rw(def_reg), word, mem - | xor Rw(def_reg), 0xf - } - } else if (insn->op == IR_CTTZ) { - if (ctx->mflags & IR_X86_BMI1) { - | ASM_TXT_TMEM_OP tzcnt, Rw(def_reg), word, mem - } else { - | ASM_TXT_TMEM_OP bsf, Rw(def_reg), word, mem - } - } else { - | ASM_TXT_TMEM_OP popcnt, Rw(def_reg), word, mem - } - break; case 4: if (insn->op == IR_CTLZ) { if (ctx->mflags & IR_X86_BMI1) { @@ -8381,6 +8469,9 @@ static void ir_emit_bool_not_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (op1_reg != IR_REG_NONE) { | ASM_REG_REG_OP test, type, op1_reg, op1_reg + } else if (IR_IS_CONST_REF(op1) || ir_rule(ctx, op1) == IR_STATIC_ALLOCA) { + ir_emit_load(ctx, type, def_reg, op1); + | ASM_REG_REG_OP test, type, def_reg, def_reg } else { ir_mem mem = ir_ref_spill_slot(ctx, op1); @@ -8389,7 +8480,7 @@ static void ir_emit_bool_not_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | sete Rb(def_reg) if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } @@ -10794,6 +10885,72 @@ static void ir_emit_sext(ir_ctx *ctx, ir_ref def, ir_insn *insn) } } +static bool ir_result_is_zero_extended(ir_ctx *ctx, ir_reg reg, ir_ref ref) +{ + if (!IR_IS_CONST_REF(ref)) { + uint32_t rule = ctx->rules[ref] & IR_RULE_MASK; + + IR_ASSERT(IR_IS_TYPE_INT(ctx->ir_base[ref].type) && ir_type_size[ctx->ir_base[ref].type] == 4); + /* 32-bit operations with register destinations implicitly perform zero extension */ + switch (rule) { + case IR_SEXT: + case IR_ZEXT: + case IR_FP2INT: + case IR_COND: + case IR_VLOAD: + case IR_VA_ARG: + case IR_MUL_INT: + case IR_DIV_INT: + case IR_MOD_INT: + case IR_LEA_OB: + case IR_LEA_SI: + case IR_LEA_SIB: + case IR_LEA_IB: + case IR_LEA_SI_O: + case IR_LEA_SIB_O: + case IR_LEA_IB_O: + case IR_LEA_I_OB: + case IR_LEA_OB_I: + case IR_LEA_OB_SI: + case IR_LEA_SI_OB: + case IR_LEA_B_SI: + case IR_LEA_SI_B: + case IR_LEA_B_SI_O: + case IR_LEA_SI_B_O: + case IR_LEA_SYM_O: + case IR_LEA_O_SYM: + case IR_INC: + case IR_DEC: + case IR_MUL_PWR2: + case IR_DIV_PWR2: + case IR_MOD_PWR2: + case IR_SDIV_PWR2: + case IR_SMOD_PWR2: + case IR_ABS_INT: + case IR_OP_INT: + case IR_IMUL3: + case IR_BINOP_INT: + case IR_SHIFT: + case IR_SHIFT_CONST: + case IR_COND_TEST_INT: + case IR_COND_TEST_BIT: + case IR_COND_CMP_INT: + case IR_MIN_MAX_INT: + case IR_LOAD_INT: + case IR_BIT_COUNT: + case IR_BIT_OP: + case IR_AND_ZEXT: + return 1; + case IR_COPY_INT: + case IR_TRUNC: + case IR_BITCAST: + return ctx->regs[ref][1] != reg; + } + } + + return 0; +} + static void ir_emit_zext_common(ir_ctx *ctx, ir_ref def, ir_insn *insn, ir_type dst_type, ir_type src_type, uint8_t src_size, ir_reg def_reg, bool force) { ir_backend_data *data = ctx->data; @@ -10837,8 +10994,7 @@ static void ir_emit_zext_common(ir_ctx *ctx, ir_ref def, ir_insn *insn, ir_type IR_ASSERT(ir_type_size[dst_type] == 8); IR_ASSERT(sizeof(void*) == 8); |.if X64 - /* Avoid zero extension to the same register. This may be not always safe ??? */ - if (force || op1_reg != def_reg) { + if (force || op1_reg != def_reg || !ir_result_is_zero_extended(ctx, def_reg, insn->op1)) { | mov Rd(def_reg), Rd(op1_reg) } |.endif @@ -11844,10 +12000,6 @@ static void ir_emit_vload(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_mem mem; int32_t slot; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(ctx->ir_base[insn->op2].op == IR_VAR); fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER; slot = ir_local_slot(ctx, insn->op2); @@ -11968,10 +12120,6 @@ static void ir_emit_load_int(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(def_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { @@ -12031,10 +12179,6 @@ static void ir_emit_load_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_mem mem; bool avoid_spill_store = 0; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } IR_ASSERT(def_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { @@ -12442,10 +12586,6 @@ static void ir_emit_block_begin(ir_ctx *ctx, ir_ref def, ir_insn *insn) dasm_State **Dst = &data->dasm_state; ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } | mov Ra(def_reg), Ra(IR_REG_RSP) if (IR_REG_SPILLED(ctx->regs[def][0])) { @@ -12682,7 +12822,7 @@ static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg tmp_reg = ctx->regs[def][3]; int32_t offset; - IR_ASSERT((def_reg != IR_REG_NONE || ctx->use_lists[def].count == 1) && tmp_reg != IR_REG_NONE); + IR_ASSERT((def_reg != IR_REG_NONE || ctx->vregs[def] == 0) && tmp_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { op2_reg = IR_REG_NUM(op2_reg); @@ -12725,15 +12865,14 @@ static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn) | add Ra(tmp_reg), IR_MAX(ir_type_size[type], sizeof(void*)) } else { int size = IR_VA_ARG_SIZE(insn->op3); + int align = IR_VA_ARG_ALIGN(insn->op3); + IR_ASSERT(type == IR_ADDR); + if (align > (int)sizeof(void*)) { + | add Ra(tmp_reg), (align-1) + | and Ra(tmp_reg), ~(align-1) + } if (def_reg != IR_REG_NONE) { - IR_ASSERT(type == IR_ADDR); - int align = IR_VA_ARG_ALIGN(insn->op3); - - if (align > (int)sizeof(void*)) { - | add Ra(tmp_reg), (align-1) - | and Ra(tmp_reg), ~(align-1) - } | mov Ra(def_reg), Ra(tmp_reg) } | add Ra(tmp_reg), IR_ALIGNED_SIZE(size, sizeof(void*)) @@ -12752,7 +12891,7 @@ static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_reg tmp_reg = ctx->regs[def][3]; int32_t offset; - IR_ASSERT((def_reg != IR_REG_NONE || ctx->use_lists[def].count == 1) && tmp_reg != IR_REG_NONE); + IR_ASSERT((def_reg != IR_REG_NONE || ctx->vregs[def] == 0) && tmp_reg != IR_REG_NONE); if (op2_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op2_reg)) { op2_reg = IR_REG_NUM(op2_reg); @@ -13324,9 +13463,25 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i && used_stack <= ctx->fixed_call_stack_size && !cc->cleanup_stack_by_callee) { used_stack = 0; - } else { + } else if (used_stack) { /* Stack must be 16 byte aligned */ int32_t aligned_stack = IR_ALIGNED_SIZE(used_stack, 16); + + if (op == IR_TAILCALL && !(ctx->flags2 & (IR_16B_FRAME_ALIGNMENT|IR_HAS_CALLS))) { + if (!(ctx->flags & IR_FUNCTION)) { + while (IR_ALIGNED_SIZE(ctx->stack_frame_size + aligned_stack, 16) != ctx->stack_frame_size + aligned_stack) { + aligned_stack += sizeof(void*); + } + } else if (ctx->flags & IR_USE_FRAME_POINTER) { + while (IR_ALIGNED_SIZE(ctx->stack_frame_size + aligned_stack + sizeof(void*) * 2, 16) != ctx->stack_frame_size + aligned_stack + sizeof(void*) * 2) { + aligned_stack += sizeof(void*); + } + } else { + while (IR_ALIGNED_SIZE(ctx->stack_frame_size + aligned_stack + sizeof(void*), 16) != ctx->stack_frame_size + aligned_stack + sizeof(void*)) { + aligned_stack += sizeof(void*); + } + } + } ctx->call_stack_size += aligned_stack; if (aligned_stack) { ir_stack_alloca(ctx, aligned_stack); @@ -13497,18 +13652,35 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i } } else { /* Pass register arguments to stack (REG->MEM moves) */ - if (!IR_IS_CONST_REF(arg) && src_reg != IR_REG_NONE && !IR_REG_SPILLED(src_reg)) { + if (!IR_IS_CONST_REF(arg) && src_reg != IR_REG_NONE) { #if IR_X86_I64 if (type == IR_I64 || type == IR_U64) { ir_mem mem = IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset); ir_mem mem_hi = IR_MEM_I64_HI(mem); - ir_reg src_reg_hi = IR_REG_I64_HI(src_reg); - src_reg = IR_REG_I64_LO(src_reg); + ir_reg src_reg_hi; + + if (IR_REG_SPILLED(src_reg)) { + src_reg = IR_REG_NUM(src_reg); + src_reg_hi = IR_REG_I64_HI(src_reg); + src_reg = IR_REG_I64_LO(src_reg); + ir_emit_load_i64_lo(ctx, src_reg, arg); + ir_emit_load_i64_hi(ctx, src_reg_hi, arg); + } else { + src_reg_hi = IR_REG_I64_HI(src_reg); + src_reg = IR_REG_I64_LO(src_reg); + } ir_emit_store_mem(ctx, IR_U32, mem, src_reg); ir_emit_store_mem(ctx, IR_U32, mem_hi, src_reg_hi); - } else + } else { +#endif + if (IR_REG_SPILLED(src_reg)) { + src_reg = IR_REG_NUM(src_reg); + ir_emit_load(ctx, type, src_reg, arg); + } + ir_emit_store_mem(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), src_reg); +#if IR_X86_I64 + } #endif - ir_emit_store_mem(ctx, type, IR_MEM_BO(IR_REG_STACK_POINTER, stack_offset), src_reg); } else { do_pass3 = 1; } @@ -13668,16 +13840,6 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i ir_emit_store_mem_int(ctx, IR_U32, mem, tmp_reg); ir_emit_load_i64_hi(ctx, tmp_reg, arg); ir_emit_store_mem_int(ctx, IR_U32, mem_hi, tmp_reg); - } else if (IR_REG_SPILLED(src_reg)) { - ir_reg src_reg_hi; - - src_reg = IR_REG_NUM(src_reg); - src_reg_hi = IR_REG_I64_HI(src_reg); - src_reg = IR_REG_I64_LO(src_reg); - ir_emit_load_i64_lo(ctx, src_reg, arg); - ir_emit_load_i64_hi(ctx, src_reg_hi, arg); - ir_emit_store_mem_int(ctx, IR_U32, mem, src_reg); - ir_emit_store_mem_int(ctx, IR_U32, mem_hi, src_reg_hi); } } else #endif @@ -13687,10 +13849,6 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i IR_ASSERT(tmp_reg != IR_REG_NONE); ir_emit_load(ctx, type, tmp_reg, arg); ir_emit_store_mem_int(ctx, type, mem, tmp_reg); - } else if (IR_REG_SPILLED(src_reg)) { - src_reg = IR_REG_NUM(src_reg); - ir_emit_load(ctx, type, src_reg, arg); - ir_emit_store_mem_int(ctx, type, mem, src_reg); } } else { if (IR_IS_CONST_REF(arg)) { @@ -13699,10 +13857,6 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, const i IR_ASSERT(tmp_fp_reg != IR_REG_NONE); ir_emit_load(ctx, type, tmp_fp_reg, arg); ir_emit_store_mem_fp(ctx, type, mem, tmp_fp_reg); - } else if (IR_REG_SPILLED(src_reg)) { - src_reg = IR_REG_NUM(src_reg); - ir_emit_load(ctx, type, src_reg, arg); - ir_emit_store_mem_fp(ctx, type, mem, src_reg); } } stack_offset += IR_MAX(sizeof(void*), ir_get_type_size(type)); @@ -13824,12 +13978,7 @@ static void ir_emit_call_ex(ir_ctx *ctx, ir_ref def, ir_insn *insn, const ir_pro def_reg = IR_REG_I64_LO(def_reg); IR_ASSERT(cc->int_ret2_reg != IR_REG_NONE); - if (def_reg != cc->int_ret_reg) { - ir_emit_mov(ctx, IR_U32, def_reg, cc->int_ret_reg); - } - if (def_reg_hi != cc->int_ret2_reg) { - ir_emit_mov(ctx, IR_U32, def_reg_hi, cc->int_ret2_reg); - } + ir_emit_mov_i64_pair(ctx, def_reg, def_reg_hi, cc->int_ret_reg, cc->int_ret2_reg); if (IR_REG_SPILLED(ctx->regs[def][0])) { ir_emit_store_i64_lo(ctx, def, def_reg); ir_emit_store_i64_hi(ctx, def, def_reg_hi); @@ -13928,14 +14077,16 @@ static void ir_emit_tailcall(ir_ctx *ctx, ir_ref def, ir_insn *insn) { ir_backend_data *data = ctx->data; dasm_State **Dst = &data->dasm_state; + uint32_t old_call_stack_size = ctx->call_stack_size; const ir_proto_t *proto = ir_call_proto(ctx, insn); const ir_call_conv_dsc *cc = ir_get_call_conv_dsc(proto ? proto->flags : IR_CC_DEFAULT); int32_t used_stack = ir_emit_arguments(ctx, def, insn, proto, cc, IR_TAILCALL, ctx->regs[def][1]); ir_ref func = insn->op2; if (used_stack != 0) { - ir_emit_call_ex(ctx, def, insn, proto, cc, used_stack); + ir_emit_call_ex(ctx, def, insn, proto, cc, 0); ir_emit_return_void(ctx); + ctx->call_stack_size = old_call_stack_size; return; } @@ -14782,11 +14933,6 @@ static void ir_emit_tls_load(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_insn *addr_insn = &ctx->ir_base[insn->op2]; ir_mem mem; - if (ctx->use_lists[def].count == 1) { - /* dead load */ - return; - } - IR_ASSERT(def_reg != IR_REG_NONE); IR_ASSERT(addr_insn->op == IR_TLS_ADDR); @@ -15397,7 +15543,7 @@ static void ir_emit_binop_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) ir_emit_load_i64_lo(ctx, op2_reg, op2); ir_emit_load_i64_hi(ctx, op2_reg_hi, op2); } - } else { + } else if (op1 != op2) { op2_reg_hi = IR_REG_I64_HI(op2_reg); op2_reg = IR_REG_I64_LO(op2_reg); } @@ -16338,6 +16484,58 @@ static void ir_emit_op_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) } } +static void ir_emit_bool_not_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) +{ + ir_backend_data *data = ctx->data; + dasm_State **Dst = &data->dasm_state; + ir_ref op1 = insn->op1; + ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); + ir_reg op1_reg = ctx->regs[def][1]; + ir_reg op1_reg_hi; + + IR_ASSERT(def_reg != IR_REG_NONE); + + if (op1_reg != IR_REG_NONE) { + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + op1_reg_hi = IR_REG_I64_HI(op1_reg); + op1_reg = IR_REG_I64_LO(op1_reg); + ir_emit_load_i64_lo(ctx, op1_reg, op1); + ir_emit_load_i64_hi(ctx, op1_reg_hi, op1); + } else { + op1_reg_hi = IR_REG_I64_HI(op1_reg); + op1_reg = IR_REG_I64_LO(op1_reg); + } + if (def_reg == op1_reg) { + | or Rd(def_reg), Rd(op1_reg_hi) + } else if (def_reg == op1_reg_hi) { + | or Rd(def_reg), Rd(op1_reg) + } else { + | mov Rd(def_reg), Rd(op1_reg) + | or Rd(def_reg), Rd(op1_reg_hi) + } + | sete Rb(def_reg) + } else if (IR_IS_CONST_REF(op1)) { + | mov Rd(def_reg), !ir_const_is_true(&ctx->ir_base[op1]) + } else { + ir_mem mem, mem_hi; + + if (ir_rule(ctx, op1) & IR_FUSED) { + mem = ir_fuse_load(ctx, def, op1); + } else { + mem = ir_ref_spill_slot(ctx, op1); + } + mem_hi = IR_MEM_I64_HI(mem); + | ASM_REG_MEM_OP mov, IR_U32, def_reg, mem + | ASM_REG_MEM_OP or, IR_U32, def_reg, mem_hi + | sete Rb(def_reg) + } + + if (IR_REG_SPILLED(ctx->regs[def][0])) { + ir_emit_store(ctx, insn->type, def, def_reg); + } +} + static void ir_emit_sext_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) { ir_backend_data *data = ctx->data; @@ -16398,7 +16596,7 @@ static void ir_emit_zext_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) def_reg = IR_REG_I64_LO(def_reg); if (ir_type_size[ctx->ir_base[insn->op1].type] < 4) { ir_type src_type = ctx->ir_base[insn->op1].type; - ir_emit_zext_common(ctx, def, insn, IR_I32, src_type, ir_type_size[src_type], def_reg, 1); + ir_emit_zext_common(ctx, def, insn, IR_I32, src_type, ir_type_size[src_type], def_reg, 0); } else if (op1_reg != def_reg) { if (op1_reg != IR_REG_NONE && !IR_REG_SPILLED(op1_reg)) { | mov Rd(def_reg), Rd(op1_reg) @@ -17306,6 +17504,152 @@ static void ir_emit_cond_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) } } +static void ir_emit_cond_test_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) +{ + ir_backend_data *data = ctx->data; + dasm_State **Dst = &data->dasm_state; + ir_type type = insn->type; + ir_ref op1 = insn->op1; + ir_ref op2 = insn->op2; + ir_ref op3 = insn->op3; + ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); + ir_reg op1_reg = ctx->regs[def][1]; + ir_reg op2_reg = ctx->regs[def][2]; + ir_reg op3_reg = ctx->regs[def][3]; + ir_reg tmp_reg = ctx->tmp_regs[def]; + + IR_ASSERT(def_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE); + + if (op2_reg != IR_REG_NONE && IR_REG_SPILLED(op2_reg)) { + op2_reg = IR_REG_NUM(op2_reg); + ir_emit_load(ctx, type, op2_reg, op2); + if (op1 == op2) { + op1_reg = op2_reg; + } + if (op3 == op2) { + op3_reg = op2_reg; + } + } + if (op3_reg != IR_REG_NONE && IR_REG_SPILLED(op3_reg)) { + op3_reg = IR_REG_NUM(op3_reg); + ir_emit_load(ctx, type, op3_reg, op3); + if (op1 == op3) { + op1_reg = op3_reg; + } + } + + if (IR_IS_CONST_REF(op1)) { + if (!ir_const_is_true(&ctx->ir_base[insn->op1])) { + op2 = op3; + op2_reg = op3_reg; + } + + if (op2_reg != IR_REG_NONE) { + if (def_reg != op2_reg) { + if (IR_IS_TYPE_INT(type)) { + ir_emit_mov(ctx, type, def_reg, op2_reg); + } else { + ir_emit_fp_mov(ctx, type, def_reg, op2_reg); + } + } + } else { + ir_emit_load_ex(ctx, type, def_reg, op2, def); + } + + if (IR_REG_SPILLED(ctx->regs[def][0])) { + ir_emit_store(ctx, type, def, def_reg); + } + return; + } + + if (op1_reg != IR_REG_NONE) { + ir_reg op1_reg_hi; + + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + op1_reg_hi = IR_REG_I64_HI(op1_reg); + op1_reg = IR_REG_I64_LO(op1_reg); + ir_emit_load_i64_lo(ctx, op1_reg, op1); + ir_emit_load_i64_hi(ctx, op1_reg_hi, op1); + } else { + op1_reg_hi = IR_REG_I64_HI(op1_reg); + op1_reg = IR_REG_I64_LO(op1_reg); + } + | mov Rd(tmp_reg), op1_reg + | or Rd(tmp_reg), op1_reg_hi + } else { + ir_mem mem, mem_hi; + + if (ir_rule(ctx, insn->op1) & IR_FUSED) { + mem = ir_fuse_load(ctx, def, insn->op1); + } else { + mem = ir_ref_spill_slot(ctx, insn->op1); + } + mem_hi = IR_MEM_I64_HI(mem); + + | ASM_REG_MEM_OP mov, IR_U32, tmp_reg, mem + | ASM_REG_MEM_OP or, IR_U32, tmp_reg, mem_hi + } + + if (IR_IS_TYPE_INT(type)) { + IR_ASSERT(op2_reg != IR_REG_NONE || op3_reg != IR_REG_NONE); + if (op3_reg != IR_REG_NONE) { + if (op3_reg == def_reg) { + IR_ASSERT(op2_reg != IR_REG_NONE); + | ASM_REG_REG_OP2 cmovne, type, def_reg, op2_reg + } else { + if (op2_reg != IR_REG_NONE) { + if (def_reg != op2_reg) { + if (IR_IS_TYPE_INT(type)) { + ir_emit_mov(ctx, type, def_reg, op2_reg); + } else { + ir_emit_fp_mov(ctx, type, def_reg, op2_reg); + } + } + } else if (IR_IS_CONST_REF(op2) && !IR_IS_SYM_CONST(ctx->ir_base[op2].op)) { + /* prevent "xor" and flags clobbering */ + ir_emit_mov_imm_int(ctx, type, def_reg, ctx->ir_base[op2].val.i64); + } else { + ir_emit_load_ex(ctx, type, def_reg, op2, def); + } + | ASM_REG_REG_OP2 cmove, type, def_reg, op3_reg + } + } else { + IR_ASSERT(op2_reg != IR_REG_NONE && op2_reg != def_reg); + if (IR_IS_CONST_REF(op3) && !IR_IS_SYM_CONST(ctx->ir_base[op3].op)) { + /* prevent "xor" and flags clobbering */ + ir_emit_mov_imm_int(ctx, type, def_reg, ctx->ir_base[op3].val.i64); + } else { + ir_emit_load_ex(ctx, type, def_reg, op3, def); + } + | ASM_REG_REG_OP2 cmovne, type, def_reg, op2_reg + } + } else { + | je >2 + if (op2_reg != IR_REG_NONE) { + if (def_reg != op2_reg) { + ir_emit_fp_mov(ctx, type, def_reg, op2_reg); + } + } else { + ir_emit_load_ex(ctx, type, def_reg, op2, def); + } + | jmp >3 + |2: + if (op3_reg != IR_REG_NONE) { + if (def_reg != op3_reg) { + ir_emit_fp_mov(ctx, type, def_reg, op3_reg); + } + } else { + ir_emit_load_ex(ctx, type, def_reg, op3, def); + } + |3: + } + + if (IR_REG_SPILLED(ctx->regs[def][0])) { + ir_emit_store(ctx, type, def, def_reg); + } +} + static void ir_emit_cond_cmp_i64(ir_ctx *ctx, ir_ref def, ir_insn *insn) { ir_backend_data *data = ctx->data; @@ -18059,7 +18403,6 @@ static bool ir_emit_guard_cmp_i64(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn * static void ir_emit_return_i64(ir_ctx *ctx, ir_ref ref, ir_insn *insn) { ir_backend_data *data = ctx->data; - dasm_State **Dst = &data->dasm_state; ir_reg ret_reg = data->ra_data.cc->int_ret_reg; ir_reg ret2_reg = data->ra_data.cc->int_ret2_reg; ir_reg op2_reg = ctx->regs[ref][2]; @@ -18068,12 +18411,7 @@ static void ir_emit_return_i64(ir_ctx *ctx, ir_ref ref, ir_insn *insn) if (op2_reg != IR_REG_NONE && !IR_REG_SPILLED(op2_reg)) { op2_reg_hi = IR_REG_I64_HI(op2_reg); op2_reg = IR_REG_I64_LO(op2_reg); - if (op2_reg != ret_reg) { - | mov Rd(ret_reg), Rd(op2_reg) - } - if (op2_reg_hi != ret2_reg) { - | mov Rd(ret2_reg), Rd(op2_reg_hi) - } + ir_emit_mov_i64_pair(ctx, ret_reg, ret2_reg, op2_reg, op2_reg_hi); } else { ir_emit_load_i64_lo(ctx, ret_reg, insn->op2); ir_emit_load_i64_hi(ctx, ret2_reg, insn->op2); @@ -19350,7 +19688,6 @@ static void ir_emit_vector_shuffle_avx(ir_ctx *ctx, ir_ref def, ir_insn *insn, u ir_reg op1_reg = ctx->regs[def][1]; ir_reg op2_reg = ctx->regs[def][2]; ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]); - ir_reg tmp_reg = IR_REG_NONE; uint32_t mask; if (op2_reg == IR_REG_NONE && insn->op1 == insn->op2) { @@ -19428,22 +19765,18 @@ static void ir_emit_vector_shuffle_avx(ir_ctx *ctx, ir_ref def, ir_insn *insn, u ir_emit_vshufps(ctx, def_reg, def_reg, op2_reg, mask >> 8); break; case IR_SHUFPS_1_21: - tmp_reg = ctx->tmp_regs[def]; - IR_ASSERT(tmp_reg != IR_REG_NONE); op1_reg = ir_emit_load_reg(ctx, type, op1_reg, insn->op1); op2_reg = ir_emit_load_reg(ctx, type, op2_reg, insn->op2); mask = ir_shufps_mask_n_12(ctx, insn->op3); - ir_emit_vshufps(ctx, tmp_reg, op1_reg, op2_reg, mask & 0xff); - ir_emit_vshufps(ctx, def_reg, op1_reg, tmp_reg, mask >> 8); + ir_emit_vshufps(ctx, def_reg, op1_reg, op2_reg, mask & 0xff); + ir_emit_vshufps(ctx, def_reg, op1_reg, def_reg, mask >> 8); break; case IR_SHUFPS_2_12: - tmp_reg = ctx->tmp_regs[def]; - IR_ASSERT(tmp_reg != IR_REG_NONE); op1_reg = ir_emit_load_reg(ctx, type, op1_reg, insn->op1); op2_reg = ir_emit_load_reg(ctx, type, op2_reg, insn->op2); mask = ir_shufps_mask_n_12(ctx, insn->op3); - ir_emit_vshufps(ctx, tmp_reg, op1_reg, op2_reg, mask & 0xff); - ir_emit_vshufps(ctx, def_reg, op2_reg, tmp_reg, mask >> 8); + ir_emit_vshufps(ctx, def_reg, op1_reg, op2_reg, mask & 0xff); + ir_emit_vshufps(ctx, def_reg, op2_reg, def_reg, mask >> 8); break; case IR_BLENDPS_12: op1_reg = ir_emit_load_reg(ctx, type, op1_reg, insn->op1); @@ -24478,7 +24811,7 @@ static void ir_emit_vector_fp2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (op1_reg != IR_REG_NONE) { if (IR_REG_SPILLED(op1_reg)) { op1_reg = IR_REG_NUM(op1_reg); - ir_emit_load(ctx, src_type, op1_reg, insn->op1); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); } if (src_type == dst_type) { if (op1_reg != def_reg) { @@ -24554,7 +24887,7 @@ static void ir_emit_vector_fp2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) } if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } @@ -24580,12 +24913,17 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_FP(src_type) && IR_IS_TYPE_INT(dst_type)); IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE); + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); + } + if (src_type == IR_FLOAT) { if (IR_IS_TYPE_SIGNED(dst_type)) { if (dst_type == IR_I8) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) if (ctx->mflags & IR_X86_AVX2) { | vextracti128 xmm(tmp1_reg-IR_REG_FP_FIRST), ymm(def_reg-IR_REG_FP_FIRST), 1 } else { @@ -24595,20 +24933,20 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | vpacksswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpackssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) | vpacksswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | packssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) | packsswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_I16) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) if (ctx->mflags & IR_X86_AVX2) { | vextracti128 xmm(tmp1_reg-IR_REG_FP_FIRST), ymm(def_reg-IR_REG_FP_FIRST), 1 } else { @@ -24617,25 +24955,25 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | vpackssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(tmp1_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpackssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | packssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_I32) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_I64) { #if defined(IR_TARGET_X64) @@ -24733,7 +25071,7 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (dst_type == IR_U8) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) if (ctx->mflags & IR_X86_AVX2) { | vextracti128 xmm(tmp1_reg-IR_REG_FP_FIRST), ymm(def_reg-IR_REG_FP_FIRST), 1 } else { @@ -24743,20 +25081,20 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpackusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | packusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) | packuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_U16) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) if (ctx->mflags & IR_X86_AVX2) { | vextracti128 xmm(tmp1_reg-IR_REG_FP_FIRST), ymm(def_reg-IR_REG_FP_FIRST), 1 } else { @@ -24765,12 +25103,12 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) | vpackusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(tmp1_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpackusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttps2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | packusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_U32) { @@ -25008,47 +25346,47 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (dst_type == IR_I8) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtpd2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) | vpackssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) | vpacksswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { - | vcvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 | vpacksswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) IR_ASSERT(src_width <= 16); } } else { IR_ASSERT(src_width <= 16); - | cvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | pshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 | packsswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_I16) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtpd2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) | vpackssdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 } } else { IR_ASSERT(src_width <= 16); - | cvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | pshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 } } else if (dst_type == IR_I32) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtpd2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_I64) { #if defined(IR_TARGET_X64) @@ -25142,34 +25480,34 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) if (dst_type == IR_U8) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtpd2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) | vpackusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) - | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 - | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vpackuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else { IR_ASSERT(src_width <= 16); - | cvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | pshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 - | packuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | packuswb xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } } else if (dst_type == IR_U16) { if (ctx->mflags & IR_X86_AVX) { if (src_width == 32) { - | vcvtpd2dq ymm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), ymm(op1_reg-IR_REG_FP_FIRST) | vpackusdw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST) } else { IR_ASSERT(src_width <= 16); - | vcvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | vcvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | vpshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 } } else { IR_ASSERT(src_width <= 16); - | cvtpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) + | cvttpd2dq xmm(def_reg-IR_REG_FP_FIRST), xmm(op1_reg-IR_REG_FP_FIRST) | pshuflw xmm(def_reg-IR_REG_FP_FIRST), xmm(def_reg-IR_REG_FP_FIRST), 232 } } else if (dst_type == IR_U32) { @@ -25389,7 +25727,7 @@ static void ir_emit_vector_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn) } if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } @@ -25415,6 +25753,11 @@ static void ir_emit_vector_int2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) IR_ASSERT(IR_IS_TYPE_INT(src_type) && IR_IS_TYPE_FP(dst_type)); IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE); + if (IR_REG_SPILLED(op1_reg)) { + op1_reg = IR_REG_NUM(op1_reg); + ir_emit_load(ctx, ctx->ir_base[insn->op1].type, op1_reg, insn->op1); + } + if (dst_type == IR_FLOAT) { if (IR_IS_TYPE_SIGNED(src_type)) { if (src_type == IR_I8) { @@ -26217,7 +26560,7 @@ static void ir_emit_vector_int2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn) } if (IR_REG_SPILLED(ctx->regs[def][0])) { - ir_emit_store(ctx, dst_type, def, def_reg); + ir_emit_store(ctx, insn->type, def, def_reg); } } #endif @@ -27347,6 +27690,9 @@ void *ir_emit_code(ir_ctx *ctx, size_t *size_ptr) case IR_OP_I64: ir_emit_op_i64(ctx, i, insn); break; + case IR_BOOL_NOT_I64: + ir_emit_bool_not_i64(ctx, i, insn); + break; case IR_SEXT_I64: ir_emit_sext_i64(ctx, i, insn); break; @@ -27377,6 +27723,9 @@ void *ir_emit_code(ir_ctx *ctx, size_t *size_ptr) case IR_COND_I64: ir_emit_cond_i64(ctx, i, insn); break; + case IR_COND_TEST_I64: + ir_emit_cond_test_i64(ctx, i, insn); + break; case IR_COND_I64_CMP_INT: ir_emit_cond_i64_cmp_int(ctx, i, insn); break; From 8fdb87ce7f3a07a92c3eeb81df4e1a8ad0b057cc Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 30 Nov 2026 23:07:20 +0100 Subject: [PATCH 2/3] Update the bundled IR revision to match the sources --- .github/scripts/download-bundled/jit-ir.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/download-bundled/jit-ir.sh b/.github/scripts/download-bundled/jit-ir.sh index 8ca8235c2f1b..bb3fb14ec761 100755 --- a/.github/scripts/download-bundled/jit-ir.sh +++ b/.github/scripts/download-bundled/jit-ir.sh @@ -5,7 +5,7 @@ cd "$(dirname "$0")/../../.." tmp_dir=/tmp/php-src-download-bundled/jit-ir rm -rf "$tmp_dir" -revision=281a66a87aedc4444ca09f696b9b8e9dab89ad32 +revision=b8a6cee5bbffea2299756a932b2388345dacd5ba git clone --depth 1 --revision="$revision" https://github.com/dstogov/ir.git "$tmp_dir" From 3568077687e412f180deecc2dfded339c4b4053b Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Mon, 30 Nov 2026 23:07:44 +0100 Subject: [PATCH 3/3] Exclude IR llvm_tests from the bundled sources --- .github/scripts/download-bundled/jit-ir.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/scripts/download-bundled/jit-ir.sh b/.github/scripts/download-bundled/jit-ir.sh index bb3fb14ec761..13560e58fd67 100755 --- a/.github/scripts/download-bundled/jit-ir.sh +++ b/.github/scripts/download-bundled/jit-ir.sh @@ -22,6 +22,7 @@ rm -r examples rm -r tests rm -r tools rm -r fuzz +rm -r llvm_tests rm README.md rm TODO rm ir.g