2018-11-09 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / omp-simd-clone.c
blob7bafe39b1778c6e7c1af2507f258ac8dec1c614c
1 /* OMP constructs' SIMD clone supporting code.
3 Copyright (C) 2005-2018 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
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"
48 #include "ipa-param-manipulation.h"
49 #include "tree-eh.h"
50 #include "varasm.h"
51 #include "stringpool.h"
52 #include "attribs.h"
53 #include "omp-simd-clone.h"
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. */
58 static unsigned HOST_WIDE_INT
59 simd_clone_subparts (tree vectype)
61 return TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
64 /* Allocate a fresh `simd_clone' and return it. NARGS is the number
65 of arguments to reserve space for. */
67 static struct cgraph_simd_clone *
68 simd_clone_struct_alloc (int nargs)
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;
78 /* Make a copy of the `struct cgraph_simd_clone' in FROM to TO. */
80 static inline void
81 simd_clone_struct_copy (struct cgraph_simd_clone *to,
82 struct cgraph_simd_clone *from)
84 memcpy (to, from, (sizeof (struct cgraph_simd_clone)
85 + ((from->nargs - from->inbranch)
86 * sizeof (struct cgraph_simd_clone_arg))));
89 /* Return vector of parameter types of function FNDECL. This uses
90 TYPE_ARG_TYPES if available, otherwise falls back to types of
91 DECL_ARGUMENTS types. */
93 static vec<tree>
94 simd_clone_vector_of_formal_parm_types (tree fndecl)
96 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
97 return ipa_get_vector_of_formal_parm_types (TREE_TYPE (fndecl));
98 vec<tree> args = ipa_get_vector_of_formal_parms (fndecl);
99 unsigned int i;
100 tree arg;
101 FOR_EACH_VEC_ELT (args, i, arg)
102 args[i] = TREE_TYPE (args[i]);
103 return args;
106 /* Given a simd function in NODE, extract the simd specific
107 information from the OMP clauses passed in CLAUSES, and return
108 the struct cgraph_simd_clone * if it should be cloned. *INBRANCH_SPECIFIED
109 is set to TRUE if the `inbranch' or `notinbranch' clause specified,
110 otherwise set to FALSE. */
112 static struct cgraph_simd_clone *
113 simd_clone_clauses_extract (struct cgraph_node *node, tree clauses,
114 bool *inbranch_specified)
116 vec<tree> args = simd_clone_vector_of_formal_parm_types (node->decl);
117 tree t;
118 int n;
119 *inbranch_specified = false;
121 n = args.length ();
122 if (n > 0 && args.last () == void_type_node)
123 n--;
125 /* Allocate one more than needed just in case this is an in-branch
126 clone which will require a mask argument. */
127 struct cgraph_simd_clone *clone_info = simd_clone_struct_alloc (n + 1);
128 clone_info->nargs = n;
130 if (!clauses)
131 goto out;
133 clauses = TREE_VALUE (clauses);
134 if (!clauses || TREE_CODE (clauses) != OMP_CLAUSE)
135 goto out;
137 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
139 switch (OMP_CLAUSE_CODE (t))
141 case OMP_CLAUSE_INBRANCH:
142 clone_info->inbranch = 1;
143 *inbranch_specified = true;
144 break;
145 case OMP_CLAUSE_NOTINBRANCH:
146 clone_info->inbranch = 0;
147 *inbranch_specified = true;
148 break;
149 case OMP_CLAUSE_SIMDLEN:
150 clone_info->simdlen
151 = TREE_INT_CST_LOW (OMP_CLAUSE_SIMDLEN_EXPR (t));
152 break;
153 case OMP_CLAUSE_LINEAR:
155 tree decl = OMP_CLAUSE_DECL (t);
156 tree step = OMP_CLAUSE_LINEAR_STEP (t);
157 int argno = TREE_INT_CST_LOW (decl);
158 if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (t))
160 enum cgraph_simd_clone_arg_type arg_type;
161 if (TREE_CODE (args[argno]) == REFERENCE_TYPE)
162 switch (OMP_CLAUSE_LINEAR_KIND (t))
164 case OMP_CLAUSE_LINEAR_REF:
165 arg_type
166 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP;
167 break;
168 case OMP_CLAUSE_LINEAR_UVAL:
169 arg_type
170 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP;
171 break;
172 case OMP_CLAUSE_LINEAR_VAL:
173 case OMP_CLAUSE_LINEAR_DEFAULT:
174 arg_type
175 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP;
176 break;
177 default:
178 gcc_unreachable ();
180 else
181 arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP;
182 clone_info->args[argno].arg_type = arg_type;
183 clone_info->args[argno].linear_step = tree_to_shwi (step);
184 gcc_assert (clone_info->args[argno].linear_step >= 0
185 && clone_info->args[argno].linear_step < n);
187 else
189 if (POINTER_TYPE_P (args[argno]))
190 step = fold_convert (ssizetype, step);
191 if (!tree_fits_shwi_p (step))
193 warning_at (OMP_CLAUSE_LOCATION (t), 0,
194 "ignoring large linear step");
195 args.release ();
196 return NULL;
198 else if (integer_zerop (step))
200 warning_at (OMP_CLAUSE_LOCATION (t), 0,
201 "ignoring zero linear step");
202 args.release ();
203 return NULL;
205 else
207 enum cgraph_simd_clone_arg_type arg_type;
208 if (TREE_CODE (args[argno]) == REFERENCE_TYPE)
209 switch (OMP_CLAUSE_LINEAR_KIND (t))
211 case OMP_CLAUSE_LINEAR_REF:
212 arg_type
213 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP;
214 break;
215 case OMP_CLAUSE_LINEAR_UVAL:
216 arg_type
217 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP;
218 break;
219 case OMP_CLAUSE_LINEAR_VAL:
220 case OMP_CLAUSE_LINEAR_DEFAULT:
221 arg_type
222 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP;
223 break;
224 default:
225 gcc_unreachable ();
227 else
228 arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP;
229 clone_info->args[argno].arg_type = arg_type;
230 clone_info->args[argno].linear_step = tree_to_shwi (step);
233 break;
235 case OMP_CLAUSE_UNIFORM:
237 tree decl = OMP_CLAUSE_DECL (t);
238 int argno = tree_to_uhwi (decl);
239 clone_info->args[argno].arg_type
240 = SIMD_CLONE_ARG_TYPE_UNIFORM;
241 break;
243 case OMP_CLAUSE_ALIGNED:
245 tree decl = OMP_CLAUSE_DECL (t);
246 int argno = tree_to_uhwi (decl);
247 clone_info->args[argno].alignment
248 = TREE_INT_CST_LOW (OMP_CLAUSE_ALIGNED_ALIGNMENT (t));
249 break;
251 default:
252 break;
256 out:
257 if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (node->decl))))
259 warning_at (DECL_SOURCE_LOCATION (node->decl), 0,
260 "ignoring %<#pragma omp declare simd%> on function "
261 "with %<_Atomic%> qualified return type");
262 args.release ();
263 return NULL;
266 for (unsigned int argno = 0; argno < clone_info->nargs; argno++)
267 if (TYPE_ATOMIC (args[argno])
268 && clone_info->args[argno].arg_type != SIMD_CLONE_ARG_TYPE_UNIFORM)
270 warning_at (DECL_SOURCE_LOCATION (node->decl), 0,
271 "ignoring %<#pragma omp declare simd%> on function "
272 "with %<_Atomic%> qualified non-%<uniform%> argument");
273 args.release ();
274 return NULL;
277 args.release ();
278 return clone_info;
281 /* Given a SIMD clone in NODE, calculate the characteristic data
282 type and return the coresponding type. The characteristic data
283 type is computed as described in the Intel Vector ABI. */
285 static tree
286 simd_clone_compute_base_data_type (struct cgraph_node *node,
287 struct cgraph_simd_clone *clone_info)
289 tree type = integer_type_node;
290 tree fndecl = node->decl;
292 /* a) For non-void function, the characteristic data type is the
293 return type. */
294 if (TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE)
295 type = TREE_TYPE (TREE_TYPE (fndecl));
297 /* b) If the function has any non-uniform, non-linear parameters,
298 then the characteristic data type is the type of the first
299 such parameter. */
300 else
302 vec<tree> map = simd_clone_vector_of_formal_parm_types (fndecl);
303 for (unsigned int i = 0; i < clone_info->nargs; ++i)
304 if (clone_info->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
306 type = map[i];
307 break;
309 map.release ();
312 /* c) If the characteristic data type determined by a) or b) above
313 is struct, union, or class type which is pass-by-value (except
314 for the type that maps to the built-in complex data type), the
315 characteristic data type is int. */
316 if (RECORD_OR_UNION_TYPE_P (type)
317 && !aggregate_value_p (type, NULL)
318 && TREE_CODE (type) != COMPLEX_TYPE)
319 return integer_type_node;
321 /* d) If none of the above three classes is applicable, the
322 characteristic data type is int. */
324 return type;
326 /* e) For Intel Xeon Phi native and offload compilation, if the
327 resulting characteristic data type is 8-bit or 16-bit integer
328 data type, the characteristic data type is int. */
329 /* Well, we don't handle Xeon Phi yet. */
332 static tree
333 simd_clone_mangle (struct cgraph_node *node,
334 struct cgraph_simd_clone *clone_info)
336 char vecsize_mangle = clone_info->vecsize_mangle;
337 char mask = clone_info->inbranch ? 'M' : 'N';
338 unsigned int simdlen = clone_info->simdlen;
339 unsigned int n;
340 pretty_printer pp;
342 gcc_assert (vecsize_mangle && simdlen);
344 pp_string (&pp, "_ZGV");
345 pp_character (&pp, vecsize_mangle);
346 pp_character (&pp, mask);
347 pp_decimal_int (&pp, simdlen);
349 for (n = 0; n < clone_info->nargs; ++n)
351 struct cgraph_simd_clone_arg arg = clone_info->args[n];
353 switch (arg.arg_type)
355 case SIMD_CLONE_ARG_TYPE_UNIFORM:
356 pp_character (&pp, 'u');
357 break;
358 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
359 pp_character (&pp, 'l');
360 goto mangle_linear;
361 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
362 pp_character (&pp, 'R');
363 goto mangle_linear;
364 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
365 pp_character (&pp, 'L');
366 goto mangle_linear;
367 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
368 pp_character (&pp, 'U');
369 goto mangle_linear;
370 mangle_linear:
371 gcc_assert (arg.linear_step != 0);
372 if (arg.linear_step > 1)
373 pp_unsigned_wide_integer (&pp, arg.linear_step);
374 else if (arg.linear_step < 0)
376 pp_character (&pp, 'n');
377 pp_unsigned_wide_integer (&pp, (-(unsigned HOST_WIDE_INT)
378 arg.linear_step));
380 break;
381 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
382 pp_string (&pp, "ls");
383 pp_unsigned_wide_integer (&pp, arg.linear_step);
384 break;
385 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
386 pp_string (&pp, "Rs");
387 pp_unsigned_wide_integer (&pp, arg.linear_step);
388 break;
389 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
390 pp_string (&pp, "Ls");
391 pp_unsigned_wide_integer (&pp, arg.linear_step);
392 break;
393 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
394 pp_string (&pp, "Us");
395 pp_unsigned_wide_integer (&pp, arg.linear_step);
396 break;
397 default:
398 pp_character (&pp, 'v');
400 if (arg.alignment)
402 pp_character (&pp, 'a');
403 pp_decimal_int (&pp, arg.alignment);
407 pp_underscore (&pp);
408 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl));
409 if (*str == '*')
410 ++str;
411 pp_string (&pp, str);
412 str = pp_formatted_text (&pp);
414 /* If there already is a SIMD clone with the same mangled name, don't
415 add another one. This can happen e.g. for
416 #pragma omp declare simd
417 #pragma omp declare simd simdlen(8)
418 int foo (int, int);
419 if the simdlen is assumed to be 8 for the first one, etc. */
420 for (struct cgraph_node *clone = node->simd_clones; clone;
421 clone = clone->simdclone->next_clone)
422 if (id_equal (DECL_ASSEMBLER_NAME (clone->decl), str))
423 return NULL_TREE;
425 return get_identifier (str);
428 /* Create a simd clone of OLD_NODE and return it. */
430 static struct cgraph_node *
431 simd_clone_create (struct cgraph_node *old_node)
433 struct cgraph_node *new_node;
434 if (old_node->definition)
436 if (!old_node->has_gimple_body_p ())
437 return NULL;
438 old_node->get_body ();
439 new_node = old_node->create_version_clone_with_body (vNULL, NULL, NULL,
440 false, NULL, NULL,
441 "simdclone");
443 else
445 tree old_decl = old_node->decl;
446 tree new_decl = copy_node (old_node->decl);
447 DECL_NAME (new_decl) = clone_function_name_numbered (old_decl,
448 "simdclone");
449 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
450 SET_DECL_RTL (new_decl, NULL);
451 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
452 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
453 new_node = old_node->create_version_clone (new_decl, vNULL, NULL);
454 if (old_node->in_other_partition)
455 new_node->in_other_partition = 1;
457 if (new_node == NULL)
458 return new_node;
460 DECL_BUILT_IN_CLASS (new_node->decl) = NOT_BUILT_IN;
461 DECL_FUNCTION_CODE (new_node->decl) = (enum built_in_function) 0;
462 TREE_PUBLIC (new_node->decl) = TREE_PUBLIC (old_node->decl);
463 DECL_COMDAT (new_node->decl) = DECL_COMDAT (old_node->decl);
464 DECL_WEAK (new_node->decl) = DECL_WEAK (old_node->decl);
465 DECL_EXTERNAL (new_node->decl) = DECL_EXTERNAL (old_node->decl);
466 DECL_VISIBILITY_SPECIFIED (new_node->decl)
467 = DECL_VISIBILITY_SPECIFIED (old_node->decl);
468 DECL_VISIBILITY (new_node->decl) = DECL_VISIBILITY (old_node->decl);
469 DECL_DLLIMPORT_P (new_node->decl) = DECL_DLLIMPORT_P (old_node->decl);
470 if (DECL_ONE_ONLY (old_node->decl))
471 make_decl_one_only (new_node->decl, DECL_ASSEMBLER_NAME (new_node->decl));
473 /* The method cgraph_version_clone_with_body () will force the new
474 symbol local. Undo this, and inherit external visibility from
475 the old node. */
476 new_node->local.local = old_node->local.local;
477 new_node->externally_visible = old_node->externally_visible;
479 return new_node;
482 /* Adjust the return type of the given function to its appropriate
483 vector counterpart. Returns a simd array to be used throughout the
484 function as a return value. */
486 static tree
487 simd_clone_adjust_return_type (struct cgraph_node *node)
489 tree fndecl = node->decl;
490 tree orig_rettype = TREE_TYPE (TREE_TYPE (fndecl));
491 unsigned int veclen;
492 tree t;
494 /* Adjust the function return type. */
495 if (orig_rettype == void_type_node)
496 return NULL_TREE;
497 TREE_TYPE (fndecl) = build_distinct_type_copy (TREE_TYPE (fndecl));
498 t = TREE_TYPE (TREE_TYPE (fndecl));
499 if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
500 veclen = node->simdclone->vecsize_int;
501 else
502 veclen = node->simdclone->vecsize_float;
503 veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (t));
504 if (veclen > node->simdclone->simdlen)
505 veclen = node->simdclone->simdlen;
506 if (POINTER_TYPE_P (t))
507 t = pointer_sized_int_node;
508 if (veclen == node->simdclone->simdlen)
509 t = build_vector_type (t, node->simdclone->simdlen);
510 else
512 t = build_vector_type (t, veclen);
513 t = build_array_type_nelts (t, node->simdclone->simdlen / veclen);
515 TREE_TYPE (TREE_TYPE (fndecl)) = t;
516 if (!node->definition)
517 return NULL_TREE;
519 t = DECL_RESULT (fndecl);
520 /* Adjust the DECL_RESULT. */
521 gcc_assert (TREE_TYPE (t) != void_type_node);
522 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (fndecl));
523 relayout_decl (t);
525 tree atype = build_array_type_nelts (orig_rettype,
526 node->simdclone->simdlen);
527 if (veclen != node->simdclone->simdlen)
528 return build1 (VIEW_CONVERT_EXPR, atype, t);
530 /* Set up a SIMD array to use as the return value. */
531 tree retval = create_tmp_var_raw (atype, "retval");
532 gimple_add_tmp_var (retval);
533 return retval;
536 /* Each vector argument has a corresponding array to be used locally
537 as part of the eventual loop. Create such temporary array and
538 return it.
540 PREFIX is the prefix to be used for the temporary.
542 TYPE is the inner element type.
544 SIMDLEN is the number of elements. */
546 static tree
547 create_tmp_simd_array (const char *prefix, tree type, int simdlen)
549 tree atype = build_array_type_nelts (type, simdlen);
550 tree avar = create_tmp_var_raw (atype, prefix);
551 gimple_add_tmp_var (avar);
552 return avar;
555 /* Modify the function argument types to their corresponding vector
556 counterparts if appropriate. Also, create one array for each simd
557 argument to be used locally when using the function arguments as
558 part of the loop.
560 NODE is the function whose arguments are to be adjusted.
562 Returns an adjustment vector that will be filled describing how the
563 argument types will be adjusted. */
565 static ipa_parm_adjustment_vec
566 simd_clone_adjust_argument_types (struct cgraph_node *node)
568 vec<tree> args;
569 ipa_parm_adjustment_vec adjustments;
571 if (node->definition)
572 args = ipa_get_vector_of_formal_parms (node->decl);
573 else
574 args = simd_clone_vector_of_formal_parm_types (node->decl);
575 adjustments.create (args.length ());
576 unsigned i, j, veclen;
577 struct ipa_parm_adjustment adj;
578 struct cgraph_simd_clone *sc = node->simdclone;
580 for (i = 0; i < sc->nargs; ++i)
582 memset (&adj, 0, sizeof (adj));
583 tree parm = args[i];
584 tree parm_type = node->definition ? TREE_TYPE (parm) : parm;
585 adj.base_index = i;
586 adj.base = parm;
588 sc->args[i].orig_arg = node->definition ? parm : NULL_TREE;
589 sc->args[i].orig_type = parm_type;
591 switch (sc->args[i].arg_type)
593 default:
594 /* No adjustment necessary for scalar arguments. */
595 adj.op = IPA_PARM_OP_COPY;
596 break;
597 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
598 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
599 if (node->definition)
600 sc->args[i].simd_array
601 = create_tmp_simd_array (IDENTIFIER_POINTER (DECL_NAME (parm)),
602 TREE_TYPE (parm_type),
603 sc->simdlen);
604 adj.op = IPA_PARM_OP_COPY;
605 break;
606 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
607 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
608 case SIMD_CLONE_ARG_TYPE_VECTOR:
609 if (INTEGRAL_TYPE_P (parm_type) || POINTER_TYPE_P (parm_type))
610 veclen = sc->vecsize_int;
611 else
612 veclen = sc->vecsize_float;
613 veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (parm_type));
614 if (veclen > sc->simdlen)
615 veclen = sc->simdlen;
616 adj.arg_prefix = "simd";
617 if (POINTER_TYPE_P (parm_type))
618 adj.type = build_vector_type (pointer_sized_int_node, veclen);
619 else
620 adj.type = build_vector_type (parm_type, veclen);
621 sc->args[i].vector_type = adj.type;
622 for (j = veclen; j < sc->simdlen; j += veclen)
624 adjustments.safe_push (adj);
625 if (j == veclen)
627 memset (&adj, 0, sizeof (adj));
628 adj.op = IPA_PARM_OP_NEW;
629 adj.arg_prefix = "simd";
630 adj.base_index = i;
631 adj.type = sc->args[i].vector_type;
635 if (node->definition)
636 sc->args[i].simd_array
637 = create_tmp_simd_array (DECL_NAME (parm)
638 ? IDENTIFIER_POINTER (DECL_NAME (parm))
639 : NULL, parm_type, sc->simdlen);
641 adjustments.safe_push (adj);
644 if (sc->inbranch)
646 tree base_type = simd_clone_compute_base_data_type (sc->origin, sc);
648 memset (&adj, 0, sizeof (adj));
649 adj.op = IPA_PARM_OP_NEW;
650 adj.arg_prefix = "mask";
652 adj.base_index = i;
653 if (INTEGRAL_TYPE_P (base_type) || POINTER_TYPE_P (base_type))
654 veclen = sc->vecsize_int;
655 else
656 veclen = sc->vecsize_float;
657 veclen /= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (base_type));
658 if (veclen > sc->simdlen)
659 veclen = sc->simdlen;
660 if (sc->mask_mode != VOIDmode)
661 adj.type
662 = lang_hooks.types.type_for_mode (sc->mask_mode, 1);
663 else if (POINTER_TYPE_P (base_type))
664 adj.type = build_vector_type (pointer_sized_int_node, veclen);
665 else
666 adj.type = build_vector_type (base_type, veclen);
667 adjustments.safe_push (adj);
669 for (j = veclen; j < sc->simdlen; j += veclen)
670 adjustments.safe_push (adj);
672 /* We have previously allocated one extra entry for the mask. Use
673 it and fill it. */
674 sc->nargs++;
675 if (sc->mask_mode != VOIDmode)
676 base_type = boolean_type_node;
677 if (node->definition)
679 sc->args[i].orig_arg
680 = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL, base_type);
681 if (sc->mask_mode == VOIDmode)
682 sc->args[i].simd_array
683 = create_tmp_simd_array ("mask", base_type, sc->simdlen);
684 else if (veclen < sc->simdlen)
685 sc->args[i].simd_array
686 = create_tmp_simd_array ("mask", adj.type, sc->simdlen / veclen);
687 else
688 sc->args[i].simd_array = NULL_TREE;
690 sc->args[i].orig_type = base_type;
691 sc->args[i].arg_type = SIMD_CLONE_ARG_TYPE_MASK;
694 if (node->definition)
695 ipa_modify_formal_parameters (node->decl, adjustments);
696 else
698 tree new_arg_types = NULL_TREE, new_reversed;
699 bool last_parm_void = false;
700 if (args.length () > 0 && args.last () == void_type_node)
701 last_parm_void = true;
703 gcc_assert (TYPE_ARG_TYPES (TREE_TYPE (node->decl)));
704 j = adjustments.length ();
705 for (i = 0; i < j; i++)
707 struct ipa_parm_adjustment *adj = &adjustments[i];
708 tree ptype;
709 if (adj->op == IPA_PARM_OP_COPY)
710 ptype = args[adj->base_index];
711 else
712 ptype = adj->type;
713 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
715 new_reversed = nreverse (new_arg_types);
716 if (last_parm_void)
718 if (new_reversed)
719 TREE_CHAIN (new_arg_types) = void_list_node;
720 else
721 new_reversed = void_list_node;
724 tree new_type = build_distinct_type_copy (TREE_TYPE (node->decl));
725 TYPE_ARG_TYPES (new_type) = new_reversed;
726 TREE_TYPE (node->decl) = new_type;
728 adjustments.release ();
730 args.release ();
731 return adjustments;
734 /* Initialize and copy the function arguments in NODE to their
735 corresponding local simd arrays. Returns a fresh gimple_seq with
736 the instruction sequence generated. */
738 static gimple_seq
739 simd_clone_init_simd_arrays (struct cgraph_node *node,
740 ipa_parm_adjustment_vec adjustments)
742 gimple_seq seq = NULL;
743 unsigned i = 0, j = 0, k;
745 for (tree arg = DECL_ARGUMENTS (node->decl);
746 arg;
747 arg = DECL_CHAIN (arg), i++, j++)
749 if (adjustments[j].op == IPA_PARM_OP_COPY
750 || POINTER_TYPE_P (TREE_TYPE (arg)))
751 continue;
753 node->simdclone->args[i].vector_arg = arg;
755 tree array = node->simdclone->args[i].simd_array;
756 if (node->simdclone->mask_mode != VOIDmode
757 && node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
759 if (array == NULL_TREE)
760 continue;
761 unsigned int l
762 = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (array))));
763 for (k = 0; k <= l; k++)
765 if (k)
767 arg = DECL_CHAIN (arg);
768 j++;
770 tree t = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)),
771 array, size_int (k), NULL, NULL);
772 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
773 gimplify_and_add (t, &seq);
775 continue;
777 if (simd_clone_subparts (TREE_TYPE (arg)) == node->simdclone->simdlen)
779 tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array)));
780 tree ptr = build_fold_addr_expr (array);
781 tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr,
782 build_int_cst (ptype, 0));
783 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
784 gimplify_and_add (t, &seq);
786 else
788 unsigned int simdlen = simd_clone_subparts (TREE_TYPE (arg));
789 tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array)));
790 for (k = 0; k < node->simdclone->simdlen; k += simdlen)
792 tree ptr = build_fold_addr_expr (array);
793 int elemsize;
794 if (k)
796 arg = DECL_CHAIN (arg);
797 j++;
799 tree elemtype = TREE_TYPE (TREE_TYPE (arg));
800 elemsize = GET_MODE_SIZE (SCALAR_TYPE_MODE (elemtype));
801 tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr,
802 build_int_cst (ptype, k * elemsize));
803 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
804 gimplify_and_add (t, &seq);
808 return seq;
811 /* Callback info for ipa_simd_modify_stmt_ops below. */
813 struct modify_stmt_info {
814 ipa_parm_adjustment_vec adjustments;
815 gimple *stmt;
816 /* True if the parent statement was modified by
817 ipa_simd_modify_stmt_ops. */
818 bool modified;
821 /* Callback for walk_gimple_op.
823 Adjust operands from a given statement as specified in the
824 adjustments vector in the callback data. */
826 static tree
827 ipa_simd_modify_stmt_ops (tree *tp, int *walk_subtrees, void *data)
829 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
830 struct modify_stmt_info *info = (struct modify_stmt_info *) wi->info;
831 tree *orig_tp = tp;
832 if (TREE_CODE (*tp) == ADDR_EXPR)
833 tp = &TREE_OPERAND (*tp, 0);
834 struct ipa_parm_adjustment *cand = NULL;
835 if (TREE_CODE (*tp) == PARM_DECL)
836 cand = ipa_get_adjustment_candidate (&tp, NULL, info->adjustments, true);
837 else
839 if (TYPE_P (*tp))
840 *walk_subtrees = 0;
843 tree repl = NULL_TREE;
844 if (cand)
845 repl = unshare_expr (cand->new_decl);
846 else
848 if (tp != orig_tp)
850 *walk_subtrees = 0;
851 bool modified = info->modified;
852 info->modified = false;
853 walk_tree (tp, ipa_simd_modify_stmt_ops, wi, wi->pset);
854 if (!info->modified)
856 info->modified = modified;
857 return NULL_TREE;
859 info->modified = modified;
860 repl = *tp;
862 else
863 return NULL_TREE;
866 if (tp != orig_tp)
868 repl = build_fold_addr_expr (repl);
869 gimple *stmt;
870 if (is_gimple_debug (info->stmt))
872 tree vexpr = make_node (DEBUG_EXPR_DECL);
873 stmt = gimple_build_debug_source_bind (vexpr, repl, NULL);
874 DECL_ARTIFICIAL (vexpr) = 1;
875 TREE_TYPE (vexpr) = TREE_TYPE (repl);
876 SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (repl)));
877 repl = vexpr;
879 else
881 stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (repl)), repl);
882 repl = gimple_assign_lhs (stmt);
884 gimple_stmt_iterator gsi = gsi_for_stmt (info->stmt);
885 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
886 *orig_tp = repl;
888 else if (!useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (repl)))
890 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*tp), repl);
891 *tp = vce;
893 else
894 *tp = repl;
896 info->modified = true;
897 return NULL_TREE;
900 /* Traverse the function body and perform all modifications as
901 described in ADJUSTMENTS. At function return, ADJUSTMENTS will be
902 modified such that the replacement/reduction value will now be an
903 offset into the corresponding simd_array.
905 This function will replace all function argument uses with their
906 corresponding simd array elements, and ajust the return values
907 accordingly. */
909 static void
910 ipa_simd_modify_function_body (struct cgraph_node *node,
911 ipa_parm_adjustment_vec adjustments,
912 tree retval_array, tree iter)
914 basic_block bb;
915 unsigned int i, j, l;
917 /* Re-use the adjustments array, but this time use it to replace
918 every function argument use to an offset into the corresponding
919 simd_array. */
920 for (i = 0, j = 0; i < node->simdclone->nargs; ++i, ++j)
922 if (!node->simdclone->args[i].vector_arg)
923 continue;
925 tree basetype = TREE_TYPE (node->simdclone->args[i].orig_arg);
926 tree vectype = TREE_TYPE (node->simdclone->args[i].vector_arg);
927 adjustments[j].new_decl
928 = build4 (ARRAY_REF,
929 basetype,
930 node->simdclone->args[i].simd_array,
931 iter,
932 NULL_TREE, NULL_TREE);
933 if (adjustments[j].op == IPA_PARM_OP_NONE
934 && simd_clone_subparts (vectype) < node->simdclone->simdlen)
935 j += node->simdclone->simdlen / simd_clone_subparts (vectype) - 1;
938 l = adjustments.length ();
939 tree name;
941 FOR_EACH_SSA_NAME (i, name, cfun)
943 if (SSA_NAME_VAR (name)
944 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL)
946 for (j = 0; j < l; j++)
947 if (SSA_NAME_VAR (name) == adjustments[j].base
948 && adjustments[j].new_decl)
950 tree base_var;
951 if (adjustments[j].new_ssa_base == NULL_TREE)
953 base_var
954 = copy_var_decl (adjustments[j].base,
955 DECL_NAME (adjustments[j].base),
956 TREE_TYPE (adjustments[j].base));
957 adjustments[j].new_ssa_base = base_var;
959 else
960 base_var = adjustments[j].new_ssa_base;
961 if (SSA_NAME_IS_DEFAULT_DEF (name))
963 bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
964 gimple_stmt_iterator gsi = gsi_after_labels (bb);
965 tree new_decl = unshare_expr (adjustments[j].new_decl);
966 set_ssa_default_def (cfun, adjustments[j].base, NULL_TREE);
967 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var);
968 SSA_NAME_IS_DEFAULT_DEF (name) = 0;
969 gimple *stmt = gimple_build_assign (name, new_decl);
970 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
972 else
973 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var);
978 struct modify_stmt_info info;
979 info.adjustments = adjustments;
981 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
983 gimple_stmt_iterator gsi;
985 gsi = gsi_start_bb (bb);
986 while (!gsi_end_p (gsi))
988 gimple *stmt = gsi_stmt (gsi);
989 info.stmt = stmt;
990 struct walk_stmt_info wi;
992 memset (&wi, 0, sizeof (wi));
993 info.modified = false;
994 wi.info = &info;
995 walk_gimple_op (stmt, ipa_simd_modify_stmt_ops, &wi);
997 if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
999 tree retval = gimple_return_retval (return_stmt);
1000 if (!retval)
1002 gsi_remove (&gsi, true);
1003 continue;
1006 /* Replace `return foo' with `retval_array[iter] = foo'. */
1007 tree ref = build4 (ARRAY_REF, TREE_TYPE (retval),
1008 retval_array, iter, NULL, NULL);
1009 stmt = gimple_build_assign (ref, retval);
1010 gsi_replace (&gsi, stmt, true);
1011 info.modified = true;
1014 if (info.modified)
1016 update_stmt (stmt);
1017 if (maybe_clean_eh_stmt (stmt))
1018 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1020 gsi_next (&gsi);
1025 /* Helper function of simd_clone_adjust, return linear step addend
1026 of Ith argument. */
1028 static tree
1029 simd_clone_linear_addend (struct cgraph_node *node, unsigned int i,
1030 tree addtype, basic_block entry_bb)
1032 tree ptype = NULL_TREE;
1033 switch (node->simdclone->args[i].arg_type)
1035 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
1036 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
1037 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
1038 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
1039 return build_int_cst (addtype, node->simdclone->args[i].linear_step);
1040 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
1041 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
1042 ptype = TREE_TYPE (node->simdclone->args[i].orig_arg);
1043 break;
1044 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
1045 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
1046 ptype = TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg));
1047 break;
1048 default:
1049 gcc_unreachable ();
1052 unsigned int idx = node->simdclone->args[i].linear_step;
1053 tree arg = node->simdclone->args[idx].orig_arg;
1054 gcc_assert (is_gimple_reg_type (TREE_TYPE (arg)));
1055 gimple_stmt_iterator gsi = gsi_after_labels (entry_bb);
1056 gimple *g;
1057 tree ret;
1058 if (is_gimple_reg (arg))
1059 ret = get_or_create_ssa_default_def (cfun, arg);
1060 else
1062 g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)), arg);
1063 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1064 ret = gimple_assign_lhs (g);
1066 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
1068 g = gimple_build_assign (make_ssa_name (TREE_TYPE (TREE_TYPE (arg))),
1069 build_simple_mem_ref (ret));
1070 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1071 ret = gimple_assign_lhs (g);
1073 if (!useless_type_conversion_p (addtype, TREE_TYPE (ret)))
1075 g = gimple_build_assign (make_ssa_name (addtype), NOP_EXPR, ret);
1076 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1077 ret = gimple_assign_lhs (g);
1079 if (POINTER_TYPE_P (ptype))
1081 tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptype));
1082 if (size && TREE_CODE (size) == INTEGER_CST)
1084 g = gimple_build_assign (make_ssa_name (addtype), MULT_EXPR,
1085 ret, fold_convert (addtype, size));
1086 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1087 ret = gimple_assign_lhs (g);
1090 return ret;
1093 /* Adjust the argument types in NODE to their appropriate vector
1094 counterparts. */
1096 static void
1097 simd_clone_adjust (struct cgraph_node *node)
1099 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1101 targetm.simd_clone.adjust (node);
1103 tree retval = simd_clone_adjust_return_type (node);
1104 ipa_parm_adjustment_vec adjustments
1105 = simd_clone_adjust_argument_types (node);
1107 push_gimplify_context ();
1109 gimple_seq seq = simd_clone_init_simd_arrays (node, adjustments);
1111 /* Adjust all uses of vector arguments accordingly. Adjust all
1112 return values accordingly. */
1113 tree iter = create_tmp_var (unsigned_type_node, "iter");
1114 tree iter1 = make_ssa_name (iter);
1115 tree iter2 = NULL_TREE;
1116 ipa_simd_modify_function_body (node, adjustments, retval, iter1);
1117 adjustments.release ();
1119 /* Initialize the iteration variable. */
1120 basic_block entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1121 basic_block body_bb = split_block_after_labels (entry_bb)->dest;
1122 gimple_stmt_iterator gsi = gsi_after_labels (entry_bb);
1123 /* Insert the SIMD array and iv initialization at function
1124 entry. */
1125 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
1127 pop_gimplify_context (NULL);
1129 gimple *g;
1130 basic_block incr_bb = NULL;
1131 struct loop *loop = NULL;
1133 /* Create a new BB right before the original exit BB, to hold the
1134 iteration increment and the condition/branch. */
1135 if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
1137 basic_block orig_exit = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), 0)->src;
1138 incr_bb = create_empty_bb (orig_exit);
1139 incr_bb->count = profile_count::zero ();
1140 add_bb_to_loop (incr_bb, body_bb->loop_father);
1141 /* The succ of orig_exit was EXIT_BLOCK_PTR_FOR_FN (cfun), with an empty
1142 flag. Set it now to be a FALLTHRU_EDGE. */
1143 gcc_assert (EDGE_COUNT (orig_exit->succs) == 1);
1144 EDGE_SUCC (orig_exit, 0)->flags |= EDGE_FALLTHRU;
1145 for (unsigned i = 0;
1146 i < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); ++i)
1148 edge e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), i);
1149 redirect_edge_succ (e, incr_bb);
1150 incr_bb->count += e->count ();
1153 else if (node->simdclone->inbranch)
1155 incr_bb = create_empty_bb (entry_bb);
1156 incr_bb->count = profile_count::zero ();
1157 add_bb_to_loop (incr_bb, body_bb->loop_father);
1160 if (incr_bb)
1162 make_single_succ_edge (incr_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1163 gsi = gsi_last_bb (incr_bb);
1164 iter2 = make_ssa_name (iter);
1165 g = gimple_build_assign (iter2, PLUS_EXPR, iter1,
1166 build_int_cst (unsigned_type_node, 1));
1167 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1169 /* Mostly annotate the loop for the vectorizer (the rest is done
1170 below). */
1171 loop = alloc_loop ();
1172 cfun->has_force_vectorize_loops = true;
1173 loop->safelen = node->simdclone->simdlen;
1174 loop->force_vectorize = true;
1175 loop->header = body_bb;
1178 /* Branch around the body if the mask applies. */
1179 if (node->simdclone->inbranch)
1181 gsi = gsi_last_bb (loop->header);
1182 tree mask_array
1183 = node->simdclone->args[node->simdclone->nargs - 1].simd_array;
1184 tree mask;
1185 if (node->simdclone->mask_mode != VOIDmode)
1187 tree shift_cnt;
1188 if (mask_array == NULL_TREE)
1190 tree arg = node->simdclone->args[node->simdclone->nargs
1191 - 1].vector_arg;
1192 mask = get_or_create_ssa_default_def (cfun, arg);
1193 shift_cnt = iter1;
1195 else
1197 tree maskt = TREE_TYPE (mask_array);
1198 int c = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (maskt)));
1199 c = node->simdclone->simdlen / (c + 1);
1200 int s = exact_log2 (c);
1201 gcc_assert (s > 0);
1202 c--;
1203 tree idx = make_ssa_name (TREE_TYPE (iter1));
1204 g = gimple_build_assign (idx, RSHIFT_EXPR, iter1,
1205 build_int_cst (NULL_TREE, s));
1206 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1207 mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array)));
1208 tree aref = build4 (ARRAY_REF,
1209 TREE_TYPE (TREE_TYPE (mask_array)),
1210 mask_array, idx, NULL, NULL);
1211 g = gimple_build_assign (mask, aref);
1212 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1213 shift_cnt = make_ssa_name (TREE_TYPE (iter1));
1214 g = gimple_build_assign (shift_cnt, BIT_AND_EXPR, iter1,
1215 build_int_cst (TREE_TYPE (iter1), c));
1216 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1218 g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)),
1219 RSHIFT_EXPR, mask, shift_cnt);
1220 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1221 mask = gimple_assign_lhs (g);
1222 g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)),
1223 BIT_AND_EXPR, mask,
1224 build_int_cst (TREE_TYPE (mask), 1));
1225 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1226 mask = gimple_assign_lhs (g);
1228 else
1230 mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array)));
1231 tree aref = build4 (ARRAY_REF,
1232 TREE_TYPE (TREE_TYPE (mask_array)),
1233 mask_array, iter1, NULL, NULL);
1234 g = gimple_build_assign (mask, aref);
1235 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1236 int bitsize = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (aref)));
1237 if (!INTEGRAL_TYPE_P (TREE_TYPE (aref)))
1239 aref = build1 (VIEW_CONVERT_EXPR,
1240 build_nonstandard_integer_type (bitsize, 0),
1241 mask);
1242 mask = make_ssa_name (TREE_TYPE (aref));
1243 g = gimple_build_assign (mask, aref);
1244 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1248 g = gimple_build_cond (EQ_EXPR, mask, build_zero_cst (TREE_TYPE (mask)),
1249 NULL, NULL);
1250 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1251 edge e = make_edge (loop->header, incr_bb, EDGE_TRUE_VALUE);
1252 e->probability = profile_probability::unlikely ().guessed ();
1253 incr_bb->count += e->count ();
1254 edge fallthru = FALLTHRU_EDGE (loop->header);
1255 fallthru->flags = EDGE_FALSE_VALUE;
1256 fallthru->probability = profile_probability::likely ().guessed ();
1259 basic_block latch_bb = NULL;
1260 basic_block new_exit_bb = NULL;
1262 /* Generate the condition. */
1263 if (incr_bb)
1265 gsi = gsi_last_bb (incr_bb);
1266 g = gimple_build_cond (LT_EXPR, iter2,
1267 build_int_cst (unsigned_type_node,
1268 node->simdclone->simdlen),
1269 NULL, NULL);
1270 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1271 edge e = split_block (incr_bb, gsi_stmt (gsi));
1272 latch_bb = e->dest;
1273 new_exit_bb = split_block_after_labels (latch_bb)->dest;
1274 loop->latch = latch_bb;
1276 redirect_edge_succ (FALLTHRU_EDGE (latch_bb), body_bb);
1278 edge new_e = make_edge (incr_bb, new_exit_bb, EDGE_FALSE_VALUE);
1280 /* FIXME: Do we need to distribute probabilities for the conditional? */
1281 new_e->probability = profile_probability::guessed_never ();
1282 /* The successor of incr_bb is already pointing to latch_bb; just
1283 change the flags.
1284 make_edge (incr_bb, latch_bb, EDGE_TRUE_VALUE); */
1285 FALLTHRU_EDGE (incr_bb)->flags = EDGE_TRUE_VALUE;
1288 gphi *phi = create_phi_node (iter1, body_bb);
1289 edge preheader_edge = find_edge (entry_bb, body_bb);
1290 edge latch_edge = NULL;
1291 add_phi_arg (phi, build_zero_cst (unsigned_type_node), preheader_edge,
1292 UNKNOWN_LOCATION);
1293 if (incr_bb)
1295 latch_edge = single_succ_edge (latch_bb);
1296 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1298 /* Generate the new return. */
1299 gsi = gsi_last_bb (new_exit_bb);
1300 if (retval
1301 && TREE_CODE (retval) == VIEW_CONVERT_EXPR
1302 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
1303 retval = TREE_OPERAND (retval, 0);
1304 else if (retval)
1306 retval = build1 (VIEW_CONVERT_EXPR,
1307 TREE_TYPE (TREE_TYPE (node->decl)),
1308 retval);
1309 retval = force_gimple_operand_gsi (&gsi, retval, true, NULL,
1310 false, GSI_CONTINUE_LINKING);
1312 g = gimple_build_return (retval);
1313 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1316 /* Handle aligned clauses by replacing default defs of the aligned
1317 uniform args with __builtin_assume_aligned (arg_N(D), alignment)
1318 lhs. Handle linear by adding PHIs. */
1319 for (unsigned i = 0; i < node->simdclone->nargs; i++)
1320 if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM
1321 && (TREE_ADDRESSABLE (node->simdclone->args[i].orig_arg)
1322 || !is_gimple_reg_type
1323 (TREE_TYPE (node->simdclone->args[i].orig_arg))))
1325 tree orig_arg = node->simdclone->args[i].orig_arg;
1326 if (is_gimple_reg_type (TREE_TYPE (orig_arg)))
1327 iter1 = make_ssa_name (TREE_TYPE (orig_arg));
1328 else
1330 iter1 = create_tmp_var_raw (TREE_TYPE (orig_arg));
1331 gimple_add_tmp_var (iter1);
1333 gsi = gsi_after_labels (entry_bb);
1334 g = gimple_build_assign (iter1, orig_arg);
1335 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1336 gsi = gsi_after_labels (body_bb);
1337 g = gimple_build_assign (orig_arg, iter1);
1338 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1340 else if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM
1341 && DECL_BY_REFERENCE (node->simdclone->args[i].orig_arg)
1342 && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg))
1343 == REFERENCE_TYPE
1344 && TREE_ADDRESSABLE
1345 (TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg))))
1347 tree orig_arg = node->simdclone->args[i].orig_arg;
1348 tree def = ssa_default_def (cfun, orig_arg);
1349 if (def && !has_zero_uses (def))
1351 iter1 = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (orig_arg)));
1352 gimple_add_tmp_var (iter1);
1353 gsi = gsi_after_labels (entry_bb);
1354 g = gimple_build_assign (iter1, build_simple_mem_ref (def));
1355 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1356 gsi = gsi_after_labels (body_bb);
1357 g = gimple_build_assign (build_simple_mem_ref (def), iter1);
1358 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1361 else if (node->simdclone->args[i].alignment
1362 && node->simdclone->args[i].arg_type
1363 == SIMD_CLONE_ARG_TYPE_UNIFORM
1364 && (node->simdclone->args[i].alignment
1365 & (node->simdclone->args[i].alignment - 1)) == 0
1366 && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg))
1367 == POINTER_TYPE)
1369 unsigned int alignment = node->simdclone->args[i].alignment;
1370 tree orig_arg = node->simdclone->args[i].orig_arg;
1371 tree def = ssa_default_def (cfun, orig_arg);
1372 if (def && !has_zero_uses (def))
1374 tree fn = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
1375 gimple_seq seq = NULL;
1376 bool need_cvt = false;
1377 gcall *call
1378 = gimple_build_call (fn, 2, def, size_int (alignment));
1379 g = call;
1380 if (!useless_type_conversion_p (TREE_TYPE (orig_arg),
1381 ptr_type_node))
1382 need_cvt = true;
1383 tree t = make_ssa_name (need_cvt ? ptr_type_node : orig_arg);
1384 gimple_call_set_lhs (g, t);
1385 gimple_seq_add_stmt_without_update (&seq, g);
1386 if (need_cvt)
1388 t = make_ssa_name (orig_arg);
1389 g = gimple_build_assign (t, NOP_EXPR, gimple_call_lhs (g));
1390 gimple_seq_add_stmt_without_update (&seq, g);
1392 gsi_insert_seq_on_edge_immediate
1393 (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
1395 entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1396 node->create_edge (cgraph_node::get_create (fn),
1397 call, entry_bb->count);
1399 imm_use_iterator iter;
1400 use_operand_p use_p;
1401 gimple *use_stmt;
1402 tree repl = gimple_get_lhs (g);
1403 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1404 if (is_gimple_debug (use_stmt) || use_stmt == call)
1405 continue;
1406 else
1407 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1408 SET_USE (use_p, repl);
1411 else if ((node->simdclone->args[i].arg_type
1412 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
1413 || (node->simdclone->args[i].arg_type
1414 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP)
1415 || (node->simdclone->args[i].arg_type
1416 == SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP)
1417 || (node->simdclone->args[i].arg_type
1418 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP))
1420 tree orig_arg = node->simdclone->args[i].orig_arg;
1421 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1422 || POINTER_TYPE_P (TREE_TYPE (orig_arg)));
1423 tree def = NULL_TREE;
1424 if (TREE_ADDRESSABLE (orig_arg))
1426 def = make_ssa_name (TREE_TYPE (orig_arg));
1427 iter1 = make_ssa_name (TREE_TYPE (orig_arg));
1428 if (incr_bb)
1429 iter2 = make_ssa_name (TREE_TYPE (orig_arg));
1430 gsi = gsi_after_labels (entry_bb);
1431 g = gimple_build_assign (def, orig_arg);
1432 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1434 else
1436 def = ssa_default_def (cfun, orig_arg);
1437 if (!def || has_zero_uses (def))
1438 def = NULL_TREE;
1439 else
1441 iter1 = make_ssa_name (orig_arg);
1442 if (incr_bb)
1443 iter2 = make_ssa_name (orig_arg);
1446 if (def)
1448 phi = create_phi_node (iter1, body_bb);
1449 add_phi_arg (phi, def, preheader_edge, UNKNOWN_LOCATION);
1450 if (incr_bb)
1452 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1453 enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1454 ? PLUS_EXPR : POINTER_PLUS_EXPR;
1455 tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1456 ? TREE_TYPE (orig_arg) : sizetype;
1457 tree addcst = simd_clone_linear_addend (node, i, addtype,
1458 entry_bb);
1459 gsi = gsi_last_bb (incr_bb);
1460 g = gimple_build_assign (iter2, code, iter1, addcst);
1461 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1464 imm_use_iterator iter;
1465 use_operand_p use_p;
1466 gimple *use_stmt;
1467 if (TREE_ADDRESSABLE (orig_arg))
1469 gsi = gsi_after_labels (body_bb);
1470 g = gimple_build_assign (orig_arg, iter1);
1471 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1473 else
1474 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1475 if (use_stmt == phi)
1476 continue;
1477 else
1478 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1479 SET_USE (use_p, iter1);
1482 else if (node->simdclone->args[i].arg_type
1483 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
1484 || (node->simdclone->args[i].arg_type
1485 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP))
1487 tree orig_arg = node->simdclone->args[i].orig_arg;
1488 tree def = ssa_default_def (cfun, orig_arg);
1489 gcc_assert (!TREE_ADDRESSABLE (orig_arg)
1490 && TREE_CODE (TREE_TYPE (orig_arg)) == REFERENCE_TYPE);
1491 if (def && !has_zero_uses (def))
1493 tree rtype = TREE_TYPE (TREE_TYPE (orig_arg));
1494 iter1 = make_ssa_name (orig_arg);
1495 if (incr_bb)
1496 iter2 = make_ssa_name (orig_arg);
1497 tree iter3 = make_ssa_name (rtype);
1498 tree iter4 = make_ssa_name (rtype);
1499 tree iter5 = incr_bb ? make_ssa_name (rtype) : NULL_TREE;
1500 gsi = gsi_after_labels (entry_bb);
1501 gimple *load
1502 = gimple_build_assign (iter3, build_simple_mem_ref (def));
1503 gsi_insert_before (&gsi, load, GSI_NEW_STMT);
1505 tree array = node->simdclone->args[i].simd_array;
1506 TREE_ADDRESSABLE (array) = 1;
1507 tree ptr = build_fold_addr_expr (array);
1508 phi = create_phi_node (iter1, body_bb);
1509 add_phi_arg (phi, ptr, preheader_edge, UNKNOWN_LOCATION);
1510 if (incr_bb)
1512 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1513 g = gimple_build_assign (iter2, POINTER_PLUS_EXPR, iter1,
1514 TYPE_SIZE_UNIT (TREE_TYPE (iter3)));
1515 gsi = gsi_last_bb (incr_bb);
1516 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1519 phi = create_phi_node (iter4, body_bb);
1520 add_phi_arg (phi, iter3, preheader_edge, UNKNOWN_LOCATION);
1521 if (incr_bb)
1523 add_phi_arg (phi, iter5, latch_edge, UNKNOWN_LOCATION);
1524 enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (iter3))
1525 ? PLUS_EXPR : POINTER_PLUS_EXPR;
1526 tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (iter3))
1527 ? TREE_TYPE (iter3) : sizetype;
1528 tree addcst = simd_clone_linear_addend (node, i, addtype,
1529 entry_bb);
1530 g = gimple_build_assign (iter5, code, iter4, addcst);
1531 gsi = gsi_last_bb (incr_bb);
1532 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1535 g = gimple_build_assign (build_simple_mem_ref (iter1), iter4);
1536 gsi = gsi_after_labels (body_bb);
1537 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1539 imm_use_iterator iter;
1540 use_operand_p use_p;
1541 gimple *use_stmt;
1542 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1543 if (use_stmt == load)
1544 continue;
1545 else
1546 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1547 SET_USE (use_p, iter1);
1549 if (!TYPE_READONLY (rtype) && incr_bb)
1551 tree v = make_ssa_name (rtype);
1552 tree aref = build4 (ARRAY_REF, rtype, array,
1553 size_zero_node, NULL_TREE,
1554 NULL_TREE);
1555 gsi = gsi_after_labels (new_exit_bb);
1556 g = gimple_build_assign (v, aref);
1557 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1558 g = gimple_build_assign (build_simple_mem_ref (def), v);
1559 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1564 calculate_dominance_info (CDI_DOMINATORS);
1565 if (loop)
1566 add_loop (loop, loop->header->loop_father);
1567 update_ssa (TODO_update_ssa);
1569 pop_cfun ();
1572 /* If the function in NODE is tagged as an elemental SIMD function,
1573 create the appropriate SIMD clones. */
1575 void
1576 expand_simd_clones (struct cgraph_node *node)
1578 tree attr = lookup_attribute ("omp declare simd",
1579 DECL_ATTRIBUTES (node->decl));
1580 if (attr == NULL_TREE
1581 || node->global.inlined_to
1582 || lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
1583 return;
1585 /* Ignore
1586 #pragma omp declare simd
1587 extern int foo ();
1588 in C, there we don't know the argument types at all. */
1589 if (!node->definition
1590 && TYPE_ARG_TYPES (TREE_TYPE (node->decl)) == NULL_TREE)
1591 return;
1593 /* Call this before creating clone_info, as it might ggc_collect. */
1594 if (node->definition && node->has_gimple_body_p ())
1595 node->get_body ();
1599 /* Start with parsing the "omp declare simd" attribute(s). */
1600 bool inbranch_clause_specified;
1601 struct cgraph_simd_clone *clone_info
1602 = simd_clone_clauses_extract (node, TREE_VALUE (attr),
1603 &inbranch_clause_specified);
1604 if (clone_info == NULL)
1605 continue;
1607 int orig_simdlen = clone_info->simdlen;
1608 tree base_type = simd_clone_compute_base_data_type (node, clone_info);
1609 /* The target can return 0 (no simd clones should be created),
1610 1 (just one ISA of simd clones should be created) or higher
1611 count of ISA variants. In that case, clone_info is initialized
1612 for the first ISA variant. */
1613 int count
1614 = targetm.simd_clone.compute_vecsize_and_simdlen (node, clone_info,
1615 base_type, 0);
1616 if (count == 0)
1617 continue;
1619 /* Loop over all COUNT ISA variants, and if !INBRANCH_CLAUSE_SPECIFIED,
1620 also create one inbranch and one !inbranch clone of it. */
1621 for (int i = 0; i < count * 2; i++)
1623 struct cgraph_simd_clone *clone = clone_info;
1624 if (inbranch_clause_specified && (i & 1) != 0)
1625 continue;
1627 if (i != 0)
1629 clone = simd_clone_struct_alloc (clone_info->nargs
1630 + ((i & 1) != 0));
1631 simd_clone_struct_copy (clone, clone_info);
1632 /* Undo changes targetm.simd_clone.compute_vecsize_and_simdlen
1633 and simd_clone_adjust_argument_types did to the first
1634 clone's info. */
1635 clone->nargs -= clone_info->inbranch;
1636 clone->simdlen = orig_simdlen;
1637 /* And call the target hook again to get the right ISA. */
1638 targetm.simd_clone.compute_vecsize_and_simdlen (node, clone,
1639 base_type,
1640 i / 2);
1641 if ((i & 1) != 0)
1642 clone->inbranch = 1;
1645 /* simd_clone_mangle might fail if such a clone has been created
1646 already. */
1647 tree id = simd_clone_mangle (node, clone);
1648 if (id == NULL_TREE)
1649 continue;
1651 /* Only when we are sure we want to create the clone actually
1652 clone the function (or definitions) or create another
1653 extern FUNCTION_DECL (for prototypes without definitions). */
1654 struct cgraph_node *n = simd_clone_create (node);
1655 if (n == NULL)
1656 continue;
1658 n->simdclone = clone;
1659 clone->origin = node;
1660 clone->next_clone = NULL;
1661 if (node->simd_clones == NULL)
1663 clone->prev_clone = n;
1664 node->simd_clones = n;
1666 else
1668 clone->prev_clone = node->simd_clones->simdclone->prev_clone;
1669 clone->prev_clone->simdclone->next_clone = n;
1670 node->simd_clones->simdclone->prev_clone = n;
1672 symtab->change_decl_assembler_name (n->decl, id);
1673 /* And finally adjust the return type, parameters and for
1674 definitions also function body. */
1675 if (node->definition)
1676 simd_clone_adjust (n);
1677 else
1679 simd_clone_adjust_return_type (n);
1680 simd_clone_adjust_argument_types (n);
1684 while ((attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr))));
1687 /* Entry point for IPA simd clone creation pass. */
1689 static unsigned int
1690 ipa_omp_simd_clone (void)
1692 struct cgraph_node *node;
1693 FOR_EACH_FUNCTION (node)
1694 expand_simd_clones (node);
1695 return 0;
1698 namespace {
1700 const pass_data pass_data_omp_simd_clone =
1702 SIMPLE_IPA_PASS, /* type */
1703 "simdclone", /* name */
1704 OPTGROUP_OMP, /* optinfo_flags */
1705 TV_NONE, /* tv_id */
1706 ( PROP_ssa | PROP_cfg ), /* properties_required */
1707 0, /* properties_provided */
1708 0, /* properties_destroyed */
1709 0, /* todo_flags_start */
1710 0, /* todo_flags_finish */
1713 class pass_omp_simd_clone : public simple_ipa_opt_pass
1715 public:
1716 pass_omp_simd_clone(gcc::context *ctxt)
1717 : simple_ipa_opt_pass(pass_data_omp_simd_clone, ctxt)
1720 /* opt_pass methods: */
1721 virtual bool gate (function *);
1722 virtual unsigned int execute (function *) { return ipa_omp_simd_clone (); }
1725 bool
1726 pass_omp_simd_clone::gate (function *)
1728 return targetm.simd_clone.compute_vecsize_and_simdlen != NULL;
1731 } // anon namespace
1733 simple_ipa_opt_pass *
1734 make_pass_omp_simd_clone (gcc::context *ctxt)
1736 return new pass_omp_simd_clone (ctxt);