From 1d2732a0e33259e73a2d8059fb5f68e359144ef6 Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 8 Oct 2015 11:21:16 +0200 Subject: [PATCH 1/2] HSA: encapsulate type conversion constructs gcc/ChangeLog: 2015-10-08 Martin Liska * hsa-gen.c (hsa_op_with_type::get_in_type): New function. (gen_hsa_insns_for_switch_stmt): Use it. (gen_set_num_threads): Dtto. (gen_hsa_insns_for_known_library_call): Dtto. * hsa.h (hsa_op_with_type::get_in_type): Declarate the function. --- gcc/hsa-gen.c | 64 +++++++++++++++++++++++++++++------------------------------ gcc/hsa.h | 4 ++++ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/gcc/hsa-gen.c b/gcc/hsa-gen.c index 8f707b5..ab4917b 100644 --- a/gcc/hsa-gen.c +++ b/gcc/hsa-gen.c @@ -795,6 +795,34 @@ hsa_op_with_type::hsa_op_with_type (BrigKind16_t k, BrigType16_t t) type = t; } +hsa_op_with_type * +hsa_op_with_type::get_in_type (BrigType16_t dtype, hsa_bb *hbb) +{ + if (type == dtype) + return this; + + hsa_op_reg *dest; + + if (hsa_needs_cvt (dtype, type)) + { + dest = new hsa_op_reg (dtype); + hbb->append_insn (new hsa_insn_basic (2, BRIG_OPCODE_CVT, + dest->type, dest, this)); + } + else + { + dest = new hsa_op_reg (type); + hbb->append_insn (new hsa_insn_basic (2, BRIG_OPCODE_MOV, + dest->type, dest, this)); + + /* We cannot simply for instance: 'mov_u32 $_3, 48 (s32)' because + type of the operand must be same as type of the instruction. */ + dest->type = dtype; + } + + return dest; +} + /* Constructor of class representing HSA immediate values. TREE_VAL is the tree representation of the immediate value. If min32int is true, always expand integer types to one that has at least 32 bits. */ @@ -3016,16 +3044,8 @@ gen_hsa_insns_for_switch_stmt (gswitch *s, hsa_bb *hbb, sub_index, index, new hsa_op_immed (lowest))); - if (hsa_needs_cvt (BRIG_TYPE_U64, sub_index->type)) - { - hsa_op_reg *sub_index_cvt = new hsa_op_reg (BRIG_TYPE_U64); - hbb->append_insn (new hsa_insn_basic (2, BRIG_OPCODE_CVT, - sub_index_cvt->type, - sub_index_cvt, sub_index)); - - sub_index = sub_index_cvt; - } - + hsa_op_base *tmp = sub_index->get_in_type (BRIG_TYPE_U64, hbb); + sub_index = as_a (tmp); unsigned labels = gimple_switch_num_labels (s); unsigned HOST_WIDE_INT size = tree_to_uhwi (get_switch_size (s)); @@ -3251,17 +3271,7 @@ gen_set_num_threads (tree value, hsa_bb *hbb, vec *ssa_map) hsa_op_with_type *src = hsa_reg_or_immed_for_gimple_op (value, hbb, ssa_map); - BrigType16_t dtype = hsa_num_threads->type; - if (hsa_needs_cvt (dtype, src->type)) - { - hsa_op_reg *tmp = new hsa_op_reg (dtype); - hbb->append_insn (new hsa_insn_basic (2, BRIG_OPCODE_CVT, tmp->type, - tmp, src)); - src = tmp; - } - else - src->type = dtype; - + src = src->get_in_type (hsa_num_threads->type, hbb); hsa_op_address *addr = new hsa_op_address (hsa_num_threads); hsa_op_immed *limit = new hsa_op_immed (64, BRIG_TYPE_U32); @@ -3394,17 +3404,7 @@ gen_hsa_insns_for_known_library_call (gimple *stmt, hsa_bb *hbb, hsa_op_with_type *src = hsa_reg_or_immed_for_gimple_op (rhs1, hbb, ssa_map); - BrigType16_t dtype = BRIG_TYPE_U64; - if (hsa_needs_cvt (dtype, src->type)) - { - hsa_op_reg *tmp = new hsa_op_reg (dtype); - hbb->append_insn (new hsa_insn_basic (2, BRIG_OPCODE_CVT, - tmp->type, tmp, src)); - src = tmp; - } - else - src->type = dtype; - + src = src->get_in_type (BRIG_TYPE_U64, hbb); set_debug_value (hbb, src); return true; } diff --git a/gcc/hsa.h b/gcc/hsa.h index 86adaa5..89d339f 100644 --- a/gcc/hsa.h +++ b/gcc/hsa.h @@ -120,6 +120,10 @@ public: /* The type. */ BrigType16_t type; + /* Convert an operand to a destination type DTYPE and attach insns + to HBB if needed. */ + hsa_op_with_type *get_in_type (BrigType16_t dtype, hsa_bb *hbb); + protected: hsa_op_with_type (BrigKind16_t k, BrigType16_t t); private: -- 2.6.0