1 /* brig-code-entry-handler.h -- a gccbrig base class
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
4 for General Processor Tech.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_BRIG_CODE_ENTRY_HANDLER_H
23 #define GCC_BRIG_CODE_ENTRY_HANDLER_H
25 #include "brig-to-generic.h"
30 class tree_element_unary_visitor
;
32 /* An interface to organize the different types of element handlers
33 for the code section. */
35 class brig_code_entry_handler
: public brig_entry_handler
38 typedef std::map
<std::pair
<BrigOpcode16_t
, BrigType16_t
>, tree
> builtin_map
;
40 brig_code_entry_handler (brig_to_generic
&parent
);
42 /* Handles the brig_code data at the given pointer and adds it to the
43 currently built tree. Returns the number of consumed bytes. */
45 virtual size_t operator () (const BrigBase
*base
) = 0;
47 void append_statement (tree stmt
);
51 tree
get_tree_expr_type_for_hsa_type (BrigType16_t brig_type
) const;
52 tree
get_tree_cst_for_hsa_operand (const BrigOperandConstantBytes
*brigConst
,
54 tree
get_builtin_for_hsa_opcode (tree type
, BrigOpcode16_t brig_opcode
,
55 BrigType16_t brig_type
) const;
56 tree
get_comparison_result_type (tree source_type
);
58 tree
build_code_ref (const BrigBase
&ref
);
60 tree
build_tree_operand (const BrigInstBase
&brig_inst
,
61 const BrigBase
&operand
,
62 tree operand_type
= NULL_TREE
,
63 bool is_input
= false);
65 tree
build_address_operand (const BrigInstBase
&brig_inst
,
66 const BrigOperandAddress
&addr_operand
);
68 tree
build_tree_operand_from_brig (const BrigInstBase
*brig_inst
,
69 tree operand_type
, size_t operand_index
);
71 tree
build_tree_cst_element (BrigType16_t element_type
,
72 const unsigned char *next_data
) const;
74 bool needs_workitem_context_data (BrigOpcode16_t brig_opcode
) const;
76 void unpack (tree value
, tree_stl_vec
&elements
);
77 tree
pack (tree_stl_vec
&elements
);
79 bool can_expand_builtin (BrigOpcode16_t brig_opcode
) const;
80 tree
expand_builtin (BrigOpcode16_t brig_opcode
, tree_stl_vec
&operands
);
82 tree
expand_or_call_builtin (BrigOpcode16_t brig_opcode
,
83 BrigType16_t brig_type
, tree arith_type
,
84 tree_stl_vec
&operands
);
86 tree
add_temp_var (std::string name
, tree expr
);
88 tree
build_f2h_conversion (tree source
);
89 tree
build_h2f_conversion (tree source
);
91 tree_stl_vec
build_operands (const BrigInstBase
&brig_inst
);
92 void analyze_operands (const BrigInstBase
&brig_inst
);
93 tree
build_output_assignment (const BrigInstBase
&brig_inst
, tree output
,
96 tree
apply_to_all_elements (tree_element_unary_visitor
&visitor
,
99 HOST_WIDE_INT
int_constant_value (tree node
);
101 tree
extend_int (tree input
, tree dest_type
, tree src_type
);
103 /* HSAIL-specific builtin functions not yet integrated to gcc. */
105 static builtin_map s_custom_builtins
;
109 tree_stl_vec
build_or_analyze_operands (const BrigInstBase
&brig_inst
,
113 /* Implement the Visitor software pattern for performing various actions on
114 elements of vector operands. This enables separating the vector element
115 traversal/extraction/packing code from whatever different actions are
116 performed to each element. */
118 class tree_element_unary_visitor
121 tree
operator () (brig_code_entry_handler
&handler
, tree operand
);
123 /* Performs an action to a single element, which can have originally
124 been a vector element or a scalar. */
126 virtual tree
visit_element (brig_code_entry_handler
&handler
, tree operand
)
130 class tree_element_binary_visitor
133 tree
operator () (brig_code_entry_handler
&handler
, tree operand0
,
136 /* Performs an action to a pair of elements, which can have originally
137 been a vector element or a scalar. */
139 virtual tree
visit_element (brig_code_entry_handler
&handler
, tree operand0
,
144 /* Visitor for flushing float elements to zero. */
146 class flush_to_zero
: public tree_element_unary_visitor
149 flush_to_zero (bool fp16
) : m_fp16 (fp16
)
153 virtual tree
visit_element (brig_code_entry_handler
&caller
, tree operand
);
157 /* True if the value should be flushed according to fp16 limits. */
162 /* Visitor for converting F16 elements to F32. */
164 class half_to_float
: public tree_element_unary_visitor
167 virtual tree
visit_element (brig_code_entry_handler
&caller
, tree operand
);
170 /* Visitor for converting F32 elements to F16. */
172 class float_to_half
: public tree_element_unary_visitor
175 virtual tree
visit_element (brig_code_entry_handler
&caller
, tree operand
);
178 /* A base class for instruction types that support floating point
181 operator () delegates to subclasses (template method pattern) in
182 type specific parts. */
184 class brig_inst_mod_handler
: public brig_code_entry_handler
187 brig_inst_mod_handler (brig_to_generic
&parent
)
188 : brig_code_entry_handler (parent
)
192 virtual size_t generate (const BrigBase
*base
);
193 virtual const BrigAluModifier8_t
*modifier (const BrigBase
*base
) const;
194 virtual const BrigRound8_t
*round (const BrigBase
*base
) const;
196 size_t operator () (const BrigBase
*base
);
199 class brig_directive_function_handler
: public brig_code_entry_handler
202 brig_directive_function_handler (brig_to_generic
&parent
)
203 : brig_code_entry_handler (parent
)
206 size_t operator () (const BrigBase
*base
);
209 class brig_directive_control_handler
: public brig_code_entry_handler
212 brig_directive_control_handler (brig_to_generic
&parent
)
213 : brig_code_entry_handler (parent
)
217 size_t operator () (const BrigBase
*base
);
220 class brig_directive_variable_handler
: public brig_code_entry_handler
223 brig_directive_variable_handler (brig_to_generic
&parent
)
224 : brig_code_entry_handler (parent
)
228 size_t operator () (const BrigBase
*base
);
230 tree
build_variable (const BrigDirectiveVariable
*brigVar
,
231 tree_code var_decltype
= VAR_DECL
);
233 size_t get_brig_var_alignment (const BrigDirectiveVariable
*brigVar
);
236 class brig_directive_fbarrier_handler
: public brig_code_entry_handler
239 brig_directive_fbarrier_handler (brig_to_generic
&parent
)
240 : brig_code_entry_handler (parent
)
244 size_t operator () (const BrigBase
*base
);
247 class brig_directive_label_handler
: public brig_code_entry_handler
250 brig_directive_label_handler (brig_to_generic
&parent
)
251 : brig_code_entry_handler (parent
)
255 size_t operator () (const BrigBase
*base
);
258 class brig_directive_comment_handler
: public brig_code_entry_handler
261 brig_directive_comment_handler (brig_to_generic
&parent
)
262 : brig_code_entry_handler (parent
)
266 size_t operator () (const BrigBase
*base
);
269 class brig_directive_arg_block_handler
: public brig_code_entry_handler
272 brig_directive_arg_block_handler (brig_to_generic
&parent
)
273 : brig_code_entry_handler (parent
)
277 size_t operator () (const BrigBase
*base
);
280 class brig_basic_inst_handler
: public brig_code_entry_handler
283 brig_basic_inst_handler (brig_to_generic
&parent
);
285 size_t operator () (const BrigBase
*base
);
288 tree
build_lower_element_broadcast (tree vec_operand
);
290 bool must_be_scalarized (const BrigInstBase
*brig_inst
,
291 tree instr_type
) const;
293 tree
build_inst_expr (BrigOpcode16_t brig_opcode
, BrigType16_t brig_type
,
294 tree arith_type
, tree_stl_vec
&operands
);
296 tree
build_shuffle (tree arith_type
, tree_stl_vec
&operands
);
297 tree
build_unpack (tree_stl_vec
&operands
);
298 tree
build_pack (tree_stl_vec
&operands
);
300 tree
build_unpack_lo_or_hi (BrigOpcode16_t brig_opcode
, tree arith_type
,
301 tree_stl_vec
&operands
);
303 tree_code
get_tree_code_for_hsa_opcode (BrigOpcode16_t brig_opcode
,
304 BrigType16_t brig_type
) const;
307 class brig_cvt_inst_handler
: public brig_inst_mod_handler
310 brig_cvt_inst_handler (brig_to_generic
&parent
)
311 : brig_inst_mod_handler (parent
)
315 virtual size_t generate (const BrigBase
*base
);
316 virtual const BrigAluModifier8_t
*modifier (const BrigBase
*base
) const;
317 virtual const BrigRound8_t
*round (const BrigBase
*base
) const;
320 class brig_branch_inst_handler
: public brig_code_entry_handler
323 brig_branch_inst_handler (brig_to_generic
&parent
)
324 : brig_code_entry_handler (parent
)
328 size_t operator () (const BrigBase
*base
);
331 class brig_mem_inst_handler
: public brig_code_entry_handler
334 brig_mem_inst_handler (brig_to_generic
&parent
)
335 : brig_code_entry_handler (parent
)
339 size_t operator () (const BrigBase
*base
);
342 tree
build_mem_access (const BrigInstBase
*brig_inst
, tree addr
, tree data
);
345 class brig_copy_move_inst_handler
: public brig_code_entry_handler
348 brig_copy_move_inst_handler (brig_to_generic
&parent
)
349 : brig_code_entry_handler (parent
)
353 size_t operator () (const BrigBase
*base
);
356 size_t handle_lda (const BrigInstBase
*base
);
359 class brig_atomic_inst_handler
: public brig_code_entry_handler
362 typedef std::map
<std::string
, tree
> atomic_builtins_map
;
365 brig_atomic_inst_handler (brig_to_generic
&parent
);
367 size_t operator () (const BrigBase
*base
);
370 size_t generate_tree (const BrigInstBase
&inst
,
371 BrigAtomicOperation8_t atomic_opcode
);
374 class brig_signal_inst_handler
: public brig_atomic_inst_handler
377 brig_signal_inst_handler (brig_to_generic
&parent
)
378 : brig_atomic_inst_handler (parent
)
381 size_t operator () (const BrigBase
*base
);
384 class brig_cmp_inst_handler
: public brig_code_entry_handler
387 brig_cmp_inst_handler (brig_to_generic
&parent
)
388 : brig_code_entry_handler (parent
)
392 size_t operator () (const BrigBase
*base
);
395 class brig_seg_inst_handler
: public brig_code_entry_handler
398 brig_seg_inst_handler (brig_to_generic
&parent
);
400 size_t operator () (const BrigBase
*base
);
403 class brig_lane_inst_handler
: public brig_code_entry_handler
406 brig_lane_inst_handler (brig_to_generic
&parent
);
408 size_t operator () (const BrigBase
*base
);
411 class brig_queue_inst_handler
: public brig_code_entry_handler
414 brig_queue_inst_handler (brig_to_generic
&parent
);
416 size_t operator () (const BrigBase
*base
);
419 class brig_directive_module_handler
: public brig_code_entry_handler
422 brig_directive_module_handler (brig_to_generic
&parent
)
423 : brig_code_entry_handler (parent
)
427 size_t operator () (const BrigBase
*base
);