docs: Document that __builtin_assoc_barrier also can be used for FMAs [PR115023]
[official-gcc.git] / gcc / omp-simd-clone.cc
blob14a37f709482e6f9e8eb395dc2a52eb712346342
1 /* OMP constructs' SIMD clone supporting code.
3 Copyright (C) 2005-2024 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 #define INCLUDE_MEMORY
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "alloc-pool.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "cgraph.h"
34 #include "pretty-print.h"
35 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "cfganal.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-walk.h"
43 #include "langhooks.h"
44 #include "tree-cfg.h"
45 #include "tree-into-ssa.h"
46 #include "tree-dfa.h"
47 #include "cfgloop.h"
48 #include "symbol-summary.h"
49 #include "ipa-param-manipulation.h"
50 #include "tree-eh.h"
51 #include "varasm.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "omp-simd-clone.h"
55 #include "omp-low.h"
56 #include "omp-general.h"
58 /* Print debug info for ok_for_auto_simd_clone to the dump file, logging
59 failure reason EXCUSE for function DECL. Always returns false. */
60 static bool
61 auto_simd_fail (tree decl, const char *excuse)
63 if (dump_file && (dump_flags & TDF_DETAILS))
64 fprintf (dump_file, "\nNot auto-cloning %s because %s\n",
65 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
66 excuse);
67 return false;
70 /* Helper function for ok_for_auto_simd_clone; return false if the statement
71 violates restrictions for an "omp declare simd" function. Specifically,
72 the function must not
73 - throw or call setjmp/longjmp
74 - write memory that could alias parallel calls
75 - read volatile memory
76 - include openmp directives or calls
77 - call functions that might do those things */
79 static bool
80 auto_simd_check_stmt (gimple *stmt, tree outer)
82 tree decl;
84 switch (gimple_code (stmt))
86 case GIMPLE_CALL:
88 /* Calls to functions that are CONST or PURE are ok, even if they
89 are internal functions without a decl. Reject other internal
90 functions. */
91 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
92 break;
93 if (gimple_call_internal_p (stmt))
94 return auto_simd_fail (outer,
95 "body contains internal function call");
97 decl = gimple_call_fndecl (stmt);
99 /* We can't know whether indirect calls are safe. */
100 if (decl == NULL_TREE)
101 return auto_simd_fail (outer, "body contains indirect call");
103 /* Calls to functions that are already marked "omp declare simd" are
104 OK. */
105 if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (decl)))
106 break;
108 /* Let recursive calls to the current function through. */
109 if (decl == outer)
110 break;
112 /* Other function calls are not permitted. This covers all calls to
113 the libgomp API and setjmp/longjmp, too, as well as things like
114 __cxa_throw_ related to exception handling. */
115 return auto_simd_fail (outer, "body contains unsafe function call");
117 /* Reject EH-related constructs. Most of the EH gimple codes are
118 already lowered by the time this pass runs during IPA.
119 GIMPLE_EH_DISPATCH and GIMPLE_RESX remain and are lowered by
120 pass_lower_eh_dispatch and pass_lower_resx, respectively; those
121 passes run later. */
122 case GIMPLE_EH_DISPATCH:
123 case GIMPLE_RESX:
124 return auto_simd_fail (outer, "body contains EH constructs");
126 /* Asms are not permitted since we don't know what they do. */
127 case GIMPLE_ASM:
128 return auto_simd_fail (outer, "body contains inline asm");
130 default:
131 break;
134 /* Memory writes are not permitted.
135 FIXME: this could be relaxed a little to permit writes to
136 function-local variables that could not alias other instances
137 of the function running in parallel. */
138 if (gimple_store_p (stmt))
139 return auto_simd_fail (outer, "body includes memory write");
141 /* Volatile reads are not permitted. */
142 if (gimple_has_volatile_ops (stmt))
143 return auto_simd_fail (outer, "body includes volatile op");
145 /* Otherwise OK. */
146 return true;
149 /* Helper function for ok_for_auto_simd_clone: return true if type T is
150 plausible for a cloneable function argument or return type. */
151 static bool
152 plausible_type_for_simd_clone (tree t)
154 if (VOID_TYPE_P (t))
155 return true;
156 else if (RECORD_OR_UNION_TYPE_P (t) || !is_a <scalar_mode> (TYPE_MODE (t)))
157 /* Small record/union types may fit into a scalar mode, but are
158 still not suitable. */
159 return false;
160 else if (TYPE_ATOMIC (t))
161 /* Atomic types trigger warnings in simd_clone_clauses_extract. */
162 return false;
163 else
164 return true;
167 /* Check if the function NODE appears suitable for auto-annotation
168 with "declare simd". */
170 static bool
171 ok_for_auto_simd_clone (struct cgraph_node *node)
173 tree decl = node->decl;
174 tree t;
175 basic_block bb;
177 /* Nothing to do if the function isn't a definition or doesn't
178 have a body. */
179 if (!node->definition || !node->has_gimple_body_p ())
180 return auto_simd_fail (decl, "no definition or body");
182 /* No point in trying to generate implicit clones if the function
183 isn't used in the compilation unit. */
184 if (!node->callers)
185 return auto_simd_fail (decl, "function is not used");
187 /* Nothing to do if the function already has the "omp declare simd"
188 attribute, is marked noclone, or is not "omp declare target". */
189 if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (decl))
190 || lookup_attribute ("noclone", DECL_ATTRIBUTES (decl))
191 || !lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
192 return auto_simd_fail (decl, "incompatible attributes");
194 /* Check whether the function is restricted host/nohost via the
195 "omp declare target device_type" clause, and that doesn't match
196 what we're compiling for. Internally, these translate into
197 "omp declare target [no]host" attributes on the decl; "any"
198 translates into both attributes, but the default (which is supposed
199 to be equivalent to "any") is neither. */
200 tree host = lookup_attribute ("omp declare target host",
201 DECL_ATTRIBUTES (decl));
202 tree nohost = lookup_attribute ("omp declare target nohost",
203 DECL_ATTRIBUTES (decl));
204 #ifdef ACCEL_COMPILER
205 if (host && !nohost)
206 return auto_simd_fail (decl, "device doesn't match for accel compiler");
207 #else
208 if (nohost && !host)
209 return auto_simd_fail (decl, "device doesn't match for host compiler");
210 #endif
212 /* Backends will check for vectorizable arguments/return types in a
213 target-specific way, but we can immediately filter out functions
214 that have implausible argument/return types. */
215 t = TREE_TYPE (TREE_TYPE (decl));
216 if (!plausible_type_for_simd_clone (t))
217 return auto_simd_fail (decl, "return type fails sniff test");
219 if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
221 for (tree temp = TYPE_ARG_TYPES (TREE_TYPE (decl));
222 temp; temp = TREE_CHAIN (temp))
224 t = TREE_VALUE (temp);
225 if (!plausible_type_for_simd_clone (t))
226 return auto_simd_fail (decl, "argument type fails sniff test");
229 else if (DECL_ARGUMENTS (decl))
231 for (tree temp = DECL_ARGUMENTS (decl); temp; temp = DECL_CHAIN (temp))
233 t = TREE_TYPE (temp);
234 if (!plausible_type_for_simd_clone (t))
235 return auto_simd_fail (decl, "argument type fails sniff test");
238 else
239 return auto_simd_fail (decl, "function has no arguments");
241 /* Scan the function body to see if it is suitable for SIMD-ization. */
242 node->get_body ();
244 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (decl))
246 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
247 gsi_next (&gsi))
248 if (!auto_simd_check_stmt (gsi_stmt (gsi), decl))
249 return false;
252 /* All is good. */
253 if (dump_file)
254 fprintf (dump_file, "\nMarking %s for auto-cloning\n",
255 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
256 return true;
259 /* Allocate a fresh `simd_clone' and return it. NARGS is the number
260 of arguments to reserve space for. */
262 static struct cgraph_simd_clone *
263 simd_clone_struct_alloc (int nargs)
265 struct cgraph_simd_clone *clone_info;
266 size_t len = (sizeof (struct cgraph_simd_clone)
267 + nargs * sizeof (struct cgraph_simd_clone_arg));
268 clone_info = (struct cgraph_simd_clone *)
269 ggc_internal_cleared_alloc (len);
270 return clone_info;
273 /* Make a copy of the `struct cgraph_simd_clone' in FROM to TO. */
275 static inline void
276 simd_clone_struct_copy (struct cgraph_simd_clone *to,
277 struct cgraph_simd_clone *from)
279 memcpy (to, from, (sizeof (struct cgraph_simd_clone)
280 + ((from->nargs - from->inbranch)
281 * sizeof (struct cgraph_simd_clone_arg))));
284 /* Fill an empty vector ARGS with parameter types of function FNDECL. This
285 uses TYPE_ARG_TYPES if available, otherwise falls back to types of
286 DECL_ARGUMENTS types. */
288 static void
289 simd_clone_vector_of_formal_parm_types (vec<tree> *args, tree fndecl)
291 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
293 push_function_arg_types (args, TREE_TYPE (fndecl));
294 return;
296 push_function_arg_decls (args, fndecl);
297 unsigned int i;
298 tree arg;
299 FOR_EACH_VEC_ELT (*args, i, arg)
300 (*args)[i] = TREE_TYPE ((*args)[i]);
303 /* Given a simd function in NODE, extract the simd specific
304 information from the OMP clauses passed in CLAUSES, and return
305 the struct cgraph_simd_clone * if it should be cloned. *INBRANCH_SPECIFIED
306 is set to TRUE if the `inbranch' or `notinbranch' clause specified,
307 otherwise set to FALSE. */
309 static struct cgraph_simd_clone *
310 simd_clone_clauses_extract (struct cgraph_node *node, tree clauses,
311 bool *inbranch_specified)
313 auto_vec<tree> args;
314 simd_clone_vector_of_formal_parm_types (&args, node->decl);
315 tree t;
316 int n;
317 *inbranch_specified = false;
319 n = args.length ();
320 if (n > 0 && args.last () == void_type_node)
321 n--;
323 /* Allocate one more than needed just in case this is an in-branch
324 clone which will require a mask argument. */
325 struct cgraph_simd_clone *clone_info = simd_clone_struct_alloc (n + 1);
326 clone_info->nargs = n;
328 if (!clauses)
329 goto out;
331 clauses = TREE_VALUE (clauses);
332 if (!clauses || TREE_CODE (clauses) != OMP_CLAUSE)
333 goto out;
335 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
337 switch (OMP_CLAUSE_CODE (t))
339 case OMP_CLAUSE_INBRANCH:
340 clone_info->inbranch = 1;
341 *inbranch_specified = true;
342 break;
343 case OMP_CLAUSE_NOTINBRANCH:
344 clone_info->inbranch = 0;
345 *inbranch_specified = true;
346 break;
347 case OMP_CLAUSE_SIMDLEN:
348 clone_info->simdlen
349 = TREE_INT_CST_LOW (OMP_CLAUSE_SIMDLEN_EXPR (t));
350 break;
351 case OMP_CLAUSE_LINEAR:
353 tree decl = OMP_CLAUSE_DECL (t);
354 tree step = OMP_CLAUSE_LINEAR_STEP (t);
355 int argno = TREE_INT_CST_LOW (decl);
356 if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (t))
358 enum cgraph_simd_clone_arg_type arg_type;
359 if (TREE_CODE (args[argno]) == REFERENCE_TYPE)
360 switch (OMP_CLAUSE_LINEAR_KIND (t))
362 case OMP_CLAUSE_LINEAR_REF:
363 arg_type
364 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP;
365 break;
366 case OMP_CLAUSE_LINEAR_UVAL:
367 arg_type
368 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP;
369 break;
370 case OMP_CLAUSE_LINEAR_VAL:
371 case OMP_CLAUSE_LINEAR_DEFAULT:
372 arg_type
373 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP;
374 break;
375 default:
376 gcc_unreachable ();
378 else
379 arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP;
380 clone_info->args[argno].arg_type = arg_type;
381 clone_info->args[argno].linear_step = tree_to_shwi (step);
382 gcc_assert (clone_info->args[argno].linear_step >= 0
383 && clone_info->args[argno].linear_step < n);
385 else
387 if (POINTER_TYPE_P (args[argno]))
388 step = fold_convert (ssizetype, step);
389 if (!tree_fits_shwi_p (step))
391 warning_at (OMP_CLAUSE_LOCATION (t), OPT_Wopenmp,
392 "ignoring large linear step");
393 return NULL;
395 else if (integer_zerop (step))
397 warning_at (OMP_CLAUSE_LOCATION (t), OPT_Wopenmp,
398 "ignoring zero linear step");
399 return NULL;
401 else
403 enum cgraph_simd_clone_arg_type arg_type;
404 if (TREE_CODE (args[argno]) == REFERENCE_TYPE)
405 switch (OMP_CLAUSE_LINEAR_KIND (t))
407 case OMP_CLAUSE_LINEAR_REF:
408 arg_type
409 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP;
410 break;
411 case OMP_CLAUSE_LINEAR_UVAL:
412 arg_type
413 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP;
414 break;
415 case OMP_CLAUSE_LINEAR_VAL:
416 case OMP_CLAUSE_LINEAR_DEFAULT:
417 arg_type
418 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP;
419 break;
420 default:
421 gcc_unreachable ();
423 else
424 arg_type = SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP;
425 clone_info->args[argno].arg_type = arg_type;
426 clone_info->args[argno].linear_step = tree_to_shwi (step);
429 break;
431 case OMP_CLAUSE_UNIFORM:
433 tree decl = OMP_CLAUSE_DECL (t);
434 int argno = tree_to_uhwi (decl);
435 clone_info->args[argno].arg_type
436 = SIMD_CLONE_ARG_TYPE_UNIFORM;
437 break;
439 case OMP_CLAUSE_ALIGNED:
441 /* Ignore aligned (x) for declare simd, for the ABI we really
442 need an alignment specified. */
443 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (t) == NULL_TREE)
444 break;
445 tree decl = OMP_CLAUSE_DECL (t);
446 int argno = tree_to_uhwi (decl);
447 clone_info->args[argno].alignment
448 = TREE_INT_CST_LOW (OMP_CLAUSE_ALIGNED_ALIGNMENT (t));
449 break;
451 default:
452 break;
456 out:
457 if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (node->decl))))
459 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wopenmp,
460 "ignoring %<#pragma omp declare simd%> on function "
461 "with %<_Atomic%> qualified return type");
462 return NULL;
465 for (unsigned int argno = 0; argno < clone_info->nargs; argno++)
466 if (TYPE_ATOMIC (args[argno])
467 && clone_info->args[argno].arg_type != SIMD_CLONE_ARG_TYPE_UNIFORM)
469 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wopenmp,
470 "ignoring %<#pragma omp declare simd%> on function "
471 "with %<_Atomic%> qualified non-%<uniform%> argument");
472 args.release ();
473 return NULL;
476 return clone_info;
479 /* Given a SIMD clone in NODE, calculate the characteristic data
480 type and return the coresponding type. The characteristic data
481 type is computed as described in the Intel Vector ABI. */
483 static tree
484 simd_clone_compute_base_data_type (struct cgraph_node *node,
485 struct cgraph_simd_clone *clone_info)
487 tree type = integer_type_node;
488 tree fndecl = node->decl;
490 /* a) For non-void function, the characteristic data type is the
491 return type. */
492 if (TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE)
493 type = TREE_TYPE (TREE_TYPE (fndecl));
495 /* b) If the function has any non-uniform, non-linear parameters,
496 then the characteristic data type is the type of the first
497 such parameter. */
498 else
500 auto_vec<tree> map;
501 simd_clone_vector_of_formal_parm_types (&map, fndecl);
502 for (unsigned int i = 0; i < clone_info->nargs; ++i)
503 if (clone_info->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
505 type = map[i];
506 break;
510 /* c) If the characteristic data type determined by a) or b) above
511 is struct, union, or class type which is pass-by-value (except
512 for the type that maps to the built-in complex data type), the
513 characteristic data type is int. */
514 if (RECORD_OR_UNION_TYPE_P (type)
515 && !aggregate_value_p (type, NULL)
516 && TREE_CODE (type) != COMPLEX_TYPE)
517 return integer_type_node;
519 /* d) If none of the above three classes is applicable, the
520 characteristic data type is int. */
522 return type;
524 /* e) For Intel Xeon Phi native and offload compilation, if the
525 resulting characteristic data type is 8-bit or 16-bit integer
526 data type, the characteristic data type is int. */
527 /* Well, we don't handle Xeon Phi yet. */
530 static tree
531 simd_clone_mangle (struct cgraph_node *node,
532 struct cgraph_simd_clone *clone_info)
534 char vecsize_mangle = clone_info->vecsize_mangle;
535 char mask = clone_info->inbranch ? 'M' : 'N';
536 poly_uint64 simdlen = clone_info->simdlen;
537 unsigned int n;
538 pretty_printer pp;
540 gcc_assert (vecsize_mangle && maybe_ne (simdlen, 0U));
542 pp_string (&pp, "_ZGV");
543 pp_character (&pp, vecsize_mangle);
544 pp_character (&pp, mask);
545 /* For now, simdlen is always constant, while variable simdlen pp 'n'. */
546 unsigned int len = simdlen.to_constant ();
547 pp_decimal_int (&pp, (len));
549 for (n = 0; n < clone_info->nargs; ++n)
551 struct cgraph_simd_clone_arg arg = clone_info->args[n];
553 switch (arg.arg_type)
555 case SIMD_CLONE_ARG_TYPE_UNIFORM:
556 pp_character (&pp, 'u');
557 break;
558 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
559 pp_character (&pp, 'l');
560 goto mangle_linear;
561 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
562 pp_character (&pp, 'R');
563 goto mangle_linear;
564 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
565 pp_character (&pp, 'L');
566 goto mangle_linear;
567 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
568 pp_character (&pp, 'U');
569 goto mangle_linear;
570 mangle_linear:
571 gcc_assert (arg.linear_step != 0);
572 if (arg.linear_step > 1)
573 pp_unsigned_wide_integer (&pp, arg.linear_step);
574 else if (arg.linear_step < 0)
576 pp_character (&pp, 'n');
577 pp_unsigned_wide_integer (&pp, (-(unsigned HOST_WIDE_INT)
578 arg.linear_step));
580 break;
581 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
582 pp_string (&pp, "ls");
583 pp_unsigned_wide_integer (&pp, arg.linear_step);
584 break;
585 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
586 pp_string (&pp, "Rs");
587 pp_unsigned_wide_integer (&pp, arg.linear_step);
588 break;
589 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
590 pp_string (&pp, "Ls");
591 pp_unsigned_wide_integer (&pp, arg.linear_step);
592 break;
593 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
594 pp_string (&pp, "Us");
595 pp_unsigned_wide_integer (&pp, arg.linear_step);
596 break;
597 default:
598 pp_character (&pp, 'v');
600 if (arg.alignment)
602 pp_character (&pp, 'a');
603 pp_decimal_int (&pp, arg.alignment);
607 pp_underscore (&pp);
608 const char *str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl));
609 if (*str == '*')
610 ++str;
611 pp_string (&pp, str);
612 str = pp_formatted_text (&pp);
614 /* If there already is a SIMD clone with the same mangled name, don't
615 add another one. This can happen e.g. for
616 #pragma omp declare simd
617 #pragma omp declare simd simdlen(8)
618 int foo (int, int);
619 if the simdlen is assumed to be 8 for the first one, etc. */
620 for (struct cgraph_node *clone = node->simd_clones; clone;
621 clone = clone->simdclone->next_clone)
622 if (id_equal (DECL_ASSEMBLER_NAME (clone->decl), str))
623 return NULL_TREE;
625 return get_identifier (str);
628 /* Create a simd clone of OLD_NODE and return it. If FORCE_LOCAL is true,
629 create it as a local symbol, otherwise copy the symbol linkage and
630 visibility attributes from OLD_NODE. */
632 static struct cgraph_node *
633 simd_clone_create (struct cgraph_node *old_node, bool force_local)
635 struct cgraph_node *new_node;
636 if (old_node->definition)
638 if (!old_node->has_gimple_body_p ())
639 return NULL;
640 old_node->get_body ();
641 new_node = old_node->create_version_clone_with_body (vNULL, NULL, NULL,
642 NULL, NULL,
643 "simdclone");
645 else
647 tree old_decl = old_node->decl;
648 tree new_decl = copy_node (old_node->decl);
649 DECL_NAME (new_decl) = clone_function_name_numbered (old_decl,
650 "simdclone");
651 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
652 SET_DECL_RTL (new_decl, NULL);
653 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
654 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
655 new_node = old_node->create_version_clone (new_decl, vNULL, NULL);
656 if (old_node->in_other_partition)
657 new_node->in_other_partition = 1;
659 if (new_node == NULL)
660 return new_node;
662 set_decl_built_in_function (new_node->decl, NOT_BUILT_IN, 0);
663 if (force_local)
665 TREE_PUBLIC (new_node->decl) = 0;
666 DECL_COMDAT (new_node->decl) = 0;
667 DECL_WEAK (new_node->decl) = 0;
668 DECL_EXTERNAL (new_node->decl) = 0;
669 DECL_VISIBILITY_SPECIFIED (new_node->decl) = 0;
670 DECL_VISIBILITY (new_node->decl) = VISIBILITY_DEFAULT;
671 DECL_DLLIMPORT_P (new_node->decl) = 0;
673 else
675 TREE_PUBLIC (new_node->decl) = TREE_PUBLIC (old_node->decl);
676 DECL_COMDAT (new_node->decl) = DECL_COMDAT (old_node->decl);
677 DECL_WEAK (new_node->decl) = DECL_WEAK (old_node->decl);
678 DECL_EXTERNAL (new_node->decl) = DECL_EXTERNAL (old_node->decl);
679 DECL_VISIBILITY_SPECIFIED (new_node->decl)
680 = DECL_VISIBILITY_SPECIFIED (old_node->decl);
681 DECL_VISIBILITY (new_node->decl) = DECL_VISIBILITY (old_node->decl);
682 DECL_DLLIMPORT_P (new_node->decl) = DECL_DLLIMPORT_P (old_node->decl);
683 if (DECL_ONE_ONLY (old_node->decl))
684 make_decl_one_only (new_node->decl,
685 DECL_ASSEMBLER_NAME (new_node->decl));
687 /* The method cgraph_version_clone_with_body () will force the new
688 symbol local. Undo this, and inherit external visibility from
689 the old node. */
690 new_node->local = old_node->local;
691 new_node->externally_visible = old_node->externally_visible;
692 new_node->calls_declare_variant_alt
693 = old_node->calls_declare_variant_alt;
696 /* Mark clones with internal linkage as gc'able, so they will not be
697 emitted unless the vectorizer can actually use them. */
698 if (!TREE_PUBLIC (new_node->decl))
699 new_node->gc_candidate = true;
701 return new_node;
704 /* Adjust the return type of the given function to its appropriate
705 vector counterpart. */
707 static void
708 simd_clone_adjust_return_type (struct cgraph_node *node)
710 tree fndecl = node->decl;
711 tree orig_rettype = TREE_TYPE (TREE_TYPE (fndecl));
712 poly_uint64 veclen;
713 tree t;
715 /* Adjust the function return type. */
716 if (orig_rettype == void_type_node)
717 return;
718 t = TREE_TYPE (TREE_TYPE (fndecl));
719 if (INTEGRAL_TYPE_P (t) || POINTER_TYPE_P (t))
720 veclen = node->simdclone->vecsize_int;
721 else
722 veclen = node->simdclone->vecsize_float;
723 if (known_eq (veclen, 0U))
724 veclen = node->simdclone->simdlen;
725 else
726 veclen = exact_div (veclen, GET_MODE_BITSIZE (SCALAR_TYPE_MODE (t)));
727 if (multiple_p (veclen, node->simdclone->simdlen))
728 veclen = node->simdclone->simdlen;
729 if (POINTER_TYPE_P (t))
730 t = pointer_sized_int_node;
731 if (known_eq (veclen, node->simdclone->simdlen))
732 t = build_vector_type (t, node->simdclone->simdlen);
733 else
735 t = build_vector_type (t, veclen);
736 t = build_array_type_nelts (t, exact_div (node->simdclone->simdlen,
737 veclen));
739 TREE_TYPE (TREE_TYPE (fndecl)) = t;
742 /* Each vector argument has a corresponding array to be used locally
743 as part of the eventual loop. Create such temporary array and
744 return it.
746 PREFIX is the prefix to be used for the temporary.
748 TYPE is the inner element type.
750 SIMDLEN is the number of elements. */
752 static tree
753 create_tmp_simd_array (const char *prefix, tree type, poly_uint64 simdlen)
755 tree atype = build_array_type_nelts (type, simdlen);
756 tree avar = create_tmp_var_raw (atype, prefix);
757 gimple_add_tmp_var (avar);
758 return avar;
761 /* Modify the function argument types to their corresponding vector
762 counterparts if appropriate. Also, create one array for each simd
763 argument to be used locally when using the function arguments as
764 part of the loop.
766 NODE is the function whose arguments are to be adjusted.
768 If NODE does not represent function definition, returns NULL. Otherwise
769 returns an adjustment class that will be filled describing how the argument
770 declarations will be remapped. New arguments which are not to be remapped
771 are marked with USER_FLAG. */
773 static void
774 simd_clone_adjust_argument_types (struct cgraph_node *node)
776 auto_vec<tree> args;
778 if (node->definition)
779 push_function_arg_decls (&args, node->decl);
780 else
781 simd_clone_vector_of_formal_parm_types (&args, node->decl);
782 struct cgraph_simd_clone *sc = node->simdclone;
783 unsigned i, k;
784 poly_uint64 veclen;
785 auto_vec<tree> new_params;
787 for (i = 0; i < sc->nargs; ++i)
789 tree parm = NULL_TREE;
790 tree parm_type = NULL_TREE;
791 if (i < args.length())
793 parm = args[i];
794 parm_type = node->definition ? TREE_TYPE (parm) : parm;
797 sc->args[i].orig_arg = node->definition ? parm : NULL_TREE;
798 sc->args[i].orig_type = parm_type;
800 switch (sc->args[i].arg_type)
802 default:
803 new_params.safe_push (parm_type);
804 break;
805 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
806 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
807 new_params.safe_push (parm_type);
808 if (node->definition)
809 sc->args[i].simd_array
810 = create_tmp_simd_array (IDENTIFIER_POINTER (DECL_NAME (parm)),
811 TREE_TYPE (parm_type),
812 sc->simdlen);
813 break;
814 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
815 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
816 case SIMD_CLONE_ARG_TYPE_VECTOR:
817 if (INTEGRAL_TYPE_P (parm_type) || POINTER_TYPE_P (parm_type))
818 veclen = sc->vecsize_int;
819 else
820 veclen = sc->vecsize_float;
821 if (known_eq (veclen, 0U))
822 veclen = sc->simdlen;
823 else
824 veclen
825 = exact_div (veclen,
826 GET_MODE_BITSIZE (SCALAR_TYPE_MODE (parm_type)));
827 if (multiple_p (veclen, sc->simdlen))
828 veclen = sc->simdlen;
829 tree vtype;
830 if (POINTER_TYPE_P (parm_type))
831 vtype = build_vector_type (pointer_sized_int_node, veclen);
832 else
833 vtype = build_vector_type (parm_type, veclen);
834 sc->args[i].vector_type = vtype;
835 k = vector_unroll_factor (sc->simdlen, veclen);
836 for (unsigned j = 0; j < k; j++)
837 new_params.safe_push (vtype);
839 if (node->definition)
840 sc->args[i].simd_array
841 = create_tmp_simd_array (DECL_NAME (parm)
842 ? IDENTIFIER_POINTER (DECL_NAME (parm))
843 : NULL, parm_type, sc->simdlen);
847 if (sc->inbranch)
849 tree base_type = simd_clone_compute_base_data_type (sc->origin, sc);
850 tree mask_type;
851 if (INTEGRAL_TYPE_P (base_type) || POINTER_TYPE_P (base_type))
852 veclen = sc->vecsize_int;
853 else
854 veclen = sc->vecsize_float;
855 if (known_eq (veclen, 0U))
856 veclen = sc->simdlen;
857 else
858 veclen = exact_div (veclen,
859 GET_MODE_BITSIZE (SCALAR_TYPE_MODE (base_type)));
860 if (multiple_p (veclen, sc->simdlen))
861 veclen = sc->simdlen;
862 if (sc->mask_mode != VOIDmode)
863 mask_type
864 = lang_hooks.types.type_for_mode (sc->mask_mode, 1);
865 else if (POINTER_TYPE_P (base_type))
866 mask_type = build_vector_type (pointer_sized_int_node, veclen);
867 else
868 mask_type = build_vector_type (base_type, veclen);
870 k = vector_unroll_factor (sc->simdlen, veclen);
872 /* We have previously allocated one extra entry for the mask. Use
873 it and fill it. */
874 sc->nargs++;
875 if (sc->mask_mode != VOIDmode)
876 base_type = boolean_type_node;
877 if (node->definition)
879 sc->args[i].orig_arg
880 = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL, base_type);
881 if (sc->mask_mode == VOIDmode)
882 sc->args[i].simd_array
883 = create_tmp_simd_array ("mask", base_type, sc->simdlen);
884 else if (k > 1)
885 sc->args[i].simd_array
886 = create_tmp_simd_array ("mask", mask_type, k);
887 else
888 sc->args[i].simd_array = NULL_TREE;
890 sc->args[i].orig_type = base_type;
891 sc->args[i].arg_type = SIMD_CLONE_ARG_TYPE_MASK;
892 sc->args[i].vector_type = mask_type;
895 if (!node->definition)
897 tree new_arg_types = NULL_TREE, new_reversed;
898 bool last_parm_void = false;
899 if (args.length () > 0 && args.last () == void_type_node)
900 last_parm_void = true;
902 gcc_assert (TYPE_ARG_TYPES (TREE_TYPE (node->decl)));
903 for (i = 0; i < new_params.length (); i++)
904 new_arg_types = tree_cons (NULL_TREE, new_params[i], new_arg_types);
905 new_reversed = nreverse (new_arg_types);
906 if (last_parm_void)
908 if (new_reversed)
909 TREE_CHAIN (new_arg_types) = void_list_node;
910 else
911 new_reversed = void_list_node;
913 TYPE_ARG_TYPES (TREE_TYPE (node->decl)) = new_reversed;
917 /* Initialize and copy the function arguments in NODE to their
918 corresponding local simd arrays. Returns a fresh gimple_seq with
919 the instruction sequence generated. */
921 static gimple_seq
922 simd_clone_init_simd_arrays (struct cgraph_node *node,
923 ipa_param_body_adjustments *adjustments)
925 gimple_seq seq = NULL;
926 unsigned i = 0, j = 0, k;
928 for (tree arg = DECL_ARGUMENTS (node->decl);
929 arg;
930 arg = DECL_CHAIN (arg), i++, j++)
932 ipa_adjusted_param adj = (*adjustments->m_adj_params)[j];
933 if (adj.op == IPA_PARAM_OP_COPY
934 || POINTER_TYPE_P (TREE_TYPE (arg)))
935 continue;
937 node->simdclone->args[i].vector_arg = arg;
939 tree array = node->simdclone->args[i].simd_array;
940 if (node->simdclone->mask_mode != VOIDmode
941 && adj.param_prefix_index == IPA_PARAM_PREFIX_MASK)
943 if (array == NULL_TREE)
944 continue;
945 unsigned int l
946 = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (array))));
947 for (k = 0; k <= l; k++)
949 if (k)
951 arg = DECL_CHAIN (arg);
952 j++;
954 tree t = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (array)),
955 array, size_int (k), NULL, NULL);
956 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
957 gimplify_and_add (t, &seq);
959 continue;
961 if (!VECTOR_TYPE_P (TREE_TYPE (arg))
962 || known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)),
963 node->simdclone->simdlen))
965 tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array)));
966 tree ptr = build_fold_addr_expr (array);
967 tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr,
968 build_int_cst (ptype, 0));
969 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
970 gimplify_and_add (t, &seq);
972 else
974 poly_uint64 simdlen = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg));
975 unsigned int times = vector_unroll_factor (node->simdclone->simdlen,
976 simdlen);
977 tree ptype = build_pointer_type (TREE_TYPE (TREE_TYPE (array)));
978 for (k = 0; k < times; k++)
980 tree ptr = build_fold_addr_expr (array);
981 int elemsize;
982 if (k)
984 arg = DECL_CHAIN (arg);
985 j++;
987 tree elemtype = TREE_TYPE (TREE_TYPE (arg));
988 elemsize = GET_MODE_SIZE (SCALAR_TYPE_MODE (elemtype));
989 tree t = build2 (MEM_REF, TREE_TYPE (arg), ptr,
990 build_int_cst (ptype, k * elemsize * simdlen));
991 t = build2 (MODIFY_EXPR, TREE_TYPE (t), t, arg);
992 gimplify_and_add (t, &seq);
996 return seq;
999 /* Callback info for ipa_simd_modify_stmt_ops below. */
1001 struct modify_stmt_info {
1002 ipa_param_body_adjustments *adjustments;
1003 gimple *stmt;
1004 gimple *after_stmt;
1005 /* True if the parent statement was modified by
1006 ipa_simd_modify_stmt_ops. */
1007 bool modified;
1010 /* Callback for walk_gimple_op.
1012 Adjust operands from a given statement as specified in the
1013 adjustments vector in the callback data. */
1015 static tree
1016 ipa_simd_modify_stmt_ops (tree *tp, int *walk_subtrees, void *data)
1018 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
1019 struct modify_stmt_info *info = (struct modify_stmt_info *) wi->info;
1020 tree *orig_tp = tp;
1021 if (TREE_CODE (*tp) == ADDR_EXPR)
1022 tp = &TREE_OPERAND (*tp, 0);
1024 if (TREE_CODE (*tp) == BIT_FIELD_REF
1025 || TREE_CODE (*tp) == IMAGPART_EXPR
1026 || TREE_CODE (*tp) == REALPART_EXPR)
1027 tp = &TREE_OPERAND (*tp, 0);
1029 tree repl = NULL_TREE;
1030 ipa_param_body_replacement *pbr = NULL;
1032 if (TREE_CODE (*tp) == PARM_DECL)
1034 pbr = info->adjustments->get_expr_replacement (*tp, true);
1035 if (pbr)
1036 repl = pbr->repl;
1038 else if (TYPE_P (*tp))
1039 *walk_subtrees = 0;
1041 if (repl)
1042 repl = unshare_expr (repl);
1043 else
1045 if (tp != orig_tp)
1047 *walk_subtrees = 0;
1048 bool modified = info->modified;
1049 info->modified = false;
1050 walk_tree (tp, ipa_simd_modify_stmt_ops, wi, wi->pset);
1051 if (!info->modified)
1053 info->modified = modified;
1054 return NULL_TREE;
1056 info->modified = modified;
1057 repl = *tp;
1059 else
1060 return NULL_TREE;
1063 if (tp != orig_tp)
1065 if (gimple_code (info->stmt) == GIMPLE_PHI
1066 && pbr
1067 && TREE_CODE (*orig_tp) == ADDR_EXPR
1068 && TREE_CODE (TREE_OPERAND (*orig_tp, 0)) == PARM_DECL
1069 && pbr->dummy)
1071 gcc_assert (TREE_CODE (pbr->dummy) == SSA_NAME);
1072 *orig_tp = pbr->dummy;
1073 info->modified = true;
1074 return NULL_TREE;
1077 repl = build_fold_addr_expr (repl);
1078 gimple *stmt;
1079 if (is_gimple_debug (info->stmt))
1081 tree vexpr = build_debug_expr_decl (TREE_TYPE (repl));
1082 stmt = gimple_build_debug_source_bind (vexpr, repl, NULL);
1083 repl = vexpr;
1085 else
1087 stmt = gimple_build_assign (make_ssa_name (TREE_TYPE (repl)), repl);
1088 repl = gimple_assign_lhs (stmt);
1090 gimple_stmt_iterator gsi;
1091 if (gimple_code (info->stmt) == GIMPLE_PHI)
1093 if (info->after_stmt)
1094 gsi = gsi_for_stmt (info->after_stmt);
1095 else
1096 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1097 /* Cache SSA_NAME for next time. */
1098 if (pbr
1099 && TREE_CODE (*orig_tp) == ADDR_EXPR
1100 && TREE_CODE (TREE_OPERAND (*orig_tp, 0)) == PARM_DECL)
1102 gcc_assert (!pbr->dummy);
1103 pbr->dummy = repl;
1106 else
1107 gsi = gsi_for_stmt (info->stmt);
1108 if (info->after_stmt)
1109 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
1110 else
1111 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1112 if (gimple_code (info->stmt) == GIMPLE_PHI)
1113 info->after_stmt = stmt;
1114 *orig_tp = repl;
1116 else if (!useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (repl)))
1118 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*tp), repl);
1119 *tp = vce;
1121 else
1122 *tp = repl;
1124 info->modified = true;
1125 return NULL_TREE;
1128 /* Traverse the function body and perform all modifications as
1129 described in ADJUSTMENTS. At function return, ADJUSTMENTS will be
1130 modified such that the replacement/reduction value will now be an
1131 offset into the corresponding simd_array.
1133 This function will replace all function argument uses with their
1134 corresponding simd array elements, and ajust the return values
1135 accordingly. */
1137 static void
1138 ipa_simd_modify_function_body (struct cgraph_node *node,
1139 ipa_param_body_adjustments *adjustments,
1140 tree retval_array, tree iter)
1142 basic_block bb;
1143 unsigned int i, j;
1146 /* Register replacements for every function argument use to an offset into
1147 the corresponding simd_array. */
1148 for (i = 0, j = 0; i < node->simdclone->nargs; ++i, ++j)
1150 if (!node->simdclone->args[i].vector_arg
1151 || (*adjustments->m_adj_params)[j].user_flag)
1152 continue;
1154 tree basetype = TREE_TYPE (node->simdclone->args[i].orig_arg);
1155 tree vectype = TREE_TYPE (node->simdclone->args[i].vector_arg);
1156 tree r = build4 (ARRAY_REF, basetype, node->simdclone->args[i].simd_array,
1157 iter, NULL_TREE, NULL_TREE);
1158 adjustments->register_replacement (&(*adjustments->m_adj_params)[j], r);
1160 if (multiple_p (node->simdclone->simdlen, TYPE_VECTOR_SUBPARTS (vectype)))
1161 j += vector_unroll_factor (node->simdclone->simdlen,
1162 TYPE_VECTOR_SUBPARTS (vectype)) - 1;
1164 adjustments->sort_replacements ();
1166 tree name;
1167 FOR_EACH_SSA_NAME (i, name, cfun)
1169 tree base_var;
1170 if (SSA_NAME_VAR (name)
1171 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
1172 && (base_var
1173 = adjustments->get_replacement_ssa_base (SSA_NAME_VAR (name))))
1175 if (SSA_NAME_IS_DEFAULT_DEF (name))
1177 tree old_decl = SSA_NAME_VAR (name);
1178 bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1179 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1180 tree repl = adjustments->lookup_replacement (old_decl, 0);
1181 gcc_checking_assert (repl);
1182 repl = unshare_expr (repl);
1183 set_ssa_default_def (cfun, old_decl, NULL_TREE);
1184 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var);
1185 SSA_NAME_IS_DEFAULT_DEF (name) = 0;
1186 gimple *stmt = gimple_build_assign (name, repl);
1187 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1189 else
1190 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, base_var);
1194 struct modify_stmt_info info;
1195 info.adjustments = adjustments;
1197 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
1199 gimple_stmt_iterator gsi;
1201 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1203 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
1204 int i, n = gimple_phi_num_args (phi);
1205 info.stmt = phi;
1206 info.after_stmt = NULL;
1207 struct walk_stmt_info wi;
1208 memset (&wi, 0, sizeof (wi));
1209 info.modified = false;
1210 wi.info = &info;
1211 for (i = 0; i < n; ++i)
1213 int walk_subtrees = 1;
1214 tree arg = gimple_phi_arg_def (phi, i);
1215 tree op = arg;
1216 ipa_simd_modify_stmt_ops (&op, &walk_subtrees, &wi);
1217 if (op != arg)
1219 SET_PHI_ARG_DEF (phi, i, op);
1220 gcc_assert (TREE_CODE (op) == SSA_NAME);
1221 if (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
1222 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1;
1227 gsi = gsi_start_bb (bb);
1228 while (!gsi_end_p (gsi))
1230 gimple *stmt = gsi_stmt (gsi);
1231 info.stmt = stmt;
1232 info.after_stmt = NULL;
1233 struct walk_stmt_info wi;
1235 memset (&wi, 0, sizeof (wi));
1236 info.modified = false;
1237 wi.info = &info;
1238 walk_gimple_op (stmt, ipa_simd_modify_stmt_ops, &wi);
1240 if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1242 tree retval = gimple_return_retval (return_stmt);
1243 edge e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
1244 e->flags |= EDGE_FALLTHRU;
1245 if (!retval)
1247 gsi_remove (&gsi, true);
1248 continue;
1251 /* Replace `return foo' with `retval_array[iter] = foo'. */
1252 tree ref = build4 (ARRAY_REF, TREE_TYPE (retval),
1253 retval_array, iter, NULL, NULL);
1254 stmt = gimple_build_assign (ref, retval);
1255 gsi_replace (&gsi, stmt, true);
1256 info.modified = true;
1259 if (info.modified)
1261 update_stmt (stmt);
1262 /* If the above changed the var of a debug bind into something
1263 different, remove the debug stmt. We could also for all the
1264 replaced parameters add VAR_DECLs for debug info purposes,
1265 add debug stmts for those to be the simd array accesses and
1266 replace debug stmt var operand with that var. Debugging of
1267 vectorized loops doesn't work too well, so don't bother for
1268 now. */
1269 if ((gimple_debug_bind_p (stmt)
1270 && !DECL_P (gimple_debug_bind_get_var (stmt)))
1271 || (gimple_debug_source_bind_p (stmt)
1272 && !DECL_P (gimple_debug_source_bind_get_var (stmt))))
1274 gsi_remove (&gsi, true);
1275 continue;
1277 if (maybe_clean_eh_stmt (stmt))
1278 gimple_purge_dead_eh_edges (gimple_bb (stmt));
1280 gsi_next (&gsi);
1285 /* Helper function of simd_clone_adjust, return linear step addend
1286 of Ith argument. */
1288 static tree
1289 simd_clone_linear_addend (struct cgraph_node *node, unsigned int i,
1290 tree addtype, basic_block entry_bb)
1292 tree ptype = NULL_TREE;
1293 switch (node->simdclone->args[i].arg_type)
1295 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
1296 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
1297 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
1298 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
1299 return build_int_cst (addtype, node->simdclone->args[i].linear_step);
1300 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
1301 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
1302 ptype = TREE_TYPE (node->simdclone->args[i].orig_arg);
1303 break;
1304 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
1305 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
1306 ptype = TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg));
1307 break;
1308 default:
1309 gcc_unreachable ();
1312 unsigned int idx = node->simdclone->args[i].linear_step;
1313 tree arg = node->simdclone->args[idx].orig_arg;
1314 gcc_assert (is_gimple_reg_type (TREE_TYPE (arg)));
1315 gimple_stmt_iterator gsi = gsi_after_labels (entry_bb);
1316 gimple *g;
1317 tree ret;
1318 if (is_gimple_reg (arg))
1319 ret = get_or_create_ssa_default_def (cfun, arg);
1320 else
1322 g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)), arg);
1323 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1324 ret = gimple_assign_lhs (g);
1326 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
1328 g = gimple_build_assign (make_ssa_name (TREE_TYPE (TREE_TYPE (arg))),
1329 build_simple_mem_ref (ret));
1330 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1331 ret = gimple_assign_lhs (g);
1333 if (!useless_type_conversion_p (addtype, TREE_TYPE (ret)))
1335 g = gimple_build_assign (make_ssa_name (addtype), NOP_EXPR, ret);
1336 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1337 ret = gimple_assign_lhs (g);
1339 if (POINTER_TYPE_P (ptype))
1341 tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptype));
1342 if (size && TREE_CODE (size) == INTEGER_CST)
1344 g = gimple_build_assign (make_ssa_name (addtype), MULT_EXPR,
1345 ret, fold_convert (addtype, size));
1346 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1347 ret = gimple_assign_lhs (g);
1350 return ret;
1353 /* Adjust the argument types in NODE to their appropriate vector
1354 counterparts. */
1356 static void
1357 simd_clone_adjust (struct cgraph_node *node)
1359 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1361 tree orig_rettype = TREE_TYPE (TREE_TYPE (node->decl));
1362 TREE_TYPE (node->decl) = build_distinct_type_copy (TREE_TYPE (node->decl));
1363 simd_clone_adjust_return_type (node);
1364 simd_clone_adjust_argument_types (node);
1365 targetm.simd_clone.adjust (node);
1366 tree retval = NULL_TREE;
1367 if (orig_rettype != void_type_node)
1369 poly_uint64 veclen;
1370 if (INTEGRAL_TYPE_P (orig_rettype) || POINTER_TYPE_P (orig_rettype))
1371 veclen = node->simdclone->vecsize_int;
1372 else
1373 veclen = node->simdclone->vecsize_float;
1374 if (known_eq (veclen, 0U))
1375 veclen = node->simdclone->simdlen;
1376 else
1377 veclen = exact_div (veclen,
1378 GET_MODE_BITSIZE (SCALAR_TYPE_MODE (orig_rettype)));
1379 if (multiple_p (veclen, node->simdclone->simdlen))
1380 veclen = node->simdclone->simdlen;
1382 retval = DECL_RESULT (node->decl);
1383 /* Adjust the DECL_RESULT. */
1384 TREE_TYPE (retval) = TREE_TYPE (TREE_TYPE (node->decl));
1385 relayout_decl (retval);
1387 tree atype = build_array_type_nelts (orig_rettype,
1388 node->simdclone->simdlen);
1389 if (maybe_ne (veclen, node->simdclone->simdlen))
1390 retval = build1 (VIEW_CONVERT_EXPR, atype, retval);
1391 else
1393 /* Set up a SIMD array to use as the return value. */
1394 retval = create_tmp_var_raw (atype, "retval");
1395 gimple_add_tmp_var (retval);
1399 struct cgraph_simd_clone *sc = node->simdclone;
1400 vec<ipa_adjusted_param, va_gc> *new_params = NULL;
1401 vec_safe_reserve (new_params, sc->nargs);
1402 unsigned i, j, k;
1403 for (i = 0; i < sc->nargs; ++i)
1405 ipa_adjusted_param adj;
1406 memset (&adj, 0, sizeof (adj));
1407 poly_uint64 veclen;
1408 tree elem_type;
1410 adj.base_index = i;
1411 adj.prev_clone_index = i;
1412 switch (sc->args[i].arg_type)
1414 default:
1415 /* No adjustment necessary for scalar arguments. */
1416 adj.op = IPA_PARAM_OP_COPY;
1417 break;
1418 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
1419 adj.op = IPA_PARAM_OP_COPY;
1420 break;
1421 case SIMD_CLONE_ARG_TYPE_MASK:
1422 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
1423 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
1424 case SIMD_CLONE_ARG_TYPE_VECTOR:
1425 if (sc->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK
1426 && sc->mask_mode != VOIDmode)
1427 elem_type = boolean_type_node;
1428 else
1429 elem_type = TREE_TYPE (sc->args[i].vector_type);
1430 if (INTEGRAL_TYPE_P (elem_type) || POINTER_TYPE_P (elem_type))
1431 veclen = sc->vecsize_int;
1432 else
1433 veclen = sc->vecsize_float;
1434 if (known_eq (veclen, 0U))
1435 veclen = sc->simdlen;
1436 else
1437 veclen
1438 = exact_div (veclen,
1439 GET_MODE_BITSIZE (SCALAR_TYPE_MODE (elem_type)));
1440 if (multiple_p (veclen, sc->simdlen))
1441 veclen = sc->simdlen;
1442 if (sc->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
1444 adj.user_flag = 1;
1445 adj.param_prefix_index = IPA_PARAM_PREFIX_MASK;
1447 else
1448 adj.param_prefix_index = IPA_PARAM_PREFIX_SIMD;
1449 adj.op = IPA_PARAM_OP_NEW;
1450 adj.type = sc->args[i].vector_type;
1451 k = vector_unroll_factor (sc->simdlen, veclen);
1452 for (j = 1; j < k; j++)
1454 vec_safe_push (new_params, adj);
1455 if (j == 1)
1457 memset (&adj, 0, sizeof (adj));
1458 adj.op = IPA_PARAM_OP_NEW;
1459 adj.user_flag = 1;
1460 if (sc->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
1461 adj.param_prefix_index = IPA_PARAM_PREFIX_MASK;
1462 else
1463 adj.param_prefix_index = IPA_PARAM_PREFIX_SIMD;
1464 adj.base_index = i;
1465 adj.prev_clone_index = i;
1466 adj.type = sc->args[i].vector_type;
1470 vec_safe_push (new_params, adj);
1472 ipa_param_body_adjustments *adjustments
1473 = new ipa_param_body_adjustments (new_params, node->decl);
1474 adjustments->modify_formal_parameters ();
1476 push_gimplify_context ();
1478 gimple_seq seq = simd_clone_init_simd_arrays (node, adjustments);
1480 /* Adjust all uses of vector arguments accordingly. Adjust all
1481 return values accordingly. */
1482 tree iter = create_tmp_var (unsigned_type_node, "iter");
1483 tree iter1 = make_ssa_name (iter);
1484 tree iter2 = NULL_TREE;
1485 ipa_simd_modify_function_body (node, adjustments, retval, iter1);
1486 delete adjustments;
1488 /* Initialize the iteration variable. */
1489 basic_block entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1490 basic_block body_bb = split_block_after_labels (entry_bb)->dest;
1491 gimple_stmt_iterator gsi = gsi_after_labels (entry_bb);
1492 /* Insert the SIMD array and iv initialization at function
1493 entry. */
1494 gsi_insert_seq_before (&gsi, seq, GSI_NEW_STMT);
1496 pop_gimplify_context (NULL);
1498 gimple *g;
1499 basic_block incr_bb = NULL;
1500 class loop *loop = NULL;
1502 /* Create a new BB right before the original exit BB, to hold the
1503 iteration increment and the condition/branch. */
1504 if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
1506 basic_block orig_exit = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), 0)->src;
1507 incr_bb = create_empty_bb (orig_exit);
1508 incr_bb->count = profile_count::zero ();
1509 add_bb_to_loop (incr_bb, body_bb->loop_father);
1510 while (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
1512 edge e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1513 redirect_edge_succ (e, incr_bb);
1514 incr_bb->count += e->count ();
1517 else if (node->simdclone->inbranch)
1519 incr_bb = create_empty_bb (entry_bb);
1520 incr_bb->count = profile_count::zero ();
1521 add_bb_to_loop (incr_bb, body_bb->loop_father);
1524 if (incr_bb)
1526 make_single_succ_edge (incr_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1527 gsi = gsi_last_bb (incr_bb);
1528 iter2 = make_ssa_name (iter);
1529 g = gimple_build_assign (iter2, PLUS_EXPR, iter1,
1530 build_int_cst (unsigned_type_node, 1));
1531 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1533 /* Mostly annotate the loop for the vectorizer (the rest is done
1534 below). */
1535 loop = alloc_loop ();
1536 cfun->has_force_vectorize_loops = true;
1537 /* For now, simlen is always constant. */
1538 loop->safelen = node->simdclone->simdlen.to_constant ();
1539 loop->force_vectorize = true;
1540 loop->header = body_bb;
1543 /* Branch around the body if the mask applies. */
1544 if (node->simdclone->inbranch)
1546 gsi = gsi_last_bb (loop->header);
1547 tree mask_array
1548 = node->simdclone->args[node->simdclone->nargs - 1].simd_array;
1549 tree mask;
1550 if (node->simdclone->mask_mode != VOIDmode)
1552 tree shift_cnt;
1553 if (mask_array == NULL_TREE)
1555 tree arg = node->simdclone->args[node->simdclone->nargs
1556 - 1].vector_arg;
1557 mask = get_or_create_ssa_default_def (cfun, arg);
1558 shift_cnt = iter1;
1560 else
1562 tree maskt = TREE_TYPE (mask_array);
1563 int c = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (maskt)));
1564 /* For now, c must be constant here. */
1565 c = exact_div (node->simdclone->simdlen, c + 1).to_constant ();
1566 int s = exact_log2 (c);
1567 gcc_assert (s > 0);
1568 c--;
1569 tree idx = make_ssa_name (TREE_TYPE (iter1));
1570 g = gimple_build_assign (idx, RSHIFT_EXPR, iter1,
1571 build_int_cst (NULL_TREE, s));
1572 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1573 mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array)));
1574 tree aref = build4 (ARRAY_REF,
1575 TREE_TYPE (TREE_TYPE (mask_array)),
1576 mask_array, idx, NULL, NULL);
1577 g = gimple_build_assign (mask, aref);
1578 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1579 shift_cnt = make_ssa_name (TREE_TYPE (iter1));
1580 g = gimple_build_assign (shift_cnt, BIT_AND_EXPR, iter1,
1581 build_int_cst (TREE_TYPE (iter1), c));
1582 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1584 tree shift_cnt_conv = shift_cnt;
1585 if (!useless_type_conversion_p (TREE_TYPE (mask),
1586 TREE_TYPE (shift_cnt)))
1588 shift_cnt_conv = make_ssa_name (TREE_TYPE (mask));
1589 g = gimple_build_assign (shift_cnt_conv, NOP_EXPR, shift_cnt);
1590 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1592 g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)),
1593 RSHIFT_EXPR, mask, shift_cnt_conv);
1594 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1595 mask = gimple_assign_lhs (g);
1596 g = gimple_build_assign (make_ssa_name (TREE_TYPE (mask)),
1597 BIT_AND_EXPR, mask,
1598 build_one_cst (TREE_TYPE (mask)));
1599 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1600 mask = gimple_assign_lhs (g);
1602 else
1604 mask = make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array)));
1605 tree aref = build4 (ARRAY_REF,
1606 TREE_TYPE (TREE_TYPE (mask_array)),
1607 mask_array, iter1, NULL, NULL);
1608 g = gimple_build_assign (mask, aref);
1609 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1610 int bitsize = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (aref)));
1611 if (!INTEGRAL_TYPE_P (TREE_TYPE (aref)))
1613 aref = build1 (VIEW_CONVERT_EXPR,
1614 build_nonstandard_integer_type (bitsize, 0),
1615 mask);
1616 mask = make_ssa_name (TREE_TYPE (aref));
1617 g = gimple_build_assign (mask, aref);
1618 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1622 g = gimple_build_cond (EQ_EXPR, mask, build_zero_cst (TREE_TYPE (mask)),
1623 NULL, NULL);
1624 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1625 edge e = make_edge (loop->header, incr_bb, EDGE_TRUE_VALUE);
1626 e->probability = profile_probability::unlikely ().guessed ();
1627 incr_bb->count += e->count ();
1628 edge fallthru = FALLTHRU_EDGE (loop->header);
1629 fallthru->flags = EDGE_FALSE_VALUE;
1630 fallthru->probability = profile_probability::likely ().guessed ();
1633 basic_block latch_bb = NULL;
1634 basic_block new_exit_bb = NULL;
1636 /* Generate the condition. */
1637 if (incr_bb)
1639 gsi = gsi_last_bb (incr_bb);
1640 g = gimple_build_cond (LT_EXPR, iter2,
1641 build_int_cst (unsigned_type_node,
1642 node->simdclone->simdlen),
1643 NULL, NULL);
1644 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1645 edge e = split_block (incr_bb, gsi_stmt (gsi));
1646 latch_bb = e->dest;
1647 new_exit_bb = split_block_after_labels (latch_bb)->dest;
1648 loop->latch = latch_bb;
1650 redirect_edge_succ (FALLTHRU_EDGE (latch_bb), body_bb);
1652 edge new_e = make_edge (incr_bb, new_exit_bb, EDGE_FALSE_VALUE);
1654 /* FIXME: Do we need to distribute probabilities for the conditional? */
1655 new_e->probability = profile_probability::guessed_never ();
1656 /* The successor of incr_bb is already pointing to latch_bb; just
1657 change the flags.
1658 make_edge (incr_bb, latch_bb, EDGE_TRUE_VALUE); */
1659 FALLTHRU_EDGE (incr_bb)->flags = EDGE_TRUE_VALUE;
1662 gphi *phi = create_phi_node (iter1, body_bb);
1663 edge preheader_edge = find_edge (entry_bb, body_bb);
1664 edge latch_edge = NULL;
1665 add_phi_arg (phi, build_zero_cst (unsigned_type_node), preheader_edge,
1666 UNKNOWN_LOCATION);
1667 if (incr_bb)
1669 latch_edge = single_succ_edge (latch_bb);
1670 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1672 /* Generate the new return. */
1673 gsi = gsi_last_bb (new_exit_bb);
1674 if (retval
1675 && TREE_CODE (retval) == VIEW_CONVERT_EXPR
1676 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
1677 retval = TREE_OPERAND (retval, 0);
1678 else if (retval)
1680 retval = build1 (VIEW_CONVERT_EXPR,
1681 TREE_TYPE (TREE_TYPE (node->decl)),
1682 retval);
1683 retval = force_gimple_operand_gsi (&gsi, retval, true, NULL,
1684 false, GSI_CONTINUE_LINKING);
1686 g = gimple_build_return (retval);
1687 gsi_insert_after (&gsi, g, GSI_CONTINUE_LINKING);
1690 /* Handle aligned clauses by replacing default defs of the aligned
1691 uniform args with __builtin_assume_aligned (arg_N(D), alignment)
1692 lhs. Handle linear by adding PHIs. */
1693 for (unsigned i = 0; i < node->simdclone->nargs; i++)
1694 if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM
1695 && (TREE_ADDRESSABLE (node->simdclone->args[i].orig_arg)
1696 || !is_gimple_reg_type
1697 (TREE_TYPE (node->simdclone->args[i].orig_arg))))
1699 tree orig_arg = node->simdclone->args[i].orig_arg;
1700 if (is_gimple_reg_type (TREE_TYPE (orig_arg)))
1701 iter1 = make_ssa_name (TREE_TYPE (orig_arg));
1702 else
1704 iter1 = create_tmp_var_raw (TREE_TYPE (orig_arg));
1705 gimple_add_tmp_var (iter1);
1707 gsi = gsi_after_labels (entry_bb);
1708 g = gimple_build_assign (iter1, orig_arg);
1709 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1710 gsi = gsi_after_labels (body_bb);
1711 g = gimple_build_assign (orig_arg, iter1);
1712 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1714 else if (node->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_UNIFORM
1715 && DECL_BY_REFERENCE (node->simdclone->args[i].orig_arg)
1716 && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg))
1717 == REFERENCE_TYPE
1718 && TREE_ADDRESSABLE
1719 (TREE_TYPE (TREE_TYPE (node->simdclone->args[i].orig_arg))))
1721 tree orig_arg = node->simdclone->args[i].orig_arg;
1722 tree def = ssa_default_def (cfun, orig_arg);
1723 if (def && !has_zero_uses (def))
1725 iter1 = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (orig_arg)));
1726 gimple_add_tmp_var (iter1);
1727 gsi = gsi_after_labels (entry_bb);
1728 g = gimple_build_assign (iter1, build_simple_mem_ref (def));
1729 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1730 gsi = gsi_after_labels (body_bb);
1731 g = gimple_build_assign (build_simple_mem_ref (def), iter1);
1732 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1735 else if (node->simdclone->args[i].alignment
1736 && node->simdclone->args[i].arg_type
1737 == SIMD_CLONE_ARG_TYPE_UNIFORM
1738 && (node->simdclone->args[i].alignment
1739 & (node->simdclone->args[i].alignment - 1)) == 0
1740 && TREE_CODE (TREE_TYPE (node->simdclone->args[i].orig_arg))
1741 == POINTER_TYPE)
1743 unsigned int alignment = node->simdclone->args[i].alignment;
1744 tree orig_arg = node->simdclone->args[i].orig_arg;
1745 tree def = ssa_default_def (cfun, orig_arg);
1746 if (def && !has_zero_uses (def))
1748 tree fn = builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED);
1749 gimple_seq seq = NULL;
1750 bool need_cvt = false;
1751 gcall *call
1752 = gimple_build_call (fn, 2, def, size_int (alignment));
1753 g = call;
1754 if (!useless_type_conversion_p (TREE_TYPE (orig_arg),
1755 ptr_type_node))
1756 need_cvt = true;
1757 tree t = make_ssa_name (need_cvt ? ptr_type_node : orig_arg);
1758 gimple_call_set_lhs (g, t);
1759 gimple_seq_add_stmt_without_update (&seq, g);
1760 if (need_cvt)
1762 t = make_ssa_name (orig_arg);
1763 g = gimple_build_assign (t, NOP_EXPR, gimple_call_lhs (g));
1764 gimple_seq_add_stmt_without_update (&seq, g);
1766 gsi_insert_seq_on_edge_immediate
1767 (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)), seq);
1769 entry_bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1770 node->create_edge (cgraph_node::get_create (fn),
1771 call, entry_bb->count);
1773 imm_use_iterator iter;
1774 use_operand_p use_p;
1775 gimple *use_stmt;
1776 tree repl = gimple_get_lhs (g);
1777 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1778 if (is_gimple_debug (use_stmt) || use_stmt == call)
1779 continue;
1780 else
1781 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1782 SET_USE (use_p, repl);
1785 else if ((node->simdclone->args[i].arg_type
1786 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP)
1787 || (node->simdclone->args[i].arg_type
1788 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP)
1789 || (node->simdclone->args[i].arg_type
1790 == SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP)
1791 || (node->simdclone->args[i].arg_type
1792 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP))
1794 tree orig_arg = node->simdclone->args[i].orig_arg;
1795 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1796 || POINTER_TYPE_P (TREE_TYPE (orig_arg)));
1797 tree def = NULL_TREE;
1798 if (TREE_ADDRESSABLE (orig_arg))
1800 def = make_ssa_name (TREE_TYPE (orig_arg));
1801 iter1 = make_ssa_name (TREE_TYPE (orig_arg));
1802 if (incr_bb)
1803 iter2 = make_ssa_name (TREE_TYPE (orig_arg));
1804 gsi = gsi_after_labels (entry_bb);
1805 g = gimple_build_assign (def, orig_arg);
1806 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1808 else
1810 def = ssa_default_def (cfun, orig_arg);
1811 if (!def || has_zero_uses (def))
1812 def = NULL_TREE;
1813 else
1815 iter1 = make_ssa_name (orig_arg);
1816 if (incr_bb)
1817 iter2 = make_ssa_name (orig_arg);
1820 if (def)
1822 phi = create_phi_node (iter1, body_bb);
1823 add_phi_arg (phi, def, preheader_edge, UNKNOWN_LOCATION);
1824 if (incr_bb)
1826 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1827 enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1828 ? PLUS_EXPR : POINTER_PLUS_EXPR;
1829 tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (orig_arg))
1830 ? TREE_TYPE (orig_arg) : sizetype;
1831 tree addcst = simd_clone_linear_addend (node, i, addtype,
1832 entry_bb);
1833 gsi = gsi_last_bb (incr_bb);
1834 g = gimple_build_assign (iter2, code, iter1, addcst);
1835 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1838 imm_use_iterator iter;
1839 use_operand_p use_p;
1840 gimple *use_stmt;
1841 if (TREE_ADDRESSABLE (orig_arg))
1843 gsi = gsi_after_labels (body_bb);
1844 g = gimple_build_assign (orig_arg, iter1);
1845 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
1847 else
1848 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1849 if (use_stmt == phi)
1850 continue;
1851 else
1852 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1853 SET_USE (use_p, iter1);
1856 else if (node->simdclone->args[i].arg_type
1857 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
1858 || (node->simdclone->args[i].arg_type
1859 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP))
1861 tree orig_arg = node->simdclone->args[i].orig_arg;
1862 tree def = ssa_default_def (cfun, orig_arg);
1863 gcc_assert (!TREE_ADDRESSABLE (orig_arg)
1864 && TREE_CODE (TREE_TYPE (orig_arg)) == REFERENCE_TYPE);
1865 if (def && !has_zero_uses (def))
1867 tree rtype = TREE_TYPE (TREE_TYPE (orig_arg));
1868 iter1 = make_ssa_name (orig_arg);
1869 if (incr_bb)
1870 iter2 = make_ssa_name (orig_arg);
1871 tree iter3 = make_ssa_name (rtype);
1872 tree iter4 = make_ssa_name (rtype);
1873 tree iter5 = incr_bb ? make_ssa_name (rtype) : NULL_TREE;
1874 gsi = gsi_after_labels (entry_bb);
1875 gimple *load
1876 = gimple_build_assign (iter3, build_simple_mem_ref (def));
1877 gsi_insert_before (&gsi, load, GSI_NEW_STMT);
1879 tree array = node->simdclone->args[i].simd_array;
1880 TREE_ADDRESSABLE (array) = 1;
1881 tree ptr = build_fold_addr_expr (array);
1882 phi = create_phi_node (iter1, body_bb);
1883 add_phi_arg (phi, ptr, preheader_edge, UNKNOWN_LOCATION);
1884 if (incr_bb)
1886 add_phi_arg (phi, iter2, latch_edge, UNKNOWN_LOCATION);
1887 g = gimple_build_assign (iter2, POINTER_PLUS_EXPR, iter1,
1888 TYPE_SIZE_UNIT (TREE_TYPE (iter3)));
1889 gsi = gsi_last_bb (incr_bb);
1890 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1893 phi = create_phi_node (iter4, body_bb);
1894 add_phi_arg (phi, iter3, preheader_edge, UNKNOWN_LOCATION);
1895 if (incr_bb)
1897 add_phi_arg (phi, iter5, latch_edge, UNKNOWN_LOCATION);
1898 enum tree_code code = INTEGRAL_TYPE_P (TREE_TYPE (iter3))
1899 ? PLUS_EXPR : POINTER_PLUS_EXPR;
1900 tree addtype = INTEGRAL_TYPE_P (TREE_TYPE (iter3))
1901 ? TREE_TYPE (iter3) : sizetype;
1902 tree addcst = simd_clone_linear_addend (node, i, addtype,
1903 entry_bb);
1904 g = gimple_build_assign (iter5, code, iter4, addcst);
1905 gsi = gsi_last_bb (incr_bb);
1906 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1909 g = gimple_build_assign (build_simple_mem_ref (iter1), iter4);
1910 gsi = gsi_after_labels (body_bb);
1911 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1913 imm_use_iterator iter;
1914 use_operand_p use_p;
1915 gimple *use_stmt;
1916 FOR_EACH_IMM_USE_STMT (use_stmt, iter, def)
1917 if (use_stmt == load)
1918 continue;
1919 else
1920 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1921 SET_USE (use_p, iter1);
1923 if (!TYPE_READONLY (rtype) && incr_bb)
1925 tree v = make_ssa_name (rtype);
1926 tree aref = build4 (ARRAY_REF, rtype, array,
1927 size_zero_node, NULL_TREE,
1928 NULL_TREE);
1929 gsi = gsi_after_labels (new_exit_bb);
1930 g = gimple_build_assign (v, aref);
1931 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1932 g = gimple_build_assign (build_simple_mem_ref (def), v);
1933 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1938 calculate_dominance_info (CDI_DOMINATORS);
1939 if (loop)
1940 add_loop (loop, loop->header->loop_father);
1941 update_ssa (TODO_update_ssa);
1943 pop_cfun ();
1946 /* If the function in NODE is tagged as an elemental SIMD function,
1947 create the appropriate SIMD clones. */
1949 void
1950 expand_simd_clones (struct cgraph_node *node)
1952 tree attr;
1953 bool explicit_p = true;
1955 if (node->inlined_to
1956 || lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
1957 return;
1959 attr = lookup_attribute ("omp declare simd",
1960 DECL_ATTRIBUTES (node->decl));
1962 /* See if we can add an "omp declare simd" directive implicitly
1963 before giving up. */
1964 /* FIXME: OpenACC "#pragma acc routine" translates into
1965 "omp declare target", but appears also to have some other effects
1966 that conflict with generating SIMD clones, causing ICEs. So don't
1967 do this if we've got OpenACC instead of OpenMP. */
1968 if (attr == NULL_TREE
1969 #ifdef ACCEL_COMPILER
1970 && (flag_openmp_target_simd_clone == OMP_TARGET_SIMD_CLONE_ANY
1971 || flag_openmp_target_simd_clone == OMP_TARGET_SIMD_CLONE_NOHOST)
1972 #else
1973 && (flag_openmp_target_simd_clone == OMP_TARGET_SIMD_CLONE_ANY
1974 || flag_openmp_target_simd_clone == OMP_TARGET_SIMD_CLONE_HOST)
1975 #endif
1976 && !oacc_get_fn_attrib (node->decl)
1977 && ok_for_auto_simd_clone (node))
1979 attr = tree_cons (get_identifier ("omp declare simd"), NULL,
1980 DECL_ATTRIBUTES (node->decl));
1981 DECL_ATTRIBUTES (node->decl) = attr;
1982 explicit_p = false;
1985 if (attr == NULL_TREE)
1986 return;
1988 /* Ignore
1989 #pragma omp declare simd
1990 extern int foo ();
1991 in C, there we don't know the argument types at all. */
1992 if (!node->definition
1993 && TYPE_ARG_TYPES (TREE_TYPE (node->decl)) == NULL_TREE)
1994 return;
1996 /* Call this before creating clone_info, as it might ggc_collect. */
1997 if (node->definition && node->has_gimple_body_p ())
1998 node->get_body ();
2002 /* Start with parsing the "omp declare simd" attribute(s). */
2003 bool inbranch_clause_specified;
2004 struct cgraph_simd_clone *clone_info
2005 = simd_clone_clauses_extract (node, TREE_VALUE (attr),
2006 &inbranch_clause_specified);
2007 if (clone_info == NULL)
2008 continue;
2010 poly_uint64 orig_simdlen = clone_info->simdlen;
2011 tree base_type = simd_clone_compute_base_data_type (node, clone_info);
2013 /* The target can return 0 (no simd clones should be created),
2014 1 (just one ISA of simd clones should be created) or higher
2015 count of ISA variants. In that case, clone_info is initialized
2016 for the first ISA variant. */
2017 int count
2018 = targetm.simd_clone.compute_vecsize_and_simdlen (node, clone_info,
2019 base_type, 0,
2020 explicit_p);
2021 if (count == 0)
2022 continue;
2024 /* Loop over all COUNT ISA variants, and if !INBRANCH_CLAUSE_SPECIFIED,
2025 also create one inbranch and one !inbranch clone of it. */
2026 for (int i = 0; i < count * 2; i++)
2028 struct cgraph_simd_clone *clone = clone_info;
2029 if (inbranch_clause_specified && (i & 1) != 0)
2030 continue;
2032 if (i != 0)
2034 clone = simd_clone_struct_alloc (clone_info->nargs
2035 + ((i & 1) != 0));
2036 simd_clone_struct_copy (clone, clone_info);
2037 /* Undo changes targetm.simd_clone.compute_vecsize_and_simdlen
2038 and simd_clone_adjust_argument_types did to the first
2039 clone's info. */
2040 clone->nargs -= clone_info->inbranch;
2041 clone->simdlen = orig_simdlen;
2042 /* And call the target hook again to get the right ISA. */
2043 targetm.simd_clone.compute_vecsize_and_simdlen (node, clone,
2044 base_type,
2045 i / 2,
2046 explicit_p);
2047 if ((i & 1) != 0)
2048 clone->inbranch = 1;
2051 /* simd_clone_mangle might fail if such a clone has been created
2052 already. */
2053 tree id = simd_clone_mangle (node, clone);
2054 if (id == NULL_TREE)
2056 if (i == 0)
2057 clone->nargs += clone->inbranch;
2058 continue;
2061 /* Only when we are sure we want to create the clone actually
2062 clone the function (or definitions) or create another
2063 extern FUNCTION_DECL (for prototypes without definitions). */
2064 struct cgraph_node *n = simd_clone_create (node, !explicit_p);
2065 if (n == NULL)
2067 if (i == 0)
2068 clone->nargs += clone->inbranch;
2069 continue;
2072 n->simdclone = clone;
2073 clone->origin = node;
2074 clone->next_clone = NULL;
2075 if (node->simd_clones == NULL)
2077 clone->prev_clone = n;
2078 node->simd_clones = n;
2080 else
2082 clone->prev_clone = node->simd_clones->simdclone->prev_clone;
2083 clone->prev_clone->simdclone->next_clone = n;
2084 node->simd_clones->simdclone->prev_clone = n;
2086 symtab->change_decl_assembler_name (n->decl, id);
2087 /* And finally adjust the return type, parameters and for
2088 definitions also function body. */
2089 if (node->definition)
2090 simd_clone_adjust (n);
2091 else
2093 TREE_TYPE (n->decl)
2094 = build_distinct_type_copy (TREE_TYPE (n->decl));
2095 simd_clone_adjust_return_type (n);
2096 simd_clone_adjust_argument_types (n);
2097 targetm.simd_clone.adjust (n);
2099 if (dump_file)
2100 fprintf (dump_file, "\nGenerated %s clone %s\n",
2101 (TREE_PUBLIC (n->decl) ? "global" : "local"),
2102 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
2105 while ((attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr))));
2108 /* Entry point for IPA simd clone creation pass. */
2110 static unsigned int
2111 ipa_omp_simd_clone (void)
2113 struct cgraph_node *node;
2114 FOR_EACH_FUNCTION (node)
2115 expand_simd_clones (node);
2116 return 0;
2119 namespace {
2121 const pass_data pass_data_omp_simd_clone =
2123 SIMPLE_IPA_PASS, /* type */
2124 "simdclone", /* name */
2125 OPTGROUP_OMP, /* optinfo_flags */
2126 TV_NONE, /* tv_id */
2127 ( PROP_ssa | PROP_cfg ), /* properties_required */
2128 0, /* properties_provided */
2129 0, /* properties_destroyed */
2130 0, /* todo_flags_start */
2131 0, /* todo_flags_finish */
2134 class pass_omp_simd_clone : public simple_ipa_opt_pass
2136 public:
2137 pass_omp_simd_clone(gcc::context *ctxt)
2138 : simple_ipa_opt_pass(pass_data_omp_simd_clone, ctxt)
2141 /* opt_pass methods: */
2142 bool gate (function *) final override;
2143 unsigned int execute (function *) final override
2145 return ipa_omp_simd_clone ();
2149 bool
2150 pass_omp_simd_clone::gate (function *)
2152 return targetm.simd_clone.compute_vecsize_and_simdlen != NULL;
2155 } // anon namespace
2157 simple_ipa_opt_pass *
2158 make_pass_omp_simd_clone (gcc::context *ctxt)
2160 return new pass_omp_simd_clone (ctxt);