1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-18 01:42:24 +02:00

Emit constant-vector-ref, constant-vector-set! for known small indices

* libguile/vm-engine.c (rtl_vm_engine): Add constant-vector-set!
  instruction and renumber.

* module/language/cps/compile-rtl.scm (emit-rtl-sequence): Emit
  constant-vector-ref and constant-vector-set! as appropriate.

* module/language/cps/dfg.scm (constant-needs-allocation?): In some
  cases, vector-ref and vector-set! don't need to allocate their index.
This commit is contained in:
Andy Wingo 2013-10-26 21:30:37 +02:00
parent fa3b6e57c2
commit 8ba3f20c47
3 changed files with 77 additions and 30 deletions

View file

@ -2932,6 +2932,31 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
NEXT (1); NEXT (1);
} }
/* constant-vector-set! dst:8 idx:8 src:8
*
* Store SRC into the vector DST at index IDX. Here IDX is an
* immediate value.
*/
VM_DEFINE_OP (93, constant_vector_set, "constant-vector-set!", OP1 (U8_U8_U8_U8))
{
scm_t_uint8 dst, idx, src;
SCM vect, val;
SCM_UNPACK_RTL_8_8_8 (op, dst, idx, src);
vect = LOCAL_REF (dst);
val = LOCAL_REF (src);
if (SCM_LIKELY (SCM_I_IS_NONWEAK_VECTOR (vect)
&& idx < SCM_I_VECTOR_LENGTH (vect)))
SCM_I_VECTOR_WELTS (vect)[idx] = val;
else
{
SYNC_IP ();
scm_vector_set_x (vect, scm_from_uint8 (idx), val);
}
NEXT (1);
}
@ -2943,7 +2968,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* *
* Store the vtable of SRC into DST. * Store the vtable of SRC into DST.
*/ */
VM_DEFINE_OP (93, struct_vtable, "struct-vtable", OP1 (U8_U12_U12) | OP_DST) VM_DEFINE_OP (94, struct_vtable, "struct-vtable", OP1 (U8_U12_U12) | OP_DST)
{ {
ARGS1 (obj); ARGS1 (obj);
VM_VALIDATE_STRUCT (obj, "struct_vtable"); VM_VALIDATE_STRUCT (obj, "struct_vtable");
@ -2956,7 +2981,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* will be constructed with space for NFIELDS fields, which should * will be constructed with space for NFIELDS fields, which should
* correspond to the field count of the VTABLE. * correspond to the field count of the VTABLE.
*/ */
VM_DEFINE_OP (94, allocate_struct, "allocate-struct", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (95, allocate_struct, "allocate-struct", OP1 (U8_U8_U8_U8) | OP_DST)
{ {
scm_t_uint8 dst, vtable, nfields; scm_t_uint8 dst, vtable, nfields;
SCM ret; SCM ret;
@ -2975,7 +3000,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* Fetch the item at slot IDX in the struct in SRC, and store it * Fetch the item at slot IDX in the struct in SRC, and store it
* in DST. * in DST.
*/ */
VM_DEFINE_OP (95, struct_ref, "struct-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (96, struct_ref, "struct-ref", OP1 (U8_U8_U8_U8) | OP_DST)
{ {
ARGS2 (obj, pos); ARGS2 (obj, pos);
@ -3009,7 +3034,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* *
* Store SRC into the struct DST at slot IDX. * Store SRC into the struct DST at slot IDX.
*/ */
VM_DEFINE_OP (96, struct_set, "struct-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (97, struct_set, "struct-set!", OP1 (U8_U8_U8_U8))
{ {
scm_t_uint8 dst, idx, src; scm_t_uint8 dst, idx, src;
SCM obj, pos, val; SCM obj, pos, val;
@ -3050,7 +3075,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* *
* Store the vtable of SRC into DST. * Store the vtable of SRC into DST.
*/ */
VM_DEFINE_OP (97, class_of, "class-of", OP1 (U8_U12_U12) | OP_DST) VM_DEFINE_OP (98, class_of, "class-of", OP1 (U8_U12_U12) | OP_DST)
{ {
ARGS1 (obj); ARGS1 (obj);
if (SCM_INSTANCEP (obj)) if (SCM_INSTANCEP (obj))
@ -3065,7 +3090,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* DST. Unlike struct-ref, IDX is an 8-bit immediate value, not an * DST. Unlike struct-ref, IDX is an 8-bit immediate value, not an
* index into the stack. * index into the stack.
*/ */
VM_DEFINE_OP (98, slot_ref, "slot-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (99, slot_ref, "slot-ref", OP1 (U8_U8_U8_U8) | OP_DST)
{ {
scm_t_uint8 dst, src, idx; scm_t_uint8 dst, src, idx;
SCM_UNPACK_RTL_8_8_8 (op, dst, src, idx); SCM_UNPACK_RTL_8_8_8 (op, dst, src, idx);
@ -3079,7 +3104,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* Store SRC into slot IDX of the struct in DST. Unlike struct-set!, * Store SRC into slot IDX of the struct in DST. Unlike struct-set!,
* IDX is an 8-bit immediate value, not an index into the stack. * IDX is an 8-bit immediate value, not an index into the stack.
*/ */
VM_DEFINE_OP (99, slot_set, "slot-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (100, slot_set, "slot-set!", OP1 (U8_U8_U8_U8))
{ {
scm_t_uint8 dst, idx, src; scm_t_uint8 dst, idx, src;
SCM_UNPACK_RTL_8_8_8 (op, dst, idx, src); SCM_UNPACK_RTL_8_8_8 (op, dst, idx, src);
@ -3100,7 +3125,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* from the instruction pointer, and store into DST. LEN is a byte * from the instruction pointer, and store into DST. LEN is a byte
* length. OFFSET is signed. * length. OFFSET is signed.
*/ */
VM_DEFINE_OP (100, load_typed_array, "load-typed-array", OP3 (U8_U8_U8_U8, N32, U32) | OP_DST) VM_DEFINE_OP (101, load_typed_array, "load-typed-array", OP3 (U8_U8_U8_U8, N32, U32) | OP_DST)
{ {
scm_t_uint8 dst, type, shape; scm_t_uint8 dst, type, shape;
scm_t_int32 offset; scm_t_int32 offset;
@ -3120,7 +3145,7 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
* *
* Make a new array with TYPE, FILL, and BOUNDS, storing it in DST. * Make a new array with TYPE, FILL, and BOUNDS, storing it in DST.
*/ */
VM_DEFINE_OP (101, make_array, "make-array", OP2 (U8_U12_U12, X8_U12_U12) | OP_DST) VM_DEFINE_OP (102, make_array, "make-array", OP2 (U8_U12_U12, X8_U12_U12) | OP_DST)
{ {
scm_t_uint16 dst, type, fill, bounds; scm_t_uint16 dst, type, fill, bounds;
SCM_UNPACK_RTL_12_12 (op, dst, type); SCM_UNPACK_RTL_12_12 (op, dst, type);
@ -3218,42 +3243,42 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \ RETURN (scm_bytevector_ ## fn_stem ## _native_ref (bv, idx)); \
} while (0) } while (0)
VM_DEFINE_OP (102, bv_u8_ref, "bv-u8-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (103, bv_u8_ref, "bv-u8-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FIXABLE_INT_REF (u8, u8, uint8, 1); BV_FIXABLE_INT_REF (u8, u8, uint8, 1);
VM_DEFINE_OP (103, bv_s8_ref, "bv-s8-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (104, bv_s8_ref, "bv-s8-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FIXABLE_INT_REF (s8, s8, int8, 1); BV_FIXABLE_INT_REF (s8, s8, int8, 1);
VM_DEFINE_OP (104, bv_u16_ref, "bv-u16-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (105, bv_u16_ref, "bv-u16-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2); BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2);
VM_DEFINE_OP (105, bv_s16_ref, "bv-s16-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (106, bv_s16_ref, "bv-s16-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FIXABLE_INT_REF (s16, s16_native, int16, 2); BV_FIXABLE_INT_REF (s16, s16_native, int16, 2);
VM_DEFINE_OP (106, bv_u32_ref, "bv-u32-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (107, bv_u32_ref, "bv-u32-ref", OP1 (U8_U8_U8_U8) | OP_DST)
#if SIZEOF_VOID_P > 4 #if SIZEOF_VOID_P > 4
BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4); BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4);
#else #else
BV_INT_REF (u32, uint32, 4); BV_INT_REF (u32, uint32, 4);
#endif #endif
VM_DEFINE_OP (107, bv_s32_ref, "bv-s32-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (108, bv_s32_ref, "bv-s32-ref", OP1 (U8_U8_U8_U8) | OP_DST)
#if SIZEOF_VOID_P > 4 #if SIZEOF_VOID_P > 4
BV_FIXABLE_INT_REF (s32, s32_native, int32, 4); BV_FIXABLE_INT_REF (s32, s32_native, int32, 4);
#else #else
BV_INT_REF (s32, int32, 4); BV_INT_REF (s32, int32, 4);
#endif #endif
VM_DEFINE_OP (108, bv_u64_ref, "bv-u64-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (109, bv_u64_ref, "bv-u64-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_INT_REF (u64, uint64, 8); BV_INT_REF (u64, uint64, 8);
VM_DEFINE_OP (109, bv_s64_ref, "bv-s64-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (110, bv_s64_ref, "bv-s64-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_INT_REF (s64, int64, 8); BV_INT_REF (s64, int64, 8);
VM_DEFINE_OP (110, bv_f32_ref, "bv-f32-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (111, bv_f32_ref, "bv-f32-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FLOAT_REF (f32, ieee_single, float, 4); BV_FLOAT_REF (f32, ieee_single, float, 4);
VM_DEFINE_OP (111, bv_f64_ref, "bv-f64-ref", OP1 (U8_U8_U8_U8) | OP_DST) VM_DEFINE_OP (112, bv_f64_ref, "bv-f64-ref", OP1 (U8_U8_U8_U8) | OP_DST)
BV_FLOAT_REF (f64, ieee_double, double, 8); BV_FLOAT_REF (f64, ieee_double, double, 8);
/* bv-u8-set! dst:8 idx:8 src:8 /* bv-u8-set! dst:8 idx:8 src:8
@ -3357,42 +3382,42 @@ RTL_VM_NAME (SCM vm, SCM program, SCM *argv, size_t nargs_)
NEXT (1); \ NEXT (1); \
} while (0) } while (0)
VM_DEFINE_OP (112, bv_u8_set, "bv-u8-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (113, bv_u8_set, "bv-u8-set!", OP1 (U8_U8_U8_U8))
BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1); BV_FIXABLE_INT_SET (u8, u8, uint8, 0, SCM_T_UINT8_MAX, 1);
VM_DEFINE_OP (113, bv_s8_set, "bv-s8-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (114, bv_s8_set, "bv-s8-set!", OP1 (U8_U8_U8_U8))
BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1); BV_FIXABLE_INT_SET (s8, s8, int8, SCM_T_INT8_MIN, SCM_T_INT8_MAX, 1);
VM_DEFINE_OP (114, bv_u16_set, "bv-u16-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (115, bv_u16_set, "bv-u16-set!", OP1 (U8_U8_U8_U8))
BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2); BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2);
VM_DEFINE_OP (115, bv_s16_set, "bv-s16-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (116, bv_s16_set, "bv-s16-set!", OP1 (U8_U8_U8_U8))
BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2); BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2);
VM_DEFINE_OP (116, bv_u32_set, "bv-u32-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (117, bv_u32_set, "bv-u32-set!", OP1 (U8_U8_U8_U8))
#if SIZEOF_VOID_P > 4 #if SIZEOF_VOID_P > 4
BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4); BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4);
#else #else
BV_INT_SET (u32, uint32, 4); BV_INT_SET (u32, uint32, 4);
#endif #endif
VM_DEFINE_OP (117, bv_s32_set, "bv-s32-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (118, bv_s32_set, "bv-s32-set!", OP1 (U8_U8_U8_U8))
#if SIZEOF_VOID_P > 4 #if SIZEOF_VOID_P > 4
BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4); BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4);
#else #else
BV_INT_SET (s32, int32, 4); BV_INT_SET (s32, int32, 4);
#endif #endif
VM_DEFINE_OP (118, bv_u64_set, "bv-u64-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (119, bv_u64_set, "bv-u64-set!", OP1 (U8_U8_U8_U8))
BV_INT_SET (u64, uint64, 8); BV_INT_SET (u64, uint64, 8);
VM_DEFINE_OP (119, bv_s64_set, "bv-s64-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (120, bv_s64_set, "bv-s64-set!", OP1 (U8_U8_U8_U8))
BV_INT_SET (s64, int64, 8); BV_INT_SET (s64, int64, 8);
VM_DEFINE_OP (120, bv_f32_set, "bv-f32-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (121, bv_f32_set, "bv-f32-set!", OP1 (U8_U8_U8_U8))
BV_FLOAT_SET (f32, ieee_single, float, 4); BV_FLOAT_SET (f32, ieee_single, float, 4);
VM_DEFINE_OP (121, bv_f64_set, "bv-f64-set!", OP1 (U8_U8_U8_U8)) VM_DEFINE_OP (122, bv_f64_set, "bv-f64-set!", OP1 (U8_U8_U8_U8))
BV_FLOAT_SET (f64, ieee_double, double, 8); BV_FLOAT_SET (f64, ieee_double, double, 8);
END_DISPATCH_SWITCH; END_DISPATCH_SWITCH;

View file

@ -186,6 +186,14 @@
(emit-resolve asm dst (constant bound?) (slot name))) (emit-resolve asm dst (constant bound?) (slot name)))
(($ $primcall 'free-ref (closure idx)) (($ $primcall 'free-ref (closure idx))
(emit-free-ref asm dst (slot closure) (constant idx))) (emit-free-ref asm dst (slot closure) (constant idx)))
(($ $primcall 'vector-ref (vector index))
(call-with-values (lambda ()
(lookup-maybe-constant-value index allocation))
(lambda (has-const? index-val)
(if (and has-const? (integer? index-val) (exact? index-val)
(<= 0 index-val 255))
(emit-constant-vector-ref asm dst (slot vector) index-val)
(emit-vector-ref asm dst (slot vector) (slot index))))))
(($ $primcall name args) (($ $primcall name args)
;; FIXME: Inline all the cases. ;; FIXME: Inline all the cases.
(let ((inst (prim-rtl-instruction name))) (let ((inst (prim-rtl-instruction name)))
@ -217,7 +225,15 @@
(($ $primcall 'struct-set! (struct index value)) (($ $primcall 'struct-set! (struct index value))
(emit-struct-set! asm (slot struct) (slot index) (slot value))) (emit-struct-set! asm (slot struct) (slot index) (slot value)))
(($ $primcall 'vector-set! (vector index value)) (($ $primcall 'vector-set! (vector index value))
(emit-vector-set! asm (slot vector) (slot index) (slot value))) (call-with-values (lambda ()
(lookup-maybe-constant-value index allocation))
(lambda (has-const? index-val)
(if (and has-const? (integer? index-val) (exact? index-val)
(<= 0 index-val 255))
(emit-constant-vector-set! asm (slot vector) index-val
(slot value))
(emit-vector-set! asm (slot vector) (slot index)
(slot value))))))
(($ $primcall 'variable-set! (var val)) (($ $primcall 'variable-set! (var val))
(emit-box-set! asm (slot var) (slot val))) (emit-box-set! asm (slot var) (slot val)))
(($ $primcall 'set-car! (pair value)) (($ $primcall 'set-car! (pair value))

View file

@ -801,6 +801,12 @@
#f) #f)
(($ $primcall 'resolve (name bound?)) (($ $primcall 'resolve (name bound?))
(eq? sym name)) (eq? sym name))
(($ $primcall 'vector-ref (v i))
(not (and (eq? sym i)
(integer? val) (exact? val) (<= 0 val 255))))
(($ $primcall 'vector-set! (v i x))
(not (and (eq? sym i)
(integer? val) (exact? val) (<= 0 val 255))))
(_ #t))) (_ #t)))
uses)))))) uses))))))