]>
Commit | Line | Data |
---|---|---|
60cbb674 TS |
1 | /* OMP constructs' SIMD clone supporting code. |
2 | ||
8d9254fc | 3 | Copyright (C) 2005-2020 Free Software Foundation, Inc. |
60cbb674 TS |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "backend.h" | |
25 | #include "target.h" | |
26 | #include "tree.h" | |
27 | #include "gimple.h" | |
28 | #include "cfghooks.h" | |
29 | #include "alloc-pool.h" | |
30 | #include "tree-pass.h" | |
31 | #include "ssa.h" | |
32 | #include "cgraph.h" | |
33 | #include "pretty-print.h" | |
34 | #include "diagnostic-core.h" | |
35 | #include "fold-const.h" | |
36 | #include "stor-layout.h" | |
37 | #include "cfganal.h" | |
38 | #include "gimplify.h" | |
39 | #include "gimple-iterator.h" | |
40 | #include "gimplify-me.h" | |
41 | #include "gimple-walk.h" | |
42 | #include "langhooks.h" | |
43 | #include "tree-cfg.h" | |
44 | #include "tree-into-ssa.h" | |
45 | #include "tree-dfa.h" | |
46 | #include "cfgloop.h" | |
47 | #include "symbol-summary.h" | |
4d99a848 | 48 | #include "ipa-param-manipulation.h" |
60cbb674 | 49 | #include "tree-eh.h" |
ad200580 | 50 | #include "varasm.h" |
314e6352 ML |
51 | #include "stringpool.h" |
52 | #include "attribs.h" | |
848bb6fc | 53 | #include "omp-simd-clone.h" |
60cbb674 | 54 | |
d8f860ef RS |
55 | /* Return the number of elements in vector type VECTYPE, which is associated |
56 | with a SIMD clone. At present these always have a constant length. */ | |
57 | ||
58 | static unsigned HOST_WIDE_INT | |
59 | simd_clone_subparts (tree vectype) | |
60 | { | |
928686b1 | 61 | return TYPE_VECTOR_SUBPARTS (vectype).to_constant (); |
d8f860ef RS |
62 | } |
63 | ||
60cbb674 TS |
64 | /* Allocate a fresh `simd_clone' and return it. NARGS is the number |
65 | of arguments to reserve space for. */ | |
66 | ||
67 | static struct cgraph_simd_clone * | |
68 | simd_clone_struct_alloc (int nargs) | |
69 | { | |
70 | struct cgraph_simd_clone *clone_info; | |
71 | size_t len = (sizeof (struct cgraph_simd_clone) | |
72 | + nargs * sizeof (struct cgraph_simd_clone_arg)); | |
73 | clone_info = (struct cgraph_simd_clone *) | |
74 | ggc_internal_cleared_alloc (len); | |
75 | return clone_info; | |
76 | } | |
77 | ||
78 | /* Make a copy of the `struct cgraph_simd_clone' in FROM to TO. */ | |
79 | ||
80 | static inline void | |
81 | simd_clone_struct_copy (struct cgraph_simd_clone *to, | |
82 | struct cgraph_simd_clone *from) | |
83 | { | |
84 | memcpy (to, from, (sizeof (struct cgraph_simd_clone) | |
85 | + ((from->nargs - from->inbranch) | |
86 | * sizeof (struct cgraph_simd_clone_arg)))); | |
87 | } | |
88 | ||
ff6686d2 MJ |
89 | /* Fill an empty vector ARGS with parameter types of function FNDECL. This |
90 | uses TYPE_ARG_TYPES if available, otherwise falls back to types of | |
60cbb674 TS |
91 | DECL_ARGUMENTS types. */ |
92 | ||
ff6686d2 MJ |
93 | static void |
94 | simd_clone_vector_of_formal_parm_types (vec<tree> *args, tree fndecl) | |
60cbb674 TS |
95 | { |
96 | if (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) | |
ff6686d2 MJ |
97 | { |
98 | push_function_arg_types (args, TREE_TYPE (fndecl)); | |
99 | return; | |
100 | } | |
101 | push_function_arg_decls (args, fndecl); | |
60cbb674 TS |
102 | unsigned int i; |
103 | tree arg; | |
ff6686d2 MJ |
104 | FOR_EACH_VEC_ELT (*args, i, arg) |
105 | (*args)[i] = TREE_TYPE ((*args)[i]); | |
60cbb674 TS |
106 | } |
107 | ||
108 | /* Given a simd function in NODE, extract the simd specific | |
109 | information from the OMP clauses passed in CLAUSES, and return | |
110 | the struct cgraph_simd_clone * if it should be cloned. *INBRANCH_SPECIFIED | |
111 | is set to TRUE if the `inbranch' or `notinbranch' clause specified, | |
112 | otherwise set to FALSE. */ | |
113 | ||
114 | static struct cgraph_simd_clone * | |
115 | simd_clone_clauses_extract (struct cgraph_node *node, tree clauses, | |
116 | bool *inbranch_specified) | |
117 | { | |
ff6686d2 MJ |
118 | auto_vec<tree> args; |
119 | simd_clone_vector_of_formal_parm_types (&args, node->decl); | |
60cbb674 TS |
120 | tree t; |
121 | int n; | |
122 | *inbranch_specified = false; | |
123 | ||
124 | n = args.length (); | |
125 | if (n > 0 && args.last () == void_type_node) | |
126 | n--; | |
127 | ||
60cbb674 TS |
128 | /* Allocate one more than needed just in case this is an in-branch |
129 | clone which will require a mask argument. */ | |
130 | struct cgraph_simd_clone *clone_info = simd_clone_struct_alloc (n + 1); | |
131 | clone_info->nargs = n; | |
60cbb674 TS |
132 | |
133 | if (!clauses) | |
9dc5773f JJ |
134 | goto out; |
135 | ||
60cbb674 TS |
136 | clauses = TREE_VALUE (clauses); |
137 | if (!clauses || TREE_CODE (clauses) != OMP_CLAUSE) | |
9dc5773f | 138 | goto out; |
60cbb674 TS |
139 | |
140 | for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t)) | |
141 | { | |
142 | switch (OMP_CLAUSE_CODE (t)) | |
143 | { | |
144 | case OMP_CLAUSE_INBRANCH: | |
145 | clone_info->inbranch = 1; | |
146 | *inbranch_specified = true; | |
147 | break; | |
148 | case OMP_CLAUSE_NOTINBRANCH: | |
149 | clone_info->inbranch = 0; | |
150 | *inbranch_specified = true; | |
151 | break; | |
152 | case OMP_CLAUSE_SIMDLEN: | |
153 | clone_info->simdlen | |
154 | = TREE_INT_CST_LOW (OMP_CLAUSE_SIMDLEN_EXPR (t)); | |
155 | break; | |
156 | case OMP_CLAUSE_LINEAR: | |
157 | { | |
158 | tree decl = OMP_CLAUSE_DECL (t); | |
159 | tree step = OMP_CLAUSE_LINEAR_STEP (t); | |
160 | int argno = TREE_INT_CST_LOW (decl); | |
161 | if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (t)) | |
162 | { | |
163 | enum cgraph_simd_clone_arg_type arg_type; | |
164 | if (TREE_CODE (args[argno]) == REFERENCE_TYPE) | |
165 | switch (OMP_CLAUSE_LINEAR_KIND (t)) | |
166 | { | |
167 | case OMP_CLAUSE_LINEAR_REF: | |
168 | arg_type | |
169 | = SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP; | |
170 | break; | |
171 | case OMP_CLAUSE_LINEAR_UVAL: | |
172 | arg_type | |
173 | = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP; | |
174 | break; | |
175 | case OMP_CLAUSE_LINEAR_VAL: | |
176 | case OMP_CLAUSE_LINEAR_DEFAULT: | |
177 | arg_type | |
178 | = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP; | |
179 | break; | |
180 | default: | |
181 | gcc_unreachable (); | |
182 | } | |
183 | else | |
184 | arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP; | |
185 | clone_info->args[argno].arg_type = arg_type; | |
186 | clone_info->args[argno].linear_step = tree_to_shwi (step); | |
187 | gcc_assert (clone_info->args[argno].linear_step >= 0 | |
188 | && clone_info->args[argno].linear_step < n); | |
189 | } | |
190 | else | |
191 | { | |
192 | if (POINTER_TYPE_P (args[argno])) | |
193 | step = fold_convert (ssizetype, step); | |
194 | if (!tree_fits_shwi_p (step)) | |
195 | { | |
196 | warning_at (OMP_CLAUSE_LOCATION (t), 0, | |
197 | "ignoring large linear step"); | |
60cbb674 TS |
198 | return NULL; |
199 | } | |
200 | else if (integer_zerop (step)) | |
201 | { | |
202 | warning_at (OMP_CLAUSE_LOCATION (t), 0, | |
203 | "ignoring zero linear step"); | |
60cbb674 TS |
204 | return NULL; |
205 | } | |
206 | else | |
207 | { | |
208 | enum cgraph_simd_clone_arg_type arg_type; | |
209 | if (TREE_CODE (args[argno]) == REFERENCE_TYPE) | |
210 | switch (OMP_CLAUSE_LINEAR_KIND (t)) | |
211 | { | |
212 | case OMP_CLAUSE_LINEAR_REF: | |
213 | arg_type | |
214 | = SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP; | |
215 | break; | |
216 | case OMP_CLAUSE_LINEAR_UVAL: | |
217 | arg_type | |
218 | = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP; | |
219 | break; | |
220 | case OMP_CLAUSE_LINEAR_VAL: | |
221 | case OMP_CLAUSE_LINEAR_DEFAULT: | |
222 | arg_type | |
223 | = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP; | |
224 | break; | |
225 | default: | |
226 | gcc_unreachable (); | |
227 | } | |
228 | else | |
229 | arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP; | |
230 | clone_info->args[argno].arg_type = arg_type; | |
231 | clone_info->args[argno].linear_step = tree_to_shwi (step); | |
232 | } | |
233 | } | |
234 | break; | |
235 | } | |
236 | case OMP_CLAUSE_UNIFORM: | |
237 | { | |
238 | tree decl = OMP_CLAUSE_DECL (t); | |
239 | int argno = tree_to_uhwi (decl); | |
240 | clone_info->args[argno].arg_type | |
241 | = SIMD_CLONE_ARG_TYPE_UNIFORM; | |
242 | break; | |
243 | } | |
244 | case OMP_CLAUSE_ALIGNED: | |
245 | { | |
b33d65e3 JJ |
246 | /* Ignore aligned (x) for declare simd, for the ABI we really |
247 | need an alignment specified. */ | |
248 | if (OMP_CLAUSE_ALIGNED_ALIGNMENT (t) == NULL_TREE) | |
249 | break; | |
60cbb674 TS |
250 | tree decl = OMP_CLAUSE_DECL (t); |
251 | int argno = tree_to_uhwi (decl); | |
252 | clone_info->args[argno].alignment | |
253 | = TREE_INT_CST_LOW (OMP_CLAUSE_ALIGNED_ALIGNMENT (t)); | |
254 | break; | |
255 | } | |
256 | default: | |
257 | break; | |
258 | } | |
259 | } | |
9dc5773f JJ |
260 | |
261 | out: | |
262 | if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (node->decl)))) | |
263 | { | |
264 | warning_at (DECL_SOURCE_LOCATION (node->decl), 0, | |
265 | "ignoring %<#pragma omp declare simd%> on function " | |
266 | "with %<_Atomic%> qualified return type"); | |
9dc5773f JJ |
267 | return NULL; |
268 | } | |
269 | ||
270 | for (unsigned int argno = 0; argno < clone_info->nargs; argno++) | |
271 | if (TYPE_ATOMIC (args[argno]) | |
272 | && clone_info->args[argno].arg_type != SIMD_CLONE_ARG_TYPE_UNIFORM) | |
273 | { | |
274 | warning_at (DECL_SOURCE_LOCATION (node->decl), 0, | |
275 | "ignoring %<#pragma omp declare simd%> on function " | |
276 | "with %<_Atomic%> qualified non-%<uniform%> argument"); | |
277 | args.release (); | |
278 | return NULL; | |
279 | } | |
280 | ||
60cbb674 TS |
281 | return clone_info; |
282 | } | |
283 | ||
284 | /* Given a SIMD clone in NODE, calculate the characteristic data | |
285 | type and return the coresponding type. The characteristic data | |
286 | type is computed as described in the Intel Vector ABI. */ | |
287 | ||
288 | static tree | |
289 | simd_clone_compute_base_data_type (struct cgraph_node *node, | |
290 | struct cgraph_simd_clone *clone_info) | |
291 | { | |
292 | tree type = integer_type_node; | |
293 | tree fndecl = node->decl; | |
294 | ||
295 | /* a) For non-void function, the characteristic data type is the | |
296 | return type. */ | |
297 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE) | |
298 | type = TREE_TYPE (TREE_TYPE (fndecl)); | |
299 | ||
300 | /* b) If the function has any non-uniform, non-linear parameters, | |
301 | then the characteristic data type is the type of the first | |
302 | such parameter. */ | |
303 | else | |
304 | { | |
ff6686d2 MJ |
305 | auto_vec<tree> map; |
306 | simd_clone_vector_of_formal_parm_types (&map, fndecl); | |
60cbb674 TS |
307 | for (unsigned int i = 0; i < clone_info->nargs; ++i) |
308 | if (clone_info->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR) | |
309 | { | |
310 | type = map[i]; | |
311 | break; | |
312 | } | |
60cbb674 TS |
313 | } |
314 | ||
315 | /* c) If the characteristic data type determined by a) or b) above | |
316 | is struct, union, or class type which is pass-by-value (except | |
317 | for the type that maps to the built-in complex data type), the | |
318 | characteristic data type is int. */ | |
319 | if (RECORD_OR_UNION_TYPE_P (type) | |
320 | && !aggregate_value_p (type, NULL) | |
321 | && TREE_CODE (type) != COMPLEX_TYPE) | |
322 | return integer_type_node; | |
323 | ||
324 | /* d) If none of the above three classes is applicable, the | |
325 | characteristic data type is int. */ | |
326 | ||
327 | return type; | |
328 | ||
329 | /* e) For Intel Xeon Phi native and offload compilation, if the | |
330 | resulting characteristic data type is 8-bit or 16-bit integer | |
331 | data type, the characteristic data type is int. */ | |
332 | /* Well, we don't handle Xeon Phi yet. */ | |
333 | } | |
334 | ||
335 | static tree | |
336 | simd_clone_mangle (struct cgraph_node *node, | |
337 | struct cgraph_simd_clone *clone_info) | |
338 | { | |
339 | char vecsize_mangle = clone_info->vecsize_mangle; | |
340 | char mask = clone_info->inbranch ? 'M' : 'N'; | |
341 | unsigned int simdlen = clone_info->simdlen; | |
342 | unsigned int n; | |
343 | pretty_printer pp; | |
344 | ||
345 | gcc_assert (vecsize_mangle && simdlen); | |
346 | ||
347 | pp_string (&pp, "_ZGV"); | |
348 | pp_character (&pp, vecsize_mangle); | |
349 | pp_character (&pp, mask); | |
350 | pp_decimal_int (&pp, simdlen); | |
351 | ||
352 | for (n = 0; n < clone_info->nargs; ++n) | |
353 | { | |
354 | struct cgraph_simd_clone_arg arg = clone_info->args[n]; | |
355 | ||
356 | switch (arg.arg_type) | |
357 | { | |
358 | case SIMD_CLONE_ARG_TYPE_UNIFORM: | |
359 | pp_character (&pp, 'u'); | |
360 | break; | |
361 | case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP: | |
362 | pp_character (&pp, 'l'); | |
363 | goto mangle_linear; | |
364 | case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP: | |
365 | pp_character (&pp, 'R'); | |
366 | goto mangle_linear; | |
367 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP: | |
368 | pp_character (&pp, 'L'); | |
369 | goto mangle_linear; | |
370 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP: | |
371 | pp_character (&pp, 'U'); | |
372 | goto mangle_linear; | |
373 | mangle_linear: | |
374 | gcc_assert (arg.linear_step != 0); | |
375 | if (arg.linear_step > 1) | |
376 | pp_unsigned_wide_integer (&pp, arg.linear_step); | |
377 | else if (arg.linear_step < 0) | |
378 | { | |
379 | pp_character (&pp, 'n'); | |
380 | pp_unsigned_wide_integer (&pp, (-(unsigned HOST_WIDE_INT) | |
381 | arg.linear_step)); | |
382 | } | |
383 | break; | |
384 | case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP: | |
385 | pp_string (&pp, "ls"); | |
386 | pp_unsigned_wide_integer (&pp, arg.linear_step); | |
387 | break; | |
388 | case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP: | |
389 | pp_string (&pp, "Rs"); | |
390 | pp_unsigned_wide_integer (&pp, arg.linear_step); | |
391 | break; | |
392 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP: | |
393 | pp_string (&pp, "Ls"); | |
394 | pp_unsigned_wide_integer (&pp, arg.linear_step); | |
395 | break; | |
396 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP: | |
397 | pp_string (&pp, "Us"); | |
398 | pp_unsigned_wide_integer (&pp, arg.linear_step); | |
399 | break; | |
400 | default: | |
401 | pp_character (&pp, 'v'); | |
402 | } | |
403 | if (arg.alignment) | |
404 | { | |
405 | pp_character (&pp, 'a'); | |
406 | pp_decimal_int (&pp, arg.alignment); | |
407 | } | |
408 | } | |
409 | ||
410 | pp_underscore (&pp); | |
411 | const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)); | |
412 | if (*str == '*') | |
413 | ++str; | |
414 | pp_string (&pp, str); | |
415 | str = pp_formatted_text (&pp); | |
416 | ||
417 | /* If there already is a SIMD clone with the same mangled name, don't | |
418 | add another one. This can happen e.g. for | |
419 | #pragma omp declare simd | |
420 | #pragma omp declare simd simdlen(8) | |
421 | int foo (int, int); | |
422 | if the simdlen is assumed to be 8 for the first one, etc. */ | |
423 | for (struct cgraph_node *clone = node->simd_clones; clone; | |
424 | clone = clone->simdclone->next_clone) | |
a01f151f | 425 | if (id_equal (DECL_ASSEMBLER_NAME (clone->decl), str)) |
60cbb674 TS |
426 | return NULL_TREE; |
427 | ||
428 | return get_identifier (str); | |
429 | } | |
430 | ||
431 | /* Create a simd clone of OLD_NODE and return it. */ | |
432 | ||
433 | static struct cgraph_node * | |
434 | simd_clone_create (struct cgraph_node *old_node) | |
435 | { | |
436 | struct cgraph_node *new_node; | |
437 | if (old_node->definition) | |
438 | { | |
439 | if (!old_node->has_gimple_body_p ()) | |
440 | return NULL; | |
441 | old_node->get_body (); | |
442 | new_node = old_node->create_version_clone_with_body (vNULL, NULL, NULL, | |
ff6686d2 | 443 | NULL, NULL, |
60cbb674 TS |
444 | "simdclone"); |
445 | } | |
446 | else | |
447 | { | |
448 | tree old_decl = old_node->decl; | |
449 | tree new_decl = copy_node (old_node->decl); | |
7958186b MP |
450 | DECL_NAME (new_decl) = clone_function_name_numbered (old_decl, |
451 | "simdclone"); | |
60cbb674 TS |
452 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); |
453 | SET_DECL_RTL (new_decl, NULL); | |
454 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; | |
455 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
456 | new_node = old_node->create_version_clone (new_decl, vNULL, NULL); | |
457 | if (old_node->in_other_partition) | |
458 | new_node->in_other_partition = 1; | |
459 | } | |
460 | if (new_node == NULL) | |
461 | return new_node; | |
462 | ||
4d732405 | 463 | set_decl_built_in_function (new_node->decl, NOT_BUILT_IN, 0); |
60cbb674 | 464 | TREE_PUBLIC (new_node->decl) = TREE_PUBLIC (old_node->decl); |
ad200580 JJ |
465 | DECL_COMDAT (new_node->decl) = DECL_COMDAT (old_node->decl); |
466 | DECL_WEAK (new_node->decl) = DECL_WEAK (old_node->decl); | |
467 | DECL_EXTERNAL (new_node->decl) = DECL_EXTERNAL (old_node->decl); | |
468 | DECL_VISIBILITY_SPECIFIED (new_node->decl) | |
469 | = DECL_VISIBILITY_SPECIFIED (old_node->decl); | |
470 | DECL_VISIBILITY (new_node->decl) = DECL_VISIBILITY (old_node->decl); | |
471 | DECL_DLLIMPORT_P (new_node->decl) = DECL_DLLIMPORT_P (old_node->decl); | |
472 | if (DECL_ONE_ONLY (old_node->decl)) | |
473 | make_decl_one_only (new_node->decl, DECL_ASSEMBLER_NAME (new_node->decl)); | |
474 | ||
475 | /* The method cgraph_version_clone_with_body () will force the new | |
476 | symbol local. Undo this, and inherit external visibility from | |
60cbb674 | 477 | the old node. */ |
87f94429 | 478 | new_node->local = old_node->local; |
60cbb674 | 479 | new_node->externally_visible = old_node->externally_visible; |
7a50e708 | 480 | new_node->calls_declare_variant_alt = old_node->calls_declare_variant_alt; |
60cbb674 TS |
481 | |
482 | return new_node; | |
483 | } | |
484 | ||
485 | /* Adjust the return type of the given function to its appropriate | |
486 | vector counterpart. Returns a simd array to be used throughout the | |
487 | function as a return value. */ | |
488 | ||
489 | static tree | |
490 | simd_clone_adjust_return_type (struct cgraph_node *node) | |
491 | { | |
492 | tree fndecl = node->decl; | |
493 | tree orig_rettype = TREE_TYPE (TREE_TYPE (fndecl)); | |
494 | unsigned int veclen; | |
495 | tree t; | |
496 | ||
497 | /* Adjust the function return type. */ | |
498 | if (orig_rettype == void_type_node) | |
499 | return NULL_TREE; | |
60cbb674 TS |
500 | t = TREE_TYPE (TREE_TYPE (fndecl)); |
501 | if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t)) | |
502 | veclen = node->simdclone->vecsize_int; | |
503 | else | |
504 | veclen = node->simdclone->vecsize_float; | |
b397965c | 505 | veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (t)); |
60cbb674 TS |
506 | if (veclen > node->simdclone->simdlen) |
507 | veclen = node->simdclone->simdlen; | |
508 | if (POINTER_TYPE_P (t)) | |
509 | t = pointer_sized_int_node; | |
510 | if (veclen == node->simdclone->simdlen) | |
511 | t = build_vector_type (t, node->simdclone->simdlen); | |
512 | else | |
513 | { | |
514 | t = build_vector_type (t, veclen); | |
515 | t = build_array_type_nelts (t, node->simdclone->simdlen / veclen); | |
516 | } | |
517 | TREE_TYPE (TREE_TYPE (fndecl)) = t; | |
518 | if (!node->definition) | |
519 | return NULL_TREE; | |
520 | ||
521 | t = DECL_RESULT (fndecl); | |
522 | /* Adjust the DECL_RESULT. */ | |
523 | gcc_assert (TREE_TYPE (t) != void_type_node); | |
524 | TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (fndecl)); | |
525 | relayout_decl (t); | |
526 | ||
527 | tree atype = build_array_type_nelts (orig_rettype, | |
528 | node->simdclone->simdlen); | |
529 | if (veclen != node->simdclone->simdlen) | |
530 | return build1 (VIEW_CONVERT_EXPR, atype, t); | |
531 | ||
532 | /* Set up a SIMD array to use as the return value. */ | |
533 | tree retval = create_tmp_var_raw (atype, "retval"); | |
534 | gimple_add_tmp_var (retval); | |
535 | return retval; | |
536 | } | |
537 | ||
538 | /* Each vector argument has a corresponding array to be used locally | |
539 | as part of the eventual loop. Create such temporary array and | |
540 | return it. | |
541 | ||
542 | PREFIX is the prefix to be used for the temporary. | |
543 | ||
544 | TYPE is the inner element type. | |
545 | ||
546 | SIMDLEN is the number of elements. */ | |
547 | ||
548 | static tree | |
549 | create_tmp_simd_array (const char *prefix, tree type, int simdlen) | |
550 | { | |
551 | tree atype = build_array_type_nelts (type, simdlen); | |
552 | tree avar = create_tmp_var_raw (atype, prefix); | |
553 | gimple_add_tmp_var (avar); | |
554 | return avar; | |
555 | } | |
556 | ||
557 | /* Modify the function argument types to their corresponding vector | |
558 | counterparts if appropriate. Also, create one array for each simd | |
559 | argument to be used locally when using the function arguments as | |
560 | part of the loop. | |
561 | ||
562 | NODE is the function whose arguments are to be adjusted. | |
563 | ||
ff6686d2 MJ |
564 | If NODE does not represent function definition, returns NULL. Otherwise |
565 | returns an adjustment class that will be filled describing how the argument | |
566 | declarations will be remapped. New arguments which are not to be remapped | |
567 | are marked with USER_FLAG. */ | |
60cbb674 | 568 | |
ff6686d2 | 569 | static ipa_param_body_adjustments * |
60cbb674 TS |
570 | simd_clone_adjust_argument_types (struct cgraph_node *node) |
571 | { | |
ff6686d2 | 572 | auto_vec<tree> args; |
60cbb674 TS |
573 | |
574 | if (node->definition) | |
ff6686d2 | 575 | push_function_arg_decls (&args, node->decl); |
60cbb674 | 576 | else |
ff6686d2 | 577 | simd_clone_vector_of_formal_parm_types (&args, node->decl); |
60cbb674 | 578 | struct cgraph_simd_clone *sc = node->simdclone; |
ff6686d2 MJ |
579 | vec<ipa_adjusted_param, va_gc> *new_params = NULL; |
580 | vec_safe_reserve (new_params, sc->nargs); | |
581 | unsigned i, j, veclen; | |
60cbb674 TS |
582 | |
583 | for (i = 0; i < sc->nargs; ++i) | |
584 | { | |
ff6686d2 | 585 | ipa_adjusted_param adj; |
60cbb674 TS |
586 | memset (&adj, 0, sizeof (adj)); |
587 | tree parm = args[i]; | |
588 | tree parm_type = node->definition ? TREE_TYPE (parm) : parm; | |
589 | adj.base_index = i; | |
ff6686d2 | 590 | adj.prev_clone_index = i; |
60cbb674 TS |
591 | |
592 | sc->args[i].orig_arg = node->definition ? parm : NULL_TREE; | |
593 | sc->args[i].orig_type = parm_type; | |
594 | ||
595 | switch (sc->args[i].arg_type) | |
596 | { | |
597 | default: | |
598 | /* No adjustment necessary for scalar arguments. */ | |
ff6686d2 | 599 | adj.op = IPA_PARAM_OP_COPY; |
60cbb674 TS |
600 | break; |
601 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP: | |
602 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP: | |
603 | if (node->definition) | |
604 | sc->args[i].simd_array | |
605 | = create_tmp_simd_array (IDENTIFIER_POINTER (DECL_NAME (parm)), | |
606 | TREE_TYPE (parm_type), | |
607 | sc->simdlen); | |
ff6686d2 | 608 | adj.op = IPA_PARAM_OP_COPY; |
60cbb674 TS |
609 | break; |
610 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP: | |
611 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP: | |
612 | case SIMD_CLONE_ARG_TYPE_VECTOR: | |
613 | if (INTEGRAL_TYPE_P (parm_type) || POINTER_TYPE_P (parm_type)) | |
614 | veclen = sc->vecsize_int; | |
615 | else | |
616 | veclen = sc->vecsize_float; | |
b397965c | 617 | veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (parm_type)); |
60cbb674 TS |
618 | if (veclen > sc->simdlen) |
619 | veclen = sc->simdlen; | |
ff6686d2 MJ |
620 | adj.op = IPA_PARAM_OP_NEW; |
621 | adj.param_prefix_index = IPA_PARAM_PREFIX_SIMD; | |
60cbb674 TS |
622 | if (POINTER_TYPE_P (parm_type)) |
623 | adj.type = build_vector_type (pointer_sized_int_node, veclen); | |
624 | else | |
625 | adj.type = build_vector_type (parm_type, veclen); | |
626 | sc->args[i].vector_type = adj.type; | |
627 | for (j = veclen; j < sc->simdlen; j += veclen) | |
628 | { | |
ff6686d2 | 629 | vec_safe_push (new_params, adj); |
60cbb674 TS |
630 | if (j == veclen) |
631 | { | |
632 | memset (&adj, 0, sizeof (adj)); | |
ff6686d2 MJ |
633 | adj.op = IPA_PARAM_OP_NEW; |
634 | adj.user_flag = 1; | |
635 | adj.param_prefix_index = IPA_PARAM_PREFIX_SIMD; | |
60cbb674 | 636 | adj.base_index = i; |
ff6686d2 | 637 | adj.prev_clone_index = i; |
60cbb674 TS |
638 | adj.type = sc->args[i].vector_type; |
639 | } | |
640 | } | |
641 | ||
642 | if (node->definition) | |
643 | sc->args[i].simd_array | |
699e8cb7 JJ |
644 | = create_tmp_simd_array (DECL_NAME (parm) |
645 | ? IDENTIFIER_POINTER (DECL_NAME (parm)) | |
646 | : NULL, parm_type, sc->simdlen); | |
60cbb674 | 647 | } |
ff6686d2 | 648 | vec_safe_push (new_params, adj); |
60cbb674 TS |
649 | } |
650 | ||
651 | if (sc->inbranch) | |
652 | { | |
653 | tree base_type = simd_clone_compute_base_data_type (sc->origin, sc); | |
ff6686d2 | 654 | ipa_adjusted_param adj; |
60cbb674 | 655 | memset (&adj, 0, sizeof (adj)); |
ff6686d2 MJ |
656 | adj.op = IPA_PARAM_OP_NEW; |
657 | adj.user_flag = 1; | |
658 | adj.param_prefix_index = IPA_PARAM_PREFIX_MASK; | |
60cbb674 TS |
659 | |
660 | adj.base_index = i; | |
ff6686d2 | 661 | adj.prev_clone_index = i; |
60cbb674 TS |
662 | if (INTEGRAL_TYPE_P (base_type) || POINTER_TYPE_P (base_type)) |
663 | veclen = sc->vecsize_int; | |
664 | else | |
665 | veclen = sc->vecsize_float; | |
b397965c | 666 | veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (base_type)); |
60cbb674 TS |
667 | if (veclen > sc->simdlen) |
668 | veclen = sc->simdlen; | |
669 | if (sc->mask_mode != VOIDmode) | |
670 | adj.type | |
671 | = lang_hooks.types.type_for_mode (sc->mask_mode, 1); | |
672 | else if (POINTER_TYPE_P (base_type)) | |
673 | adj.type = build_vector_type (pointer_sized_int_node, veclen); | |
674 | else | |
675 | adj.type = build_vector_type (base_type, veclen); | |
ff6686d2 | 676 | vec_safe_push (new_params, adj); |
60cbb674 TS |
677 | |
678 | for (j = veclen; j < sc->simdlen; j += veclen) | |
ff6686d2 | 679 | vec_safe_push (new_params, adj); |
60cbb674 TS |
680 | |
681 | /* We have previously allocated one extra entry for the mask. Use | |
682 | it and fill it. */ | |
683 | sc->nargs++; | |
684 | if (sc->mask_mode != VOIDmode) | |
685 | base_type = boolean_type_node; | |
686 | if (node->definition) | |
687 | { | |
688 | sc->args[i].orig_arg | |
689 | = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL, base_type); | |
690 | if (sc->mask_mode == VOIDmode) | |
691 | sc->args[i].simd_array | |
692 | = create_tmp_simd_array ("mask", base_type, sc->simdlen); | |
693 | else if (veclen < sc->simdlen) | |
694 | sc->args[i].simd_array | |
695 | = create_tmp_simd_array ("mask", adj.type, sc->simdlen / veclen); | |
696 | else | |
697 | sc->args[i].simd_array = NULL_TREE; | |
698 | } | |
699 | sc->args[i].orig_type = base_type; | |
700 | sc->args[i].arg_type = SIMD_CLONE_ARG_TYPE_MASK; | |
701 | } | |
702 | ||
703 | if (node->definition) | |
ff6686d2 MJ |
704 | { |
705 | ipa_param_body_adjustments *adjustments | |
706 | = new ipa_param_body_adjustments (new_params, node->decl); | |
707 | ||
708 | adjustments->modify_formal_parameters (); | |
709 | return adjustments; | |
710 | } | |
60cbb674 TS |
711 | else |
712 | { | |
713 | tree new_arg_types = NULL_TREE, new_reversed; | |
714 | bool last_parm_void = false; | |
715 | if (args.length () > 0 && args.last () == void_type_node) | |
716 | last_parm_void = true; | |
717 | ||
718 | gcc_assert (TYPE_ARG_TYPES (TREE_TYPE (node->decl))); | |
ff6686d2 | 719 | j = vec_safe_length (new_params); |
60cbb674 TS |
720 | for (i = 0; i < j; i++) |
721 | { | |
ff6686d2 | 722 | struct ipa_adjusted_param *adj = &(*new_params)[i]; |
60cbb674 | 723 | tree ptype; |
ff6686d2 | 724 | if (adj->op == IPA_PARAM_OP_COPY) |
60cbb674 TS |
725 | ptype = args[adj->base_index]; |
726 | else | |
727 | ptype = adj->type; | |
728 | new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types); | |
729 | } | |
730 | new_reversed = nreverse (new_arg_types); | |
731 | if (last_parm_void) | |
732 | { | |
733 | if (new_reversed) | |
734 | TREE_CHAIN (new_arg_types) = void_list_node; | |
735 | else | |
736 | new_reversed = void_list_node; | |
737 | } | |
e8a70c17 | 738 | TYPE_ARG_TYPES (TREE_TYPE (node->decl)) = new_reversed; |
ff6686d2 | 739 | return NULL; |
60cbb674 | 740 | } |
60cbb674 TS |
741 | } |
742 | ||
743 | /* Initialize and copy the function arguments in NODE to their | |
744 | corresponding local simd arrays. Returns a fresh gimple_seq with | |
745 | the instruction sequence generated. */ | |
746 | ||
747 | static gimple_seq | |
748 | simd_clone_init_simd_arrays (struct cgraph_node *node, | |
ff6686d2 | 749 | ipa_param_body_adjustments *adjustments) |
60cbb674 TS |
750 | { |
751 | gimple_seq seq = NULL; | |
752 | unsigned i = 0, j = 0, k; | |
753 | ||
754 | for (tree arg = DECL_ARGUMENTS (node->decl); | |
755 | arg; | |
756 | arg = DECL_CHAIN (arg), i++, j++) | |
757 | { | |
ff6686d2 | 758 | if ((*adjustments->m_adj_params)[j].op == IPA_PARAM_OP_COPY |
60cbb674 TS |
759 | || POINTER_TYPE_P (TREE_TYPE (arg))) |
760 | continue; | |
761 | ||
762 | node->simdclone->args[i].vector_arg = arg; | |
763 | ||
764 | tree array = node->simdclone->args[i].simd_array; | |
765 | if (node->simdclone->mask_mode != VOIDmode | |
766 | && node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK) | |
767 | { | |
768 | if (array == NULL_TREE) | |
769 | continue; | |
770 | unsigned int l | |
771 | = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (array)))); | |
772 | for (k = 0; k <= l; k++) | |
773 | { | |
774 | if (k) | |
775 | { | |
776 | arg = DECL_CHAIN (arg); | |
777 | j++; | |
778 | } | |
779 | tree t = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)), | |
780 | array, size_int (k), NULL, NULL); | |
781 | t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg); | |
782 | gimplify_and_add (t, &seq); | |
783 | } | |
784 | continue; | |
785 | } | |
d8f860ef | 786 | if (simd_clone_subparts (TREE_TYPE (arg)) == node->simdclone->simdlen) |
60cbb674 TS |
787 | { |
788 | tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array))); | |
789 | tree ptr = build_fold_addr_expr (array); | |
790 | tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr, | |
791 | build_int_cst (ptype, 0)); | |
792 | t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg); | |
793 | gimplify_and_add (t, &seq); | |
794 | } | |
795 | else | |
796 | { | |
d8f860ef | 797 | unsigned int simdlen = simd_clone_subparts (TREE_TYPE (arg)); |
60cbb674 TS |
798 | tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array))); |
799 | for (k = 0; k < node->simdclone->simdlen; k += simdlen) | |
800 | { | |
801 | tree ptr = build_fold_addr_expr (array); | |
802 | int elemsize; | |
803 | if (k) | |
804 | { | |
805 | arg = DECL_CHAIN (arg); | |
806 | j++; | |
807 | } | |
b397965c RS |
808 | tree elemtype = TREE_TYPE (TREE_TYPE (arg)); |
809 | elemsize = GET_MODE_SIZE (SCALAR_TYPE_MODE (elemtype)); | |
60cbb674 TS |
810 | tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr, |
811 | build_int_cst (ptype, k * elemsize)); | |
812 | t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg); | |
813 | gimplify_and_add (t, &seq); | |
814 | } | |
815 | } | |
816 | } | |
817 | return seq; | |
818 | } | |
819 | ||
820 | /* Callback info for ipa_simd_modify_stmt_ops below. */ | |
821 | ||
822 | struct modify_stmt_info { | |
ff6686d2 | 823 | ipa_param_body_adjustments *adjustments; |
60cbb674 | 824 | gimple *stmt; |
d0fb9ffc | 825 | gimple *after_stmt; |
60cbb674 TS |
826 | /* True if the parent statement was modified by |
827 | ipa_simd_modify_stmt_ops. */ | |
828 | bool modified; | |
829 | }; | |
830 | ||
831 | /* Callback for walk_gimple_op. | |
832 | ||
833 | Adjust operands from a given statement as specified in the | |
834 | adjustments vector in the callback data. */ | |
835 | ||
836 | static tree | |
837 | ipa_simd_modify_stmt_ops (tree *tp, int *walk_subtrees, void *data) | |
838 | { | |
839 | struct walk_stmt_info *wi = (struct walk_stmt_info *) data; | |
840 | struct modify_stmt_info *info = (struct modify_stmt_info *) wi->info; | |
841 | tree *orig_tp = tp; | |
842 | if (TREE_CODE (*tp) == ADDR_EXPR) | |
843 | tp = &TREE_OPERAND (*tp, 0); | |
ff6686d2 MJ |
844 | |
845 | if (TREE_CODE (*tp) == BIT_FIELD_REF | |
846 | || TREE_CODE (*tp) == IMAGPART_EXPR | |
847 | || TREE_CODE (*tp) == REALPART_EXPR) | |
848 | tp = &TREE_OPERAND (*tp, 0); | |
849 | ||
850 | tree repl = NULL_TREE; | |
851 | ipa_param_body_replacement *pbr = NULL; | |
852 | ||
60cbb674 | 853 | if (TREE_CODE (*tp) == PARM_DECL) |
ff6686d2 MJ |
854 | { |
855 | pbr = info->adjustments->get_expr_replacement (*tp, true); | |
856 | if (pbr) | |
857 | repl = pbr->repl; | |
858 | } | |
66146c90 JJ |
859 | else if (TYPE_P (*tp)) |
860 | *walk_subtrees = 0; | |
60cbb674 | 861 | |
ff6686d2 MJ |
862 | if (repl) |
863 | repl = unshare_expr (repl); | |
60cbb674 TS |
864 | else |
865 | { | |
866 | if (tp != orig_tp) | |
867 | { | |
868 | *walk_subtrees = 0; | |
869 | bool modified = info->modified; | |
870 | info->modified = false; | |
871 | walk_tree (tp, ipa_simd_modify_stmt_ops, wi, wi->pset); | |
872 | if (!info->modified) | |
873 | { | |
874 | info->modified = modified; | |
875 | return NULL_TREE; | |
876 | } | |
877 | info->modified = modified; | |
878 | repl = *tp; | |
879 | } | |
880 | else | |
881 | return NULL_TREE; | |
882 | } | |
883 | ||
884 | if (tp != orig_tp) | |
885 | { | |
33813f1d | 886 | if (gimple_code (info->stmt) == GIMPLE_PHI |
ff6686d2 | 887 | && pbr |
33813f1d JJ |
888 | && TREE_CODE (*orig_tp) == ADDR_EXPR |
889 | && TREE_CODE (TREE_OPERAND (*orig_tp, 0)) == PARM_DECL | |
ff6686d2 | 890 | && pbr->dummy) |
33813f1d | 891 | { |
ff6686d2 MJ |
892 | gcc_assert (TREE_CODE (pbr->dummy) == SSA_NAME); |
893 | *orig_tp = pbr->dummy; | |
33813f1d JJ |
894 | info->modified = true; |
895 | return NULL_TREE; | |
896 | } | |
897 | ||
60cbb674 TS |
898 | repl = build_fold_addr_expr (repl); |
899 | gimple *stmt; | |
900 | if (is_gimple_debug (info->stmt)) | |
901 | { | |
902 | tree vexpr = make_node (DEBUG_EXPR_DECL); | |
903 | stmt = gimple_build_debug_source_bind (vexpr, repl, NULL); | |
904 | DECL_ARTIFICIAL (vexpr) = 1; | |
905 | TREE_TYPE (vexpr) = TREE_TYPE (repl); | |
899ca90e | 906 | SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (repl))); |
60cbb674 TS |
907 | repl = vexpr; |
908 | } | |
909 | else | |
910 | { | |
911 | stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (repl)), repl); | |
912 | repl = gimple_assign_lhs (stmt); | |
913 | } | |
33813f1d JJ |
914 | gimple_stmt_iterator gsi; |
915 | if (gimple_code (info->stmt) == GIMPLE_PHI) | |
916 | { | |
d0fb9ffc JJ |
917 | if (info->after_stmt) |
918 | gsi = gsi_for_stmt (info->after_stmt); | |
919 | else | |
920 | gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); | |
33813f1d | 921 | /* Cache SSA_NAME for next time. */ |
ff6686d2 | 922 | if (pbr |
33813f1d JJ |
923 | && TREE_CODE (*orig_tp) == ADDR_EXPR |
924 | && TREE_CODE (TREE_OPERAND (*orig_tp, 0)) == PARM_DECL) | |
ff6686d2 MJ |
925 | { |
926 | gcc_assert (!pbr->dummy); | |
927 | pbr->dummy = repl; | |
928 | } | |
33813f1d JJ |
929 | } |
930 | else | |
931 | gsi = gsi_for_stmt (info->stmt); | |
d0fb9ffc JJ |
932 | if (info->after_stmt) |
933 | gsi_insert_after (&gsi, stmt, GSI_SAME_STMT); | |
934 | else | |
935 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); | |
936 | if (gimple_code (info->stmt) == GIMPLE_PHI) | |
937 | info->after_stmt = stmt; | |
60cbb674 TS |
938 | *orig_tp = repl; |
939 | } | |
940 | else if (!useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (repl))) | |
941 | { | |
942 | tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*tp), repl); | |
943 | *tp = vce; | |
944 | } | |
945 | else | |
946 | *tp = repl; | |
947 | ||
948 | info->modified = true; | |
949 | return NULL_TREE; | |
950 | } | |
951 | ||
952 | /* Traverse the function body and perform all modifications as | |
953 | described in ADJUSTMENTS. At function return, ADJUSTMENTS will be | |
954 | modified such that the replacement/reduction value will now be an | |
955 | offset into the corresponding simd_array. | |
956 | ||
957 | This function will replace all function argument uses with their | |
958 | corresponding simd array elements, and ajust the return values | |
959 | accordingly. */ | |
960 | ||
961 | static void | |
962 | ipa_simd_modify_function_body (struct cgraph_node *node, | |
ff6686d2 | 963 | ipa_param_body_adjustments *adjustments, |
60cbb674 TS |
964 | tree retval_array, tree iter) |
965 | { | |
966 | basic_block bb; | |
ff6686d2 | 967 | unsigned int i, j; |
60cbb674 | 968 | |
ff6686d2 MJ |
969 | |
970 | /* Register replacements for every function argument use to an offset into | |
971 | the corresponding simd_array. */ | |
60cbb674 TS |
972 | for (i = 0, j = 0; i < node->simdclone->nargs; ++i, ++j) |
973 | { | |
ff6686d2 MJ |
974 | if (!node->simdclone->args[i].vector_arg |
975 | || (*adjustments->m_adj_params)[j].user_flag) | |
60cbb674 TS |
976 | continue; |
977 | ||
978 | tree basetype = TREE_TYPE (node->simdclone->args[i].orig_arg); | |
979 | tree vectype = TREE_TYPE (node->simdclone->args[i].vector_arg); | |
ff6686d2 MJ |
980 | tree r = build4 (ARRAY_REF, basetype, node->simdclone->args[i].simd_array, |
981 | iter, NULL_TREE, NULL_TREE); | |
982 | adjustments->register_replacement (&(*adjustments->m_adj_params)[j], r); | |
983 | ||
984 | if (simd_clone_subparts (vectype) < node->simdclone->simdlen) | |
d8f860ef | 985 | j += node->simdclone->simdlen / simd_clone_subparts (vectype) - 1; |
60cbb674 TS |
986 | } |
987 | ||
46aa019a | 988 | tree name; |
46aa019a | 989 | FOR_EACH_SSA_NAME (i, name, cfun) |
60cbb674 | 990 | { |
ff6686d2 | 991 | tree base_var; |
46aa019a | 992 | if (SSA_NAME_VAR (name) |
ff6686d2 MJ |
993 | && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL |
994 | && (base_var | |
995 | = adjustments->get_replacement_ssa_base (SSA_NAME_VAR (name)))) | |
60cbb674 | 996 | { |
ff6686d2 MJ |
997 | if (SSA_NAME_IS_DEFAULT_DEF (name)) |
998 | { | |
999 | tree old_decl = SSA_NAME_VAR (name); | |
1000 | bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)); | |
1001 | gimple_stmt_iterator gsi = gsi_after_labels (bb); | |
1002 | tree repl = adjustments->lookup_replacement (old_decl, 0); | |
1003 | gcc_checking_assert (repl); | |
1004 | repl = unshare_expr (repl); | |
1005 | set_ssa_default_def (cfun, old_decl, NULL_TREE); | |
1006 | SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var); | |
1007 | SSA_NAME_IS_DEFAULT_DEF (name) = 0; | |
1008 | gimple *stmt = gimple_build_assign (name, repl); | |
1009 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); | |
1010 | } | |
1011 | else | |
1012 | SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var); | |
60cbb674 TS |
1013 | } |
1014 | } | |
1015 | ||
1016 | struct modify_stmt_info info; | |
1017 | info.adjustments = adjustments; | |
1018 | ||
1019 | FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl)) | |
1020 | { | |
1021 | gimple_stmt_iterator gsi; | |
1022 | ||
33813f1d JJ |
1023 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
1024 | { | |
1025 | gphi *phi = as_a <gphi *> (gsi_stmt (gsi)); | |
1026 | int i, n = gimple_phi_num_args (phi); | |
1027 | info.stmt = phi; | |
d0fb9ffc | 1028 | info.after_stmt = NULL; |
33813f1d JJ |
1029 | struct walk_stmt_info wi; |
1030 | memset (&wi, 0, sizeof (wi)); | |
1031 | info.modified = false; | |
1032 | wi.info = &info; | |
1033 | for (i = 0; i < n; ++i) | |
1034 | { | |
1035 | int walk_subtrees = 1; | |
1036 | tree arg = gimple_phi_arg_def (phi, i); | |
1037 | tree op = arg; | |
1038 | ipa_simd_modify_stmt_ops (&op, &walk_subtrees, &wi); | |
1039 | if (op != arg) | |
1040 | { | |
1041 | SET_PHI_ARG_DEF (phi, i, op); | |
1042 | gcc_assert (TREE_CODE (op) == SSA_NAME); | |
1043 | if (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL) | |
1044 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1; | |
1045 | } | |
1046 | } | |
1047 | } | |
1048 | ||
60cbb674 TS |
1049 | gsi = gsi_start_bb (bb); |
1050 | while (!gsi_end_p (gsi)) | |
1051 | { | |
1052 | gimple *stmt = gsi_stmt (gsi); | |
1053 | info.stmt = stmt; | |
d0fb9ffc | 1054 | info.after_stmt = NULL; |
60cbb674 TS |
1055 | struct walk_stmt_info wi; |
1056 | ||
1057 | memset (&wi, 0, sizeof (wi)); | |
1058 | info.modified = false; | |
1059 | wi.info = &info; | |
1060 | walk_gimple_op (stmt, ipa_simd_modify_stmt_ops, &wi); | |
1061 | ||
1062 | if (greturn *return_stmt = dyn_cast <greturn *> (stmt)) | |
1063 | { | |
1064 | tree retval = gimple_return_retval (return_stmt); | |
260a9933 JJ |
1065 | edge e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)); |
1066 | e->flags |= EDGE_FALLTHRU; | |
60cbb674 TS |
1067 | if (!retval) |
1068 | { | |
1069 | gsi_remove (&gsi, true); | |
1070 | continue; | |
1071 | } | |
1072 | ||
1073 | /* Replace `return foo' with `retval_array[iter] = foo'. */ | |
1074 | tree ref = build4 (ARRAY_REF, TREE_TYPE (retval), | |
1075 | retval_array, iter, NULL, NULL); | |
1076 | stmt = gimple_build_assign (ref, retval); | |
1077 | gsi_replace (&gsi, stmt, true); | |
1078 | info.modified = true; | |
1079 | } | |
1080 | ||
1081 | if (info.modified) | |
1082 | { | |
1083 | update_stmt (stmt); | |
66146c90 JJ |
1084 | /* If the above changed the var of a debug bind into something |
1085 | different, remove the debug stmt. We could also for all the | |
1086 | replaced parameters add VAR_DECLs for debug info purposes, | |
1087 | add debug stmts for those to be the simd array accesses and | |
1088 | replace debug stmt var operand with that var. Debugging of | |
1089 | vectorized loops doesn't work too well, so don't bother for | |
1090 | now. */ | |
1091 | if ((gimple_debug_bind_p (stmt) | |
1092 | && !DECL_P (gimple_debug_bind_get_var (stmt))) | |
1093 | || (gimple_debug_source_bind_p (stmt) | |
1094 | && !DECL_P (gimple_debug_source_bind_get_var (stmt)))) | |
1095 | { | |
1096 | gsi_remove (&gsi, true); | |
1097 | continue; | |
1098 | } | |
60cbb674 TS |
1099 | if (maybe_clean_eh_stmt (stmt)) |
1100 | gimple_purge_dead_eh_edges (gimple_bb (stmt)); | |
1101 | } | |
1102 | gsi_next (&gsi); | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | /* Helper function of simd_clone_adjust, return linear step addend | |
1108 | of Ith argument. */ | |
1109 | ||
1110 | static tree | |
1111 | simd_clone_linear_addend (struct cgraph_node *node, unsigned int i, | |
1112 | tree addtype, basic_block entry_bb) | |
1113 | { | |
1114 | tree ptype = NULL_TREE; | |
1115 | switch (node->simdclone->args[i].arg_type) | |
1116 | { | |
1117 | case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP: | |
1118 | case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP: | |
1119 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP: | |
1120 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP: | |
1121 | return build_int_cst (addtype, node->simdclone->args[i].linear_step); | |
1122 | case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP: | |
1123 | case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP: | |
1124 | ptype = TREE_TYPE (node->simdclone->args[i].orig_arg); | |
1125 | break; | |
1126 | case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP: | |
1127 | case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP: | |
1128 | ptype = TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg)); | |
1129 | break; | |
1130 | default: | |
1131 | gcc_unreachable (); | |
1132 | } | |
1133 | ||
1134 | unsigned int idx = node->simdclone->args[i].linear_step; | |
1135 | tree arg = node->simdclone->args[idx].orig_arg; | |
1136 | gcc_assert (is_gimple_reg_type (TREE_TYPE (arg))); | |
1137 | gimple_stmt_iterator gsi = gsi_after_labels (entry_bb); | |
1138 | gimple *g; | |
1139 | tree ret; | |
1140 | if (is_gimple_reg (arg)) | |
1141 | ret = get_or_create_ssa_default_def (cfun, arg); | |
1142 | else | |
1143 | { | |
1144 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)), arg); | |
1145 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1146 | ret = gimple_assign_lhs (g); | |
1147 | } | |
1148 | if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE) | |
1149 | { | |
1150 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (TREE_TYPE (arg))), | |
1151 | build_simple_mem_ref (ret)); | |
1152 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1153 | ret = gimple_assign_lhs (g); | |
1154 | } | |
1155 | if (!useless_type_conversion_p (addtype, TREE_TYPE (ret))) | |
1156 | { | |
1157 | g = gimple_build_assign (make_ssa_name (addtype), NOP_EXPR, ret); | |
1158 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1159 | ret = gimple_assign_lhs (g); | |
1160 | } | |
1161 | if (POINTER_TYPE_P (ptype)) | |
1162 | { | |
1163 | tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptype)); | |
1164 | if (size && TREE_CODE (size) == INTEGER_CST) | |
1165 | { | |
1166 | g = gimple_build_assign (make_ssa_name (addtype), MULT_EXPR, | |
1167 | ret, fold_convert (addtype, size)); | |
1168 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1169 | ret = gimple_assign_lhs (g); | |
1170 | } | |
1171 | } | |
1172 | return ret; | |
1173 | } | |
1174 | ||
1175 | /* Adjust the argument types in NODE to their appropriate vector | |
1176 | counterparts. */ | |
1177 | ||
1178 | static void | |
1179 | simd_clone_adjust (struct cgraph_node *node) | |
1180 | { | |
1181 | push_cfun (DECL_STRUCT_FUNCTION (node->decl)); | |
1182 | ||
e8a70c17 | 1183 | TREE_TYPE (node->decl) = build_distinct_type_copy (TREE_TYPE (node->decl)); |
60cbb674 TS |
1184 | targetm.simd_clone.adjust (node); |
1185 | ||
1186 | tree retval = simd_clone_adjust_return_type (node); | |
ff6686d2 | 1187 | ipa_param_body_adjustments *adjustments |
60cbb674 | 1188 | = simd_clone_adjust_argument_types (node); |
ff6686d2 | 1189 | gcc_assert (adjustments); |
60cbb674 TS |
1190 | |
1191 | push_gimplify_context (); | |
1192 | ||
1193 | gimple_seq seq = simd_clone_init_simd_arrays (node, adjustments); | |
1194 | ||
1195 | /* Adjust all uses of vector arguments accordingly. Adjust all | |
1196 | return values accordingly. */ | |
1197 | tree iter = create_tmp_var (unsigned_type_node, "iter"); | |
1198 | tree iter1 = make_ssa_name (iter); | |
4ce71579 | 1199 | tree iter2 = NULL_TREE; |
60cbb674 | 1200 | ipa_simd_modify_function_body (node, adjustments, retval, iter1); |
ff6686d2 | 1201 | delete adjustments; |
60cbb674 TS |
1202 | |
1203 | /* Initialize the iteration variable. */ | |
1204 | basic_block entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)); | |
1205 | basic_block body_bb = split_block_after_labels (entry_bb)->dest; | |
1206 | gimple_stmt_iterator gsi = gsi_after_labels (entry_bb); | |
1207 | /* Insert the SIMD array and iv initialization at function | |
1208 | entry. */ | |
1209 | gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT); | |
1210 | ||
1211 | pop_gimplify_context (NULL); | |
1212 | ||
4ce71579 JJ |
1213 | gimple *g; |
1214 | basic_block incr_bb = NULL; | |
99b1c316 | 1215 | class loop *loop = NULL; |
4ce71579 | 1216 | |
60cbb674 TS |
1217 | /* Create a new BB right before the original exit BB, to hold the |
1218 | iteration increment and the condition/branch. */ | |
4ce71579 JJ |
1219 | if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)) |
1220 | { | |
1221 | basic_block orig_exit = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), 0)->src; | |
1222 | incr_bb = create_empty_bb (orig_exit); | |
e7a74006 | 1223 | incr_bb->count = profile_count::zero (); |
4ce71579 | 1224 | add_bb_to_loop (incr_bb, body_bb->loop_father); |
260a9933 | 1225 | while (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)) |
4ce71579 | 1226 | { |
260a9933 | 1227 | edge e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), 0); |
4ce71579 | 1228 | redirect_edge_succ (e, incr_bb); |
e7a74006 | 1229 | incr_bb->count += e->count (); |
4ce71579 JJ |
1230 | } |
1231 | } | |
1232 | else if (node->simdclone->inbranch) | |
1233 | { | |
1234 | incr_bb = create_empty_bb (entry_bb); | |
e7a74006 | 1235 | incr_bb->count = profile_count::zero (); |
4ce71579 JJ |
1236 | add_bb_to_loop (incr_bb, body_bb->loop_father); |
1237 | } | |
1238 | ||
1239 | if (incr_bb) | |
60cbb674 | 1240 | { |
357067f2 | 1241 | make_single_succ_edge (incr_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); |
4ce71579 JJ |
1242 | gsi = gsi_last_bb (incr_bb); |
1243 | iter2 = make_ssa_name (iter); | |
1244 | g = gimple_build_assign (iter2, PLUS_EXPR, iter1, | |
1245 | build_int_cst (unsigned_type_node, 1)); | |
1246 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1247 | ||
1248 | /* Mostly annotate the loop for the vectorizer (the rest is done | |
1249 | below). */ | |
1250 | loop = alloc_loop (); | |
1251 | cfun->has_force_vectorize_loops = true; | |
1252 | loop->safelen = node->simdclone->simdlen; | |
1253 | loop->force_vectorize = true; | |
1254 | loop->header = body_bb; | |
60cbb674 | 1255 | } |
60cbb674 TS |
1256 | |
1257 | /* Branch around the body if the mask applies. */ | |
1258 | if (node->simdclone->inbranch) | |
1259 | { | |
4ce71579 | 1260 | gsi = gsi_last_bb (loop->header); |
60cbb674 TS |
1261 | tree mask_array |
1262 | = node->simdclone->args[node->simdclone->nargs - 1].simd_array; | |
1263 | tree mask; | |
1264 | if (node->simdclone->mask_mode != VOIDmode) | |
1265 | { | |
1266 | tree shift_cnt; | |
1267 | if (mask_array == NULL_TREE) | |
1268 | { | |
1269 | tree arg = node->simdclone->args[node->simdclone->nargs | |
1270 | - 1].vector_arg; | |
1271 | mask = get_or_create_ssa_default_def (cfun, arg); | |
1272 | shift_cnt = iter1; | |
1273 | } | |
1274 | else | |
1275 | { | |
1276 | tree maskt = TREE_TYPE (mask_array); | |
1277 | int c = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (maskt))); | |
1278 | c = node->simdclone->simdlen / (c + 1); | |
1279 | int s = exact_log2 (c); | |
1280 | gcc_assert (s > 0); | |
1281 | c--; | |
1282 | tree idx = make_ssa_name (TREE_TYPE (iter1)); | |
1283 | g = gimple_build_assign (idx, RSHIFT_EXPR, iter1, | |
1284 | build_int_cst (NULL_TREE, s)); | |
1285 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1286 | mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array))); | |
1287 | tree aref = build4 (ARRAY_REF, | |
1288 | TREE_TYPE (TREE_TYPE (mask_array)), | |
1289 | mask_array, idx, NULL, NULL); | |
1290 | g = gimple_build_assign (mask, aref); | |
1291 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1292 | shift_cnt = make_ssa_name (TREE_TYPE (iter1)); | |
1293 | g = gimple_build_assign (shift_cnt, BIT_AND_EXPR, iter1, | |
1294 | build_int_cst (TREE_TYPE (iter1), c)); | |
1295 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1296 | } | |
1297 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)), | |
1298 | RSHIFT_EXPR, mask, shift_cnt); | |
1299 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1300 | mask = gimple_assign_lhs (g); | |
1301 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)), | |
1302 | BIT_AND_EXPR, mask, | |
1303 | build_int_cst (TREE_TYPE (mask), 1)); | |
1304 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1305 | mask = gimple_assign_lhs (g); | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array))); | |
1310 | tree aref = build4 (ARRAY_REF, | |
1311 | TREE_TYPE (TREE_TYPE (mask_array)), | |
1312 | mask_array, iter1, NULL, NULL); | |
1313 | g = gimple_build_assign (mask, aref); | |
1314 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
b397965c | 1315 | int bitsize = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (aref))); |
60cbb674 TS |
1316 | if (!INTEGRAL_TYPE_P (TREE_TYPE (aref))) |
1317 | { | |
1318 | aref = build1 (VIEW_CONVERT_EXPR, | |
1319 | build_nonstandard_integer_type (bitsize, 0), | |
1320 | mask); | |
1321 | mask = make_ssa_name (TREE_TYPE (aref)); | |
1322 | g = gimple_build_assign (mask, aref); | |
1323 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | g = gimple_build_cond (EQ_EXPR, mask, build_zero_cst (TREE_TYPE (mask)), | |
1328 | NULL, NULL); | |
1329 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
54cb4e20 TV |
1330 | edge e = make_edge (loop->header, incr_bb, EDGE_TRUE_VALUE); |
1331 | e->probability = profile_probability::unlikely ().guessed (); | |
e7a74006 | 1332 | incr_bb->count += e->count (); |
54cb4e20 TV |
1333 | edge fallthru = FALLTHRU_EDGE (loop->header); |
1334 | fallthru->flags = EDGE_FALSE_VALUE; | |
1335 | fallthru->probability = profile_probability::likely ().guessed (); | |
60cbb674 TS |
1336 | } |
1337 | ||
4ce71579 JJ |
1338 | basic_block latch_bb = NULL; |
1339 | basic_block new_exit_bb = NULL; | |
1340 | ||
60cbb674 | 1341 | /* Generate the condition. */ |
4ce71579 JJ |
1342 | if (incr_bb) |
1343 | { | |
1344 | gsi = gsi_last_bb (incr_bb); | |
1345 | g = gimple_build_cond (LT_EXPR, iter2, | |
1346 | build_int_cst (unsigned_type_node, | |
1347 | node->simdclone->simdlen), | |
1348 | NULL, NULL); | |
1349 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
1350 | edge e = split_block (incr_bb, gsi_stmt (gsi)); | |
1351 | latch_bb = e->dest; | |
1352 | new_exit_bb = split_block_after_labels (latch_bb)->dest; | |
1353 | loop->latch = latch_bb; | |
1354 | ||
1355 | redirect_edge_succ (FALLTHRU_EDGE (latch_bb), body_bb); | |
1356 | ||
357067f2 JH |
1357 | edge new_e = make_edge (incr_bb, new_exit_bb, EDGE_FALSE_VALUE); |
1358 | ||
1359 | /* FIXME: Do we need to distribute probabilities for the conditional? */ | |
1360 | new_e->probability = profile_probability::guessed_never (); | |
4ce71579 JJ |
1361 | /* The successor of incr_bb is already pointing to latch_bb; just |
1362 | change the flags. | |
1363 | make_edge (incr_bb, latch_bb, EDGE_TRUE_VALUE); */ | |
1364 | FALLTHRU_EDGE (incr_bb)->flags = EDGE_TRUE_VALUE; | |
1365 | } | |
60cbb674 TS |
1366 | |
1367 | gphi *phi = create_phi_node (iter1, body_bb); | |
1368 | edge preheader_edge = find_edge (entry_bb, body_bb); | |
4ce71579 | 1369 | edge latch_edge = NULL; |
60cbb674 TS |
1370 | add_phi_arg (phi, build_zero_cst (unsigned_type_node), preheader_edge, |
1371 | UNKNOWN_LOCATION); | |
4ce71579 | 1372 | if (incr_bb) |
60cbb674 | 1373 | { |
4ce71579 JJ |
1374 | latch_edge = single_succ_edge (latch_bb); |
1375 | add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION); | |
1376 | ||
1377 | /* Generate the new return. */ | |
1378 | gsi = gsi_last_bb (new_exit_bb); | |
1379 | if (retval | |
1380 | && TREE_CODE (retval) == VIEW_CONVERT_EXPR | |
1381 | && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL) | |
1382 | retval = TREE_OPERAND (retval, 0); | |
1383 | else if (retval) | |
1384 | { | |
1385 | retval = build1 (VIEW_CONVERT_EXPR, | |
1386 | TREE_TYPE (TREE_TYPE (node->decl)), | |
1387 | retval); | |
1388 | retval = force_gimple_operand_gsi (&gsi, retval, true, NULL, | |
1389 | false, GSI_CONTINUE_LINKING); | |
1390 | } | |
1391 | g = gimple_build_return (retval); | |
1392 | gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING); | |
60cbb674 | 1393 | } |
60cbb674 TS |
1394 | |
1395 | /* Handle aligned clauses by replacing default defs of the aligned | |
1396 | uniform args with __builtin_assume_aligned (arg_N(D), alignment) | |
1397 | lhs. Handle linear by adding PHIs. */ | |
1398 | for (unsigned i = 0; i < node->simdclone->nargs; i++) | |
1399 | if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM | |
1400 | && (TREE_ADDRESSABLE (node->simdclone->args[i].orig_arg) | |
1401 | || !is_gimple_reg_type | |
1402 | (TREE_TYPE (node->simdclone->args[i].orig_arg)))) | |
1403 | { | |
1404 | tree orig_arg = node->simdclone->args[i].orig_arg; | |
1405 | if (is_gimple_reg_type (TREE_TYPE (orig_arg))) | |
1406 | iter1 = make_ssa_name (TREE_TYPE (orig_arg)); | |
1407 | else | |
1408 | { | |
1409 | iter1 = create_tmp_var_raw (TREE_TYPE (orig_arg)); | |
1410 | gimple_add_tmp_var (iter1); | |
1411 | } | |
1412 | gsi = gsi_after_labels (entry_bb); | |
1413 | g = gimple_build_assign (iter1, orig_arg); | |
1414 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1415 | gsi = gsi_after_labels (body_bb); | |
1416 | g = gimple_build_assign (orig_arg, iter1); | |
1417 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1418 | } | |
1419 | else if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM | |
1420 | && DECL_BY_REFERENCE (node->simdclone->args[i].orig_arg) | |
1421 | && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg)) | |
1422 | == REFERENCE_TYPE | |
1423 | && TREE_ADDRESSABLE | |
1424 | (TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg)))) | |
1425 | { | |
1426 | tree orig_arg = node->simdclone->args[i].orig_arg; | |
1427 | tree def = ssa_default_def (cfun, orig_arg); | |
1428 | if (def && !has_zero_uses (def)) | |
1429 | { | |
1430 | iter1 = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (orig_arg))); | |
1431 | gimple_add_tmp_var (iter1); | |
1432 | gsi = gsi_after_labels (entry_bb); | |
1433 | g = gimple_build_assign (iter1, build_simple_mem_ref (def)); | |
1434 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1435 | gsi = gsi_after_labels (body_bb); | |
1436 | g = gimple_build_assign (build_simple_mem_ref (def), iter1); | |
1437 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1438 | } | |
1439 | } | |
1440 | else if (node->simdclone->args[i].alignment | |
1441 | && node->simdclone->args[i].arg_type | |
1442 | == SIMD_CLONE_ARG_TYPE_UNIFORM | |
1443 | && (node->simdclone->args[i].alignment | |
1444 | & (node->simdclone->args[i].alignment - 1)) == 0 | |
1445 | && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg)) | |
1446 | == POINTER_TYPE) | |
1447 | { | |
1448 | unsigned int alignment = node->simdclone->args[i].alignment; | |
1449 | tree orig_arg = node->simdclone->args[i].orig_arg; | |
1450 | tree def = ssa_default_def (cfun, orig_arg); | |
1451 | if (def && !has_zero_uses (def)) | |
1452 | { | |
1453 | tree fn = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED); | |
1454 | gimple_seq seq = NULL; | |
1455 | bool need_cvt = false; | |
1456 | gcall *call | |
1457 | = gimple_build_call (fn, 2, def, size_int (alignment)); | |
1458 | g = call; | |
1459 | if (!useless_type_conversion_p (TREE_TYPE (orig_arg), | |
1460 | ptr_type_node)) | |
1461 | need_cvt = true; | |
1462 | tree t = make_ssa_name (need_cvt ? ptr_type_node : orig_arg); | |
1463 | gimple_call_set_lhs (g, t); | |
1464 | gimple_seq_add_stmt_without_update (&seq, g); | |
1465 | if (need_cvt) | |
1466 | { | |
1467 | t = make_ssa_name (orig_arg); | |
1468 | g = gimple_build_assign (t, NOP_EXPR, gimple_call_lhs (g)); | |
1469 | gimple_seq_add_stmt_without_update (&seq, g); | |
1470 | } | |
1471 | gsi_insert_seq_on_edge_immediate | |
1472 | (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq); | |
1473 | ||
1474 | entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)); | |
60cbb674 | 1475 | node->create_edge (cgraph_node::get_create (fn), |
1bad9c18 | 1476 | call, entry_bb->count); |
60cbb674 TS |
1477 | |
1478 | imm_use_iterator iter; | |
1479 | use_operand_p use_p; | |
1480 | gimple *use_stmt; | |
1481 | tree repl = gimple_get_lhs (g); | |
1482 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, def) | |
1483 | if (is_gimple_debug (use_stmt) || use_stmt == call) | |
1484 | continue; | |
1485 | else | |
1486 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
1487 | SET_USE (use_p, repl); | |
1488 | } | |
1489 | } | |
1490 | else if ((node->simdclone->args[i].arg_type | |
1491 | == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP) | |
1492 | || (node->simdclone->args[i].arg_type | |
1493 | == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP) | |
1494 | || (node->simdclone->args[i].arg_type | |
1495 | == SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP) | |
1496 | || (node->simdclone->args[i].arg_type | |
1497 | == SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP)) | |
1498 | { | |
1499 | tree orig_arg = node->simdclone->args[i].orig_arg; | |
1500 | gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (orig_arg)) | |
1501 | || POINTER_TYPE_P (TREE_TYPE (orig_arg))); | |
1502 | tree def = NULL_TREE; | |
1503 | if (TREE_ADDRESSABLE (orig_arg)) | |
1504 | { | |
1505 | def = make_ssa_name (TREE_TYPE (orig_arg)); | |
1506 | iter1 = make_ssa_name (TREE_TYPE (orig_arg)); | |
4ce71579 JJ |
1507 | if (incr_bb) |
1508 | iter2 = make_ssa_name (TREE_TYPE (orig_arg)); | |
60cbb674 TS |
1509 | gsi = gsi_after_labels (entry_bb); |
1510 | g = gimple_build_assign (def, orig_arg); | |
1511 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1512 | } | |
1513 | else | |
1514 | { | |
1515 | def = ssa_default_def (cfun, orig_arg); | |
1516 | if (!def || has_zero_uses (def)) | |
1517 | def = NULL_TREE; | |
1518 | else | |
1519 | { | |
1520 | iter1 = make_ssa_name (orig_arg); | |
4ce71579 JJ |
1521 | if (incr_bb) |
1522 | iter2 = make_ssa_name (orig_arg); | |
60cbb674 TS |
1523 | } |
1524 | } | |
1525 | if (def) | |
1526 | { | |
1527 | phi = create_phi_node (iter1, body_bb); | |
1528 | add_phi_arg (phi, def, preheader_edge, UNKNOWN_LOCATION); | |
4ce71579 JJ |
1529 | if (incr_bb) |
1530 | { | |
1531 | add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION); | |
1532 | enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg)) | |
1533 | ? PLUS_EXPR : POINTER_PLUS_EXPR; | |
1534 | tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg)) | |
1535 | ? TREE_TYPE (orig_arg) : sizetype; | |
1536 | tree addcst = simd_clone_linear_addend (node, i, addtype, | |
1537 | entry_bb); | |
1538 | gsi = gsi_last_bb (incr_bb); | |
1539 | g = gimple_build_assign (iter2, code, iter1, addcst); | |
1540 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1541 | } | |
60cbb674 TS |
1542 | |
1543 | imm_use_iterator iter; | |
1544 | use_operand_p use_p; | |
1545 | gimple *use_stmt; | |
1546 | if (TREE_ADDRESSABLE (orig_arg)) | |
1547 | { | |
1548 | gsi = gsi_after_labels (body_bb); | |
1549 | g = gimple_build_assign (orig_arg, iter1); | |
1550 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
1551 | } | |
1552 | else | |
1553 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, def) | |
1554 | if (use_stmt == phi) | |
1555 | continue; | |
1556 | else | |
1557 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
1558 | SET_USE (use_p, iter1); | |
1559 | } | |
1560 | } | |
1561 | else if (node->simdclone->args[i].arg_type | |
1562 | == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP | |
1563 | || (node->simdclone->args[i].arg_type | |
1564 | == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP)) | |
1565 | { | |
1566 | tree orig_arg = node->simdclone->args[i].orig_arg; | |
1567 | tree def = ssa_default_def (cfun, orig_arg); | |
1568 | gcc_assert (!TREE_ADDRESSABLE (orig_arg) | |
1569 | && TREE_CODE (TREE_TYPE (orig_arg)) == REFERENCE_TYPE); | |
1570 | if (def && !has_zero_uses (def)) | |
1571 | { | |
1572 | tree rtype = TREE_TYPE (TREE_TYPE (orig_arg)); | |
1573 | iter1 = make_ssa_name (orig_arg); | |
4ce71579 JJ |
1574 | if (incr_bb) |
1575 | iter2 = make_ssa_name (orig_arg); | |
60cbb674 TS |
1576 | tree iter3 = make_ssa_name (rtype); |
1577 | tree iter4 = make_ssa_name (rtype); | |
4ce71579 | 1578 | tree iter5 = incr_bb ? make_ssa_name (rtype) : NULL_TREE; |
60cbb674 TS |
1579 | gsi = gsi_after_labels (entry_bb); |
1580 | gimple *load | |
1581 | = gimple_build_assign (iter3, build_simple_mem_ref (def)); | |
1582 | gsi_insert_before (&gsi, load, GSI_NEW_STMT); | |
1583 | ||
1584 | tree array = node->simdclone->args[i].simd_array; | |
1585 | TREE_ADDRESSABLE (array) = 1; | |
1586 | tree ptr = build_fold_addr_expr (array); | |
1587 | phi = create_phi_node (iter1, body_bb); | |
1588 | add_phi_arg (phi, ptr, preheader_edge, UNKNOWN_LOCATION); | |
4ce71579 JJ |
1589 | if (incr_bb) |
1590 | { | |
1591 | add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION); | |
1592 | g = gimple_build_assign (iter2, POINTER_PLUS_EXPR, iter1, | |
1593 | TYPE_SIZE_UNIT (TREE_TYPE (iter3))); | |
1594 | gsi = gsi_last_bb (incr_bb); | |
1595 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1596 | } | |
60cbb674 TS |
1597 | |
1598 | phi = create_phi_node (iter4, body_bb); | |
1599 | add_phi_arg (phi, iter3, preheader_edge, UNKNOWN_LOCATION); | |
4ce71579 JJ |
1600 | if (incr_bb) |
1601 | { | |
1602 | add_phi_arg (phi, iter5, latch_edge, UNKNOWN_LOCATION); | |
1603 | enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (iter3)) | |
1604 | ? PLUS_EXPR : POINTER_PLUS_EXPR; | |
1605 | tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (iter3)) | |
1606 | ? TREE_TYPE (iter3) : sizetype; | |
1607 | tree addcst = simd_clone_linear_addend (node, i, addtype, | |
1608 | entry_bb); | |
1609 | g = gimple_build_assign (iter5, code, iter4, addcst); | |
1610 | gsi = gsi_last_bb (incr_bb); | |
1611 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1612 | } | |
60cbb674 TS |
1613 | |
1614 | g = gimple_build_assign (build_simple_mem_ref (iter1), iter4); | |
1615 | gsi = gsi_after_labels (body_bb); | |
1616 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1617 | ||
1618 | imm_use_iterator iter; | |
1619 | use_operand_p use_p; | |
1620 | gimple *use_stmt; | |
1621 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, def) | |
1622 | if (use_stmt == load) | |
1623 | continue; | |
1624 | else | |
1625 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
1626 | SET_USE (use_p, iter1); | |
1627 | ||
4ce71579 | 1628 | if (!TYPE_READONLY (rtype) && incr_bb) |
60cbb674 TS |
1629 | { |
1630 | tree v = make_ssa_name (rtype); | |
1631 | tree aref = build4 (ARRAY_REF, rtype, array, | |
1632 | size_zero_node, NULL_TREE, | |
1633 | NULL_TREE); | |
1634 | gsi = gsi_after_labels (new_exit_bb); | |
1635 | g = gimple_build_assign (v, aref); | |
1636 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1637 | g = gimple_build_assign (build_simple_mem_ref (def), v); | |
1638 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | calculate_dominance_info (CDI_DOMINATORS); | |
4ce71579 JJ |
1644 | if (loop) |
1645 | add_loop (loop, loop->header->loop_father); | |
60cbb674 TS |
1646 | update_ssa (TODO_update_ssa); |
1647 | ||
1648 | pop_cfun (); | |
1649 | } | |
1650 | ||
1651 | /* If the function in NODE is tagged as an elemental SIMD function, | |
1652 | create the appropriate SIMD clones. */ | |
1653 | ||
848bb6fc | 1654 | void |
60cbb674 TS |
1655 | expand_simd_clones (struct cgraph_node *node) |
1656 | { | |
1657 | tree attr = lookup_attribute ("omp declare simd", | |
1658 | DECL_ATTRIBUTES (node->decl)); | |
1659 | if (attr == NULL_TREE | |
a62bfab5 | 1660 | || node->inlined_to |
60cbb674 TS |
1661 | || lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl))) |
1662 | return; | |
1663 | ||
1664 | /* Ignore | |
1665 | #pragma omp declare simd | |
1666 | extern int foo (); | |
1667 | in C, there we don't know the argument types at all. */ | |
1668 | if (!node->definition | |
1669 | && TYPE_ARG_TYPES (TREE_TYPE (node->decl)) == NULL_TREE) | |
1670 | return; | |
1671 | ||
1672 | /* Call this before creating clone_info, as it might ggc_collect. */ | |
1673 | if (node->definition && node->has_gimple_body_p ()) | |
1674 | node->get_body (); | |
1675 | ||
1676 | do | |
1677 | { | |
1678 | /* Start with parsing the "omp declare simd" attribute(s). */ | |
1679 | bool inbranch_clause_specified; | |
1680 | struct cgraph_simd_clone *clone_info | |
1681 | = simd_clone_clauses_extract (node, TREE_VALUE (attr), | |
1682 | &inbranch_clause_specified); | |
1683 | if (clone_info == NULL) | |
1684 | continue; | |
1685 | ||
1686 | int orig_simdlen = clone_info->simdlen; | |
1687 | tree base_type = simd_clone_compute_base_data_type (node, clone_info); | |
1688 | /* The target can return 0 (no simd clones should be created), | |
1689 | 1 (just one ISA of simd clones should be created) or higher | |
1690 | count of ISA variants. In that case, clone_info is initialized | |
1691 | for the first ISA variant. */ | |
1692 | int count | |
1693 | = targetm.simd_clone.compute_vecsize_and_simdlen (node, clone_info, | |
1694 | base_type, 0); | |
1695 | if (count == 0) | |
1696 | continue; | |
1697 | ||
1698 | /* Loop over all COUNT ISA variants, and if !INBRANCH_CLAUSE_SPECIFIED, | |
1699 | also create one inbranch and one !inbranch clone of it. */ | |
1700 | for (int i = 0; i < count * 2; i++) | |
1701 | { | |
1702 | struct cgraph_simd_clone *clone = clone_info; | |
1703 | if (inbranch_clause_specified && (i & 1) != 0) | |
1704 | continue; | |
1705 | ||
1706 | if (i != 0) | |
1707 | { | |
1708 | clone = simd_clone_struct_alloc (clone_info->nargs | |
1709 | + ((i & 1) != 0)); | |
1710 | simd_clone_struct_copy (clone, clone_info); | |
1711 | /* Undo changes targetm.simd_clone.compute_vecsize_and_simdlen | |
1712 | and simd_clone_adjust_argument_types did to the first | |
1713 | clone's info. */ | |
1714 | clone->nargs -= clone_info->inbranch; | |
1715 | clone->simdlen = orig_simdlen; | |
1716 | /* And call the target hook again to get the right ISA. */ | |
1717 | targetm.simd_clone.compute_vecsize_and_simdlen (node, clone, | |
1718 | base_type, | |
1719 | i / 2); | |
1720 | if ((i & 1) != 0) | |
1721 | clone->inbranch = 1; | |
1722 | } | |
1723 | ||
1724 | /* simd_clone_mangle might fail if such a clone has been created | |
1725 | already. */ | |
1726 | tree id = simd_clone_mangle (node, clone); | |
1727 | if (id == NULL_TREE) | |
b843bcb8 JJ |
1728 | { |
1729 | if (i == 0) | |
1730 | clone->nargs += clone->inbranch; | |
1731 | continue; | |
1732 | } | |
60cbb674 TS |
1733 | |
1734 | /* Only when we are sure we want to create the clone actually | |
1735 | clone the function (or definitions) or create another | |
1736 | extern FUNCTION_DECL (for prototypes without definitions). */ | |
1737 | struct cgraph_node *n = simd_clone_create (node); | |
1738 | if (n == NULL) | |
b843bcb8 JJ |
1739 | { |
1740 | if (i == 0) | |
1741 | clone->nargs += clone->inbranch; | |
1742 | continue; | |
1743 | } | |
60cbb674 TS |
1744 | |
1745 | n->simdclone = clone; | |
1746 | clone->origin = node; | |
1747 | clone->next_clone = NULL; | |
1748 | if (node->simd_clones == NULL) | |
1749 | { | |
1750 | clone->prev_clone = n; | |
1751 | node->simd_clones = n; | |
1752 | } | |
1753 | else | |
1754 | { | |
1755 | clone->prev_clone = node->simd_clones->simdclone->prev_clone; | |
1756 | clone->prev_clone->simdclone->next_clone = n; | |
1757 | node->simd_clones->simdclone->prev_clone = n; | |
1758 | } | |
1759 | symtab->change_decl_assembler_name (n->decl, id); | |
1760 | /* And finally adjust the return type, parameters and for | |
1761 | definitions also function body. */ | |
1762 | if (node->definition) | |
1763 | simd_clone_adjust (n); | |
1764 | else | |
1765 | { | |
e8a70c17 SE |
1766 | TREE_TYPE (n->decl) |
1767 | = build_distinct_type_copy (TREE_TYPE (n->decl)); | |
d9186814 | 1768 | targetm.simd_clone.adjust (n); |
60cbb674 TS |
1769 | simd_clone_adjust_return_type (n); |
1770 | simd_clone_adjust_argument_types (n); | |
1771 | } | |
1772 | } | |
1773 | } | |
1774 | while ((attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))); | |
1775 | } | |
1776 | ||
1777 | /* Entry point for IPA simd clone creation pass. */ | |
1778 | ||
1779 | static unsigned int | |
1780 | ipa_omp_simd_clone (void) | |
1781 | { | |
1782 | struct cgraph_node *node; | |
1783 | FOR_EACH_FUNCTION (node) | |
1784 | expand_simd_clones (node); | |
1785 | return 0; | |
1786 | } | |
1787 | ||
1788 | namespace { | |
1789 | ||
1790 | const pass_data pass_data_omp_simd_clone = | |
1791 | { | |
1792 | SIMPLE_IPA_PASS, /* type */ | |
1793 | "simdclone", /* name */ | |
d03958cf | 1794 | OPTGROUP_OMP, /* optinfo_flags */ |
60cbb674 TS |
1795 | TV_NONE, /* tv_id */ |
1796 | ( PROP_ssa | PROP_cfg ), /* properties_required */ | |
1797 | 0, /* properties_provided */ | |
1798 | 0, /* properties_destroyed */ | |
1799 | 0, /* todo_flags_start */ | |
1800 | 0, /* todo_flags_finish */ | |
1801 | }; | |
1802 | ||
1803 | class pass_omp_simd_clone : public simple_ipa_opt_pass | |
1804 | { | |
1805 | public: | |
1806 | pass_omp_simd_clone(gcc::context *ctxt) | |
1807 | : simple_ipa_opt_pass(pass_data_omp_simd_clone, ctxt) | |
1808 | {} | |
1809 | ||
1810 | /* opt_pass methods: */ | |
1811 | virtual bool gate (function *); | |
1812 | virtual unsigned int execute (function *) { return ipa_omp_simd_clone (); } | |
1813 | }; | |
1814 | ||
1815 | bool | |
1816 | pass_omp_simd_clone::gate (function *) | |
1817 | { | |
1818 | return targetm.simd_clone.compute_vecsize_and_simdlen != NULL; | |
1819 | } | |
1820 | ||
1821 | } // anon namespace | |
1822 | ||
1823 | simple_ipa_opt_pass * | |
1824 | make_pass_omp_simd_clone (gcc::context *ctxt) | |
1825 | { | |
1826 | return new pass_omp_simd_clone (ctxt); | |
1827 | } |