1 /* brig-atomic-inst-handler.cc -- brig atomic instruction handling
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
4 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5 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/>. */
24 #include "brig-code-entry-handler.h"
25 #include "brig-util.h"
26 #include "fold-const.h"
27 #include "diagnostic.h"
28 #include "tree-pretty-print.h"
29 #include "print-tree.h"
31 #include "langhooks.h"
32 #include "gimple-expr.h"
33 #include "stringpool.h"
34 #include "brig-builtins.h"
36 brig_atomic_inst_handler::brig_atomic_inst_handler (brig_to_generic
&parent
)
37 : brig_code_entry_handler (parent
)
42 brig_atomic_inst_handler::generate_tree (const BrigInstBase
&inst
,
43 BrigAtomicOperation8_t atomic_opcode
)
45 tree_stl_vec operands
= build_operands (inst
);
47 = gccbrig_hsa_opcode_op_output_p (inst
.opcode
, 0) ? 1 : 0;
49 tree instr_type
= gccbrig_tree_type_for_hsa_type (inst
.type
);
51 /* Utilize the atomic data types (from C++11 support) for implementing
54 tree atomic_type
= build_qualified_type (instr_type
, TYPE_QUAL_ATOMIC
);
56 gcc_assert (atomic_type
!= NULL_TREE
);
58 tree signal_handle
= operands
[first_input
];
59 tree atomic_ptype
= build_pointer_type (atomic_type
);
60 tree casted_to_ptr
= convert_to_pointer (atomic_ptype
, signal_handle
);
62 tree src0
= NULL_TREE
;
63 if (atomic_opcode
!= BRIG_ATOMIC_LD
)
64 src0
= operands
[first_input
+ 1];
66 tree instr_expr
= NULL_TREE
;
68 tree ptype
= build_pointer_type (instr_type
);
69 tree ptr
= convert_to_pointer (ptype
, operands
[first_input
]);
71 if (atomic_opcode
== BRIG_ATOMIC_ST
)
73 tree mem_ref
= build2 (MEM_REF
, atomic_type
, casted_to_ptr
,
74 build_int_cst (atomic_ptype
, 0));
75 instr_expr
= build2 (MODIFY_EXPR
, atomic_type
, mem_ref
, src0
);
77 else if (atomic_opcode
== BRIG_ATOMIC_LD
78 || (atomic_opcode
>= BRIG_ATOMIC_WAIT_EQ
79 && atomic_opcode
<= BRIG_ATOMIC_WAITTIMEOUT_GTE
))
81 tree mem_ref
= build2 (MEM_REF
, atomic_type
, casted_to_ptr
,
82 build_int_cst (atomic_ptype
, 0));
83 /* signal_wait* instructions can return spuriously before the
84 condition becomes true. Therefore it's legal to return
85 right away. TODO: builtin calls which can be
86 implemented with a power efficient sleep-wait. */
89 else if (atomic_opcode
== BRIG_ATOMIC_CAS
)
91 /* Special case for CAS due to the two args. */
92 tree built_in
= NULL_TREE
;
93 switch (gccbrig_hsa_type_bit_size (inst
.type
))
97 = builtin_decl_explicit (BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
);
101 = builtin_decl_explicit (BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
);
107 tree src1
= operands
[first_input
+ 2];
110 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (built_in
))));
112 tree src1_type
= TREE_VALUE
113 (TREE_CHAIN (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (built_in
)))));
115 instr_expr
= call_builtin (built_in
, 3, instr_type
, ptype
, ptr
,
116 src0_type
, src0
, src1_type
, src1
);
120 tree built_in
= NULL_TREE
;
121 /* The rest of the builtins have the same number of parameters.
122 Generate a big if..else that finds the correct builtin
123 automagically from the def file. */
124 #undef DEF_HSAIL_SAT_BUILTIN
125 #undef DEF_HSAIL_BUILTIN
126 #undef DEF_HSAIL_ATOMIC_BUILTIN
127 #undef DEF_HSAIL_INTR_BUILTIN
128 #undef DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN
130 #define DEF_HSAIL_ATOMIC_BUILTIN(ENUM, ATOMIC_OPCODE, HSAIL_TYPE, \
132 if (atomic_opcode == ATOMIC_OPCODE && inst.type == HSAIL_TYPE) \
133 built_in = builtin_decl_explicit (ENUM); \
135 #include "brig-builtins.def"
136 switch (atomic_opcode
)
138 case BRIG_ATOMIC_ADD
:
139 switch (gccbrig_hsa_type_bit_size (inst
.type
))
143 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_ADD_4
);
147 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_ADD_8
);
153 case BRIG_ATOMIC_SUB
:
154 switch (gccbrig_hsa_type_bit_size (inst
.type
))
158 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_SUB_4
);
162 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_SUB_8
);
168 case BRIG_ATOMIC_AND
:
169 switch (gccbrig_hsa_type_bit_size (inst
.type
))
173 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_AND_4
);
177 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_AND_8
);
183 case BRIG_ATOMIC_XOR
:
184 switch (gccbrig_hsa_type_bit_size (inst
.type
))
188 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_XOR_4
);
192 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_XOR_8
);
199 switch (gccbrig_hsa_type_bit_size (inst
.type
))
203 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_OR_4
);
207 = builtin_decl_explicit (BUILT_IN_SYNC_FETCH_AND_OR_8
);
213 case BRIG_ATOMIC_EXCH
:
214 switch (gccbrig_hsa_type_bit_size (inst
.type
))
218 = builtin_decl_explicit (BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
);
222 = builtin_decl_explicit (BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
);
232 gcc_assert (built_in
!= NULL_TREE
);
234 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (built_in
))));
236 instr_expr
= call_builtin (built_in
, 2, instr_type
, ptr_type_node
,
237 ptr
, arg0_type
, src0
);
239 /* We need a temp variable for the result, because otherwise
240 the gimplifier drops a necessary (unsigned to signed) cast in
241 the output assignment and fails a check later. */
242 tree tmp_var
= create_tmp_var (arg0_type
, "builtin_out");
244 = build2 (MODIFY_EXPR
, TREE_TYPE (tmp_var
), tmp_var
, instr_expr
);
245 m_parent
.m_cf
->append_statement (tmp_assign
);
246 instr_expr
= tmp_var
;
250 build_output_assignment (inst
, operands
[0], instr_expr
);
252 m_parent
.m_cf
->append_statement (instr_expr
);
254 return inst
.base
.byteCount
;
258 brig_atomic_inst_handler::operator () (const BrigBase
*base
)
260 const BrigInstAtomic
*inst
= (const BrigInstAtomic
*) base
;
261 BrigAtomicOperation8_t atomic_opcode
;
262 atomic_opcode
= inst
->atomicOperation
;
264 return generate_tree (inst
->base
, atomic_opcode
);