From e3dff709b75c83c3939727538aa0bd072c268687 Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Sun, 5 Apr 2020 14:24:00 +0100 Subject: [PATCH] * src/comp.c: Emit cast only when necessary. Coerce only when the destination type is different from the current one. --- src/comp.c | 89 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/src/comp.c b/src/comp.c index 605e92680c7..75a2534b2ee 100644 --- a/src/comp.c +++ b/src/comp.c @@ -605,12 +605,17 @@ emit_cond_jump (gcc_jit_rvalue *test, } static gcc_jit_rvalue * -emit_cast (gcc_jit_type *new_type, gcc_jit_rvalue *obj) +emit_coerce (gcc_jit_type *new_type, gcc_jit_rvalue *obj) { static ptrdiff_t i; + gcc_jit_type *old_type = gcc_jit_rvalue_get_type (obj); + + if (new_type == old_type) + return obj; + gcc_jit_field *orig_field = - type_to_cast_field (gcc_jit_rvalue_get_type (obj)); + type_to_cast_field (old_type); gcc_jit_field *dest_field = type_to_cast_field (new_type); gcc_jit_lvalue *tmp_u = @@ -667,7 +672,7 @@ emit_rvalue_from_long_long (long long n) 32)); return - emit_cast (comp.long_long_type, + emit_coerce (comp.long_long_type, gcc_jit_context_new_binary_op ( comp.ctxt, NULL, @@ -729,17 +734,17 @@ emit_ptr_arithmetic (gcc_jit_rvalue *ptr, gcc_jit_type *ptr_type, gcc_jit_context_new_rvalue_from_int (comp.ctxt, comp.uintptr_type, size_of_ptr_ref), - emit_cast (comp.uintptr_type, i)); + emit_coerce (comp.uintptr_type, i)); return - emit_cast ( + emit_coerce ( ptr_type, gcc_jit_context_new_binary_op ( comp.ctxt, NULL, GCC_JIT_BINARY_OP_PLUS, comp.uintptr_type, - emit_cast (comp.uintptr_type, ptr), + emit_coerce (comp.uintptr_type, ptr), offset)); } @@ -785,7 +790,7 @@ emit_XUNTAG (gcc_jit_rvalue *a, gcc_jit_type *type, long long lisp_word_tag) emit_comment ("XUNTAG"); #ifndef WIDE_EMACS_INT - return emit_cast ( + return emit_coerce ( gcc_jit_type_get_pointer (type), gcc_jit_context_new_binary_op ( comp.ctxt, @@ -798,7 +803,7 @@ emit_XUNTAG (gcc_jit_rvalue *a, gcc_jit_type *type, long long lisp_word_tag) comp.emacs_int_type, lisp_word_tag))); #else - return emit_cast ( + return emit_coerce ( gcc_jit_type_get_pointer (type), gcc_jit_context_new_binary_op ( comp.ctxt, @@ -806,9 +811,9 @@ emit_XUNTAG (gcc_jit_rvalue *a, gcc_jit_type *type, long long lisp_word_tag) GCC_JIT_BINARY_OP_MINUS, comp.unsigned_long_long_type, /* FIXME Should be XLP. */ - emit_cast (comp.unsigned_long_long_type, emit_XLI (a)), - emit_cast (comp.unsigned_long_long_type, - emit_rvalue_from_long_long (lisp_word_tag)))); + emit_coerce (comp.unsigned_long_long_type, emit_XLI (a)), + emit_coerce (comp.unsigned_long_long_type, + emit_rvalue_from_long_long (lisp_word_tag)))); #endif } @@ -859,7 +864,7 @@ emit_TAGGEDP (gcc_jit_rvalue *obj, ptrdiff_t tag) NULL, GCC_JIT_BINARY_OP_MINUS, comp.unsigned_type, - emit_cast (comp.unsigned_type, sh_res), + emit_coerce (comp.unsigned_type, sh_res), gcc_jit_context_new_rvalue_from_int ( comp.ctxt, comp.unsigned_type, @@ -951,7 +956,7 @@ emit_FIXNUMP (gcc_jit_rvalue *obj) NULL, GCC_JIT_BINARY_OP_MINUS, comp.unsigned_type, - emit_cast (comp.unsigned_type, sh_res), + emit_coerce (comp.unsigned_type, sh_res), gcc_jit_context_new_rvalue_from_int ( comp.ctxt, comp.unsigned_type, @@ -980,7 +985,7 @@ static gcc_jit_rvalue * emit_XFIXNUM (gcc_jit_rvalue *obj) { emit_comment ("XFIXNUM"); - gcc_jit_rvalue *i = emit_cast (comp.emacs_uint_type, emit_XLI (obj)); + gcc_jit_rvalue *i = emit_coerce (comp.emacs_uint_type, emit_XLI (obj)); if (!USE_LSB_TAG) { @@ -988,7 +993,7 @@ emit_XFIXNUM (gcc_jit_rvalue *obj) NULL, GCC_JIT_BINARY_OP_LSHIFT, comp.emacs_uint_type, - emit_cast (comp.emacs_uint_type, i), + emit_coerce (comp.emacs_uint_type, i), comp.inttypebits); return gcc_jit_context_new_binary_op (comp.ctxt, @@ -1016,8 +1021,8 @@ emit_INTEGERP (gcc_jit_rvalue *obj) NULL, GCC_JIT_BINARY_OP_LOGICAL_OR, comp.bool_type, - emit_cast (comp.bool_type, - emit_FIXNUMP (obj)), + emit_coerce (comp.bool_type, + emit_FIXNUMP (obj)), emit_BIGNUMP (obj)); } @@ -1031,8 +1036,8 @@ emit_NUMBERP (gcc_jit_rvalue *obj) GCC_JIT_BINARY_OP_LOGICAL_OR, comp.bool_type, emit_INTEGERP (obj), - emit_cast (comp.bool_type, - emit_FLOATP (obj))); + emit_coerce (comp.bool_type, + emit_FLOATP (obj))); } static gcc_jit_rvalue * @@ -1049,7 +1054,7 @@ emit_make_fixnum_LSB_TAG (gcc_jit_rvalue *n) NULL, GCC_JIT_BINARY_OP_LSHIFT, comp.emacs_int_type, - emit_cast (comp.emacs_uint_type, n), + emit_coerce (comp.emacs_uint_type, n), comp.inttypebits); tmp = gcc_jit_context_new_binary_op (comp.ctxt, @@ -1082,16 +1087,16 @@ emit_make_fixnum_MSB_TAG (gcc_jit_rvalue *n) */ gcc_jit_rvalue *intmask = - emit_cast (comp.emacs_uint_type, - emit_rvalue_from_long_long ((EMACS_INT_MAX - >> (INTTYPEBITS - 1)))); + emit_coerce (comp.emacs_uint_type, + emit_rvalue_from_long_long ((EMACS_INT_MAX + >> (INTTYPEBITS - 1)))); n = gcc_jit_context_new_binary_op ( comp.ctxt, NULL, GCC_JIT_BINARY_OP_BITWISE_AND, comp.emacs_uint_type, intmask, - emit_cast (comp.emacs_uint_type, n)); + emit_coerce (comp.emacs_uint_type, n)); n = gcc_jit_context_new_binary_op ( comp.ctxt, @@ -1103,12 +1108,12 @@ emit_make_fixnum_MSB_TAG (gcc_jit_rvalue *n) NULL, GCC_JIT_BINARY_OP_LSHIFT, comp.emacs_uint_type, - emit_cast (comp.emacs_uint_type, comp.lisp_int0), + emit_coerce (comp.emacs_uint_type, comp.lisp_int0), gcc_jit_context_new_rvalue_from_int (comp.ctxt, comp.emacs_uint_type, VALBITS)), n); - return emit_XLI (emit_cast (comp.emacs_int_type, n)); + return emit_XLI (emit_coerce (comp.emacs_int_type, n)); } @@ -1128,10 +1133,10 @@ emit_const_lisp_obj (Lisp_Object obj) SSDATA (Fprin1_to_string (obj, Qnil)))); if (NIL_IS_ZERO && EQ (obj, Qnil)) - return emit_cast (comp.lisp_obj_type, - gcc_jit_context_new_rvalue_from_ptr (comp.ctxt, - comp.void_ptr_type, - NULL)); + return emit_coerce (comp.lisp_obj_type, + gcc_jit_context_new_rvalue_from_ptr (comp.ctxt, + comp.void_ptr_type, + NULL)); imm_reloc_t reloc = obj_to_reloc (obj); return gcc_jit_lvalue_as_rvalue ( @@ -1321,10 +1326,10 @@ emit_PURE_P (gcc_jit_rvalue *ptr) NULL, GCC_JIT_BINARY_OP_MINUS, comp.uintptr_type, - emit_cast (comp.uintptr_type, ptr), - emit_cast (comp.uintptr_type, - gcc_jit_lvalue_as_rvalue ( - gcc_jit_rvalue_dereference (comp.pure_ref, NULL)))), + emit_coerce (comp.uintptr_type, ptr), + emit_coerce (comp.uintptr_type, + gcc_jit_lvalue_as_rvalue ( + gcc_jit_rvalue_dereference (comp.pure_ref, NULL)))), gcc_jit_context_new_rvalue_from_int (comp.ctxt, comp.uintptr_type, PURESIZE)); @@ -1358,7 +1363,7 @@ emit_mvar_val (Lisp_Object mvar) : gcc_jit_context_new_rvalue_from_long (comp.ctxt, comp.void_ptr_type, constant); - return emit_cast (comp.lisp_obj_type, word); + return emit_coerce (comp.lisp_obj_type, word); } /* Other const objects are fetched from the reloc array. */ return emit_const_lisp_obj (constant); @@ -1861,7 +1866,7 @@ static gcc_jit_rvalue * emit_consp (Lisp_Object insn) { gcc_jit_rvalue *x = emit_mvar_val (SECOND (insn)); - gcc_jit_rvalue *res = emit_cast (comp.bool_type, + gcc_jit_rvalue *res = emit_coerce (comp.bool_type, emit_CONSP (x)); return gcc_jit_context_new_call (comp.ctxt, NULL, @@ -2622,8 +2627,8 @@ define_CAR_CDR (void) GCC_JIT_BINARY_OP_LOGICAL_OR, comp.bool_type, gcc_jit_param_as_rvalue (param[1]), - emit_cast (comp.bool_type, - emit_CONSP (c))), + emit_coerce (comp.bool_type, + emit_CONSP (c))), is_cons_b, not_a_cons_b); comp.block = is_cons_b; @@ -2781,8 +2786,8 @@ define_add1_sub1 (void) GCC_JIT_BINARY_OP_LOGICAL_OR, comp.bool_type, gcc_jit_param_as_rvalue (param[1]), - emit_cast (comp.bool_type, - emit_FIXNUMP (n))); + emit_coerce (comp.bool_type, + emit_FIXNUMP (n))); emit_cond_jump ( gcc_jit_context_new_binary_op ( @@ -2865,8 +2870,8 @@ define_negate (void) GCC_JIT_BINARY_OP_LOGICAL_OR, comp.bool_type, gcc_jit_param_as_rvalue (param[1]), - emit_cast (comp.bool_type, - emit_FIXNUMP (gcc_jit_lvalue_as_rvalue (n)))); + emit_coerce (comp.bool_type, + emit_FIXNUMP (gcc_jit_lvalue_as_rvalue (n)))); emit_cond_jump ( gcc_jit_context_new_binary_op ( -- 2.39.5