1 /* OMP constructs' SIMD clone supporting code.
3 Copyright (C) 2005-2020 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
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
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/>. */
23 #include "coretypes.h"
29 #include "alloc-pool.h"
30 #include "tree-pass.h"
33 #include "pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "gimple-walk.h"
42 #include "langhooks.h"
44 #include "tree-into-ssa.h"
47 #include "symbol-summary.h"
48 #include "ipa-param-manipulation.h"
51 #include "stringpool.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
);
78 /* Make a copy of the `struct cgraph_simd_clone' in FROM to TO. */
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 /* Fill an empty vector ARGS with parameter types of function FNDECL. This
90 uses TYPE_ARG_TYPES if available, otherwise falls back to types of
91 DECL_ARGUMENTS types. */
94 simd_clone_vector_of_formal_parm_types (vec
<tree
> *args
, tree fndecl
)
96 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
98 push_function_arg_types (args
, TREE_TYPE (fndecl
));
101 push_function_arg_decls (args
, fndecl
);
104 FOR_EACH_VEC_ELT (*args
, i
, arg
)
105 (*args
)[i
] = TREE_TYPE ((*args
)[i
]);
108 /* Given a simd function in NODE, extract the simd specific
109 information from the OMP clauses passed in CLAUSES, and return
110 the struct cgraph_simd_clone * if it should be cloned. *INBRANCH_SPECIFIED
111 is set to TRUE if the `inbranch' or `notinbranch' clause specified,
112 otherwise set to FALSE. */
114 static struct cgraph_simd_clone
*
115 simd_clone_clauses_extract (struct cgraph_node
*node
, tree clauses
,
116 bool *inbranch_specified
)
119 simd_clone_vector_of_formal_parm_types (&args
, node
->decl
);
122 *inbranch_specified
= false;
125 if (n
> 0 && args
.last () == void_type_node
)
128 /* Allocate one more than needed just in case this is an in-branch
129 clone which will require a mask argument. */
130 struct cgraph_simd_clone
*clone_info
= simd_clone_struct_alloc (n
+ 1);
131 clone_info
->nargs
= n
;
136 clauses
= TREE_VALUE (clauses
);
137 if (!clauses
|| TREE_CODE (clauses
) != OMP_CLAUSE
)
140 for (t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
142 switch (OMP_CLAUSE_CODE (t
))
144 case OMP_CLAUSE_INBRANCH
:
145 clone_info
->inbranch
= 1;
146 *inbranch_specified
= true;
148 case OMP_CLAUSE_NOTINBRANCH
:
149 clone_info
->inbranch
= 0;
150 *inbranch_specified
= true;
152 case OMP_CLAUSE_SIMDLEN
:
154 = TREE_INT_CST_LOW (OMP_CLAUSE_SIMDLEN_EXPR (t
));
156 case OMP_CLAUSE_LINEAR
:
158 tree decl
= OMP_CLAUSE_DECL (t
);
159 tree step
= OMP_CLAUSE_LINEAR_STEP (t
);
160 int argno
= TREE_INT_CST_LOW (decl
);
161 if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (t
))
163 enum cgraph_simd_clone_arg_type arg_type
;
164 if (TREE_CODE (args
[argno
]) == REFERENCE_TYPE
)
165 switch (OMP_CLAUSE_LINEAR_KIND (t
))
167 case OMP_CLAUSE_LINEAR_REF
:
169 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP
;
171 case OMP_CLAUSE_LINEAR_UVAL
:
173 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP
;
175 case OMP_CLAUSE_LINEAR_VAL
:
176 case OMP_CLAUSE_LINEAR_DEFAULT
:
178 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP
;
184 arg_type
= SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP
;
185 clone_info
->args
[argno
].arg_type
= arg_type
;
186 clone_info
->args
[argno
].linear_step
= tree_to_shwi (step
);
187 gcc_assert (clone_info
->args
[argno
].linear_step
>= 0
188 && clone_info
->args
[argno
].linear_step
< n
);
192 if (POINTER_TYPE_P (args
[argno
]))
193 step
= fold_convert (ssizetype
, step
);
194 if (!tree_fits_shwi_p (step
))
196 warning_at (OMP_CLAUSE_LOCATION (t
), 0,
197 "ignoring large linear step");
200 else if (integer_zerop (step
))
202 warning_at (OMP_CLAUSE_LOCATION (t
), 0,
203 "ignoring zero linear step");
208 enum cgraph_simd_clone_arg_type arg_type
;
209 if (TREE_CODE (args
[argno
]) == REFERENCE_TYPE
)
210 switch (OMP_CLAUSE_LINEAR_KIND (t
))
212 case OMP_CLAUSE_LINEAR_REF
:
214 = SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP
;
216 case OMP_CLAUSE_LINEAR_UVAL
:
218 = SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
;
220 case OMP_CLAUSE_LINEAR_VAL
:
221 case OMP_CLAUSE_LINEAR_DEFAULT
:
223 = SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP
;
229 arg_type
= SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP
;
230 clone_info
->args
[argno
].arg_type
= arg_type
;
231 clone_info
->args
[argno
].linear_step
= tree_to_shwi (step
);
236 case OMP_CLAUSE_UNIFORM
:
238 tree decl
= OMP_CLAUSE_DECL (t
);
239 int argno
= tree_to_uhwi (decl
);
240 clone_info
->args
[argno
].arg_type
241 = SIMD_CLONE_ARG_TYPE_UNIFORM
;
244 case OMP_CLAUSE_ALIGNED
:
246 /* Ignore aligned (x) for declare simd, for the ABI we really
247 need an alignment specified. */
248 if (OMP_CLAUSE_ALIGNED_ALIGNMENT (t
) == NULL_TREE
)
250 tree decl
= OMP_CLAUSE_DECL (t
);
251 int argno
= tree_to_uhwi (decl
);
252 clone_info
->args
[argno
].alignment
253 = TREE_INT_CST_LOW (OMP_CLAUSE_ALIGNED_ALIGNMENT (t
));
262 if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (node
->decl
))))
264 warning_at (DECL_SOURCE_LOCATION (node
->decl
), 0,
265 "ignoring %<#pragma omp declare simd%> on function "
266 "with %<_Atomic%> qualified return type");
270 for (unsigned int argno
= 0; argno
< clone_info
->nargs
; argno
++)
271 if (TYPE_ATOMIC (args
[argno
])
272 && clone_info
->args
[argno
].arg_type
!= SIMD_CLONE_ARG_TYPE_UNIFORM
)
274 warning_at (DECL_SOURCE_LOCATION (node
->decl
), 0,
275 "ignoring %<#pragma omp declare simd%> on function "
276 "with %<_Atomic%> qualified non-%<uniform%> argument");
284 /* Given a SIMD clone in NODE, calculate the characteristic data
285 type and return the coresponding type. The characteristic data
286 type is computed as described in the Intel Vector ABI. */
289 simd_clone_compute_base_data_type (struct cgraph_node
*node
,
290 struct cgraph_simd_clone
*clone_info
)
292 tree type
= integer_type_node
;
293 tree fndecl
= node
->decl
;
295 /* a) For non-void function, the characteristic data type is the
297 if (TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl
))) != VOID_TYPE
)
298 type
= TREE_TYPE (TREE_TYPE (fndecl
));
300 /* b) If the function has any non-uniform, non-linear parameters,
301 then the characteristic data type is the type of the first
306 simd_clone_vector_of_formal_parm_types (&map
, fndecl
);
307 for (unsigned int i
= 0; i
< clone_info
->nargs
; ++i
)
308 if (clone_info
->args
[i
].arg_type
== SIMD_CLONE_ARG_TYPE_VECTOR
)
315 /* c) If the characteristic data type determined by a) or b) above
316 is struct, union, or class type which is pass-by-value (except
317 for the type that maps to the built-in complex data type), the
318 characteristic data type is int. */
319 if (RECORD_OR_UNION_TYPE_P (type
)
320 && !aggregate_value_p (type
, NULL
)
321 && TREE_CODE (type
) != COMPLEX_TYPE
)
322 return integer_type_node
;
324 /* d) If none of the above three classes is applicable, the
325 characteristic data type is int. */
329 /* e) For Intel Xeon Phi native and offload compilation, if the
330 resulting characteristic data type is 8-bit or 16-bit integer
331 data type, the characteristic data type is int. */
332 /* Well, we don't handle Xeon Phi yet. */
336 simd_clone_mangle (struct cgraph_node
*node
,
337 struct cgraph_simd_clone
*clone_info
)
339 char vecsize_mangle
= clone_info
->vecsize_mangle
;
340 char mask
= clone_info
->inbranch
? 'M' : 'N';
341 unsigned int simdlen
= clone_info
->simdlen
;
345 gcc_assert (vecsize_mangle
&& simdlen
);
347 pp_string (&pp
, "_ZGV");
348 pp_character (&pp
, vecsize_mangle
);
349 pp_character (&pp
, mask
);
350 pp_decimal_int (&pp
, simdlen
);
352 for (n
= 0; n
< clone_info
->nargs
; ++n
)
354 struct cgraph_simd_clone_arg arg
= clone_info
->args
[n
];
356 switch (arg
.arg_type
)
358 case SIMD_CLONE_ARG_TYPE_UNIFORM
:
359 pp_character (&pp
, 'u');
361 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP
:
362 pp_character (&pp
, 'l');
364 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP
:
365 pp_character (&pp
, 'R');
367 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP
:
368 pp_character (&pp
, 'L');
370 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
:
371 pp_character (&pp
, 'U');
374 gcc_assert (arg
.linear_step
!= 0);
375 if (arg
.linear_step
> 1)
376 pp_unsigned_wide_integer (&pp
, arg
.linear_step
);
377 else if (arg
.linear_step
< 0)
379 pp_character (&pp
, 'n');
380 pp_unsigned_wide_integer (&pp
, (-(unsigned HOST_WIDE_INT
)
384 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP
:
385 pp_string (&pp
, "ls");
386 pp_unsigned_wide_integer (&pp
, arg
.linear_step
);
388 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP
:
389 pp_string (&pp
, "Rs");
390 pp_unsigned_wide_integer (&pp
, arg
.linear_step
);
392 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP
:
393 pp_string (&pp
, "Ls");
394 pp_unsigned_wide_integer (&pp
, arg
.linear_step
);
396 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP
:
397 pp_string (&pp
, "Us");
398 pp_unsigned_wide_integer (&pp
, arg
.linear_step
);
401 pp_character (&pp
, 'v');
405 pp_character (&pp
, 'a');
406 pp_decimal_int (&pp
, arg
.alignment
);
411 const char *str
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
));
414 pp_string (&pp
, str
);
415 str
= pp_formatted_text (&pp
);
417 /* If there already is a SIMD clone with the same mangled name, don't
418 add another one. This can happen e.g. for
419 #pragma omp declare simd
420 #pragma omp declare simd simdlen(8)
422 if the simdlen is assumed to be 8 for the first one, etc. */
423 for (struct cgraph_node
*clone
= node
->simd_clones
; clone
;
424 clone
= clone
->simdclone
->next_clone
)
425 if (id_equal (DECL_ASSEMBLER_NAME (clone
->decl
), str
))
428 return get_identifier (str
);
431 /* Create a simd clone of OLD_NODE and return it. */
433 static struct cgraph_node
*
434 simd_clone_create (struct cgraph_node
*old_node
)
436 struct cgraph_node
*new_node
;
437 if (old_node
->definition
)
439 if (!old_node
->has_gimple_body_p ())
441 old_node
->get_body ();
442 new_node
= old_node
->create_version_clone_with_body (vNULL
, NULL
, NULL
,
448 tree old_decl
= old_node
->decl
;
449 tree new_decl
= copy_node (old_node
->decl
);
450 DECL_NAME (new_decl
) = clone_function_name_numbered (old_decl
,
452 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
453 SET_DECL_RTL (new_decl
, NULL
);
454 DECL_STATIC_CONSTRUCTOR (new_decl
) = 0;
455 DECL_STATIC_DESTRUCTOR (new_decl
) = 0;
456 new_node
= old_node
->create_version_clone (new_decl
, vNULL
, NULL
);
457 if (old_node
->in_other_partition
)
458 new_node
->in_other_partition
= 1;
460 if (new_node
== NULL
)
463 set_decl_built_in_function (new_node
->decl
, NOT_BUILT_IN
, 0);
464 TREE_PUBLIC (new_node
->decl
) = TREE_PUBLIC (old_node
->decl
);
465 DECL_COMDAT (new_node
->decl
) = DECL_COMDAT (old_node
->decl
);
466 DECL_WEAK (new_node
->decl
) = DECL_WEAK (old_node
->decl
);
467 DECL_EXTERNAL (new_node
->decl
) = DECL_EXTERNAL (old_node
->decl
);
468 DECL_VISIBILITY_SPECIFIED (new_node
->decl
)
469 = DECL_VISIBILITY_SPECIFIED (old_node
->decl
);
470 DECL_VISIBILITY (new_node
->decl
) = DECL_VISIBILITY (old_node
->decl
);
471 DECL_DLLIMPORT_P (new_node
->decl
) = DECL_DLLIMPORT_P (old_node
->decl
);
472 if (DECL_ONE_ONLY (old_node
->decl
))
473 make_decl_one_only (new_node
->decl
, DECL_ASSEMBLER_NAME (new_node
->decl
));
475 /* The method cgraph_version_clone_with_body () will force the new
476 symbol local. Undo this, and inherit external visibility from
478 new_node
->local
= old_node
->local
;
479 new_node
->externally_visible
= old_node
->externally_visible
;
480 new_node
->calls_declare_variant_alt
= old_node
->calls_declare_variant_alt
;
485 /* Adjust the return type of the given function to its appropriate
486 vector counterpart. Returns a simd array to be used throughout the
487 function as a return value. */
490 simd_clone_adjust_return_type (struct cgraph_node
*node
)
492 tree fndecl
= node
->decl
;
493 tree orig_rettype
= TREE_TYPE (TREE_TYPE (fndecl
));
497 /* Adjust the function return type. */
498 if (orig_rettype
== void_type_node
)
500 t
= TREE_TYPE (TREE_TYPE (fndecl
));
501 if (INTEGRAL_TYPE_P (t
) || POINTER_TYPE_P (t
))
502 veclen
= node
->simdclone
->vecsize_int
;
504 veclen
= node
->simdclone
->vecsize_float
;
505 veclen
/= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (t
));
506 if (veclen
> node
->simdclone
->simdlen
)
507 veclen
= node
->simdclone
->simdlen
;
508 if (POINTER_TYPE_P (t
))
509 t
= pointer_sized_int_node
;
510 if (veclen
== node
->simdclone
->simdlen
)
511 t
= build_vector_type (t
, node
->simdclone
->simdlen
);
514 t
= build_vector_type (t
, veclen
);
515 t
= build_array_type_nelts (t
, node
->simdclone
->simdlen
/ veclen
);
517 TREE_TYPE (TREE_TYPE (fndecl
)) = t
;
518 if (!node
->definition
)
521 t
= DECL_RESULT (fndecl
);
522 /* Adjust the DECL_RESULT. */
523 gcc_assert (TREE_TYPE (t
) != void_type_node
);
524 TREE_TYPE (t
) = TREE_TYPE (TREE_TYPE (fndecl
));
527 tree atype
= build_array_type_nelts (orig_rettype
,
528 node
->simdclone
->simdlen
);
529 if (veclen
!= node
->simdclone
->simdlen
)
530 return build1 (VIEW_CONVERT_EXPR
, atype
, t
);
532 /* Set up a SIMD array to use as the return value. */
533 tree retval
= create_tmp_var_raw (atype
, "retval");
534 gimple_add_tmp_var (retval
);
538 /* Each vector argument has a corresponding array to be used locally
539 as part of the eventual loop. Create such temporary array and
542 PREFIX is the prefix to be used for the temporary.
544 TYPE is the inner element type.
546 SIMDLEN is the number of elements. */
549 create_tmp_simd_array (const char *prefix
, tree type
, int simdlen
)
551 tree atype
= build_array_type_nelts (type
, simdlen
);
552 tree avar
= create_tmp_var_raw (atype
, prefix
);
553 gimple_add_tmp_var (avar
);
557 /* Modify the function argument types to their corresponding vector
558 counterparts if appropriate. Also, create one array for each simd
559 argument to be used locally when using the function arguments as
562 NODE is the function whose arguments are to be adjusted.
564 If NODE does not represent function definition, returns NULL. Otherwise
565 returns an adjustment class that will be filled describing how the argument
566 declarations will be remapped. New arguments which are not to be remapped
567 are marked with USER_FLAG. */
569 static ipa_param_body_adjustments
*
570 simd_clone_adjust_argument_types (struct cgraph_node
*node
)
574 if (node
->definition
)
575 push_function_arg_decls (&args
, node
->decl
);
577 simd_clone_vector_of_formal_parm_types (&args
, node
->decl
);
578 struct cgraph_simd_clone
*sc
= node
->simdclone
;
579 vec
<ipa_adjusted_param
, va_gc
> *new_params
= NULL
;
580 vec_safe_reserve (new_params
, sc
->nargs
);
581 unsigned i
, j
, veclen
;
583 for (i
= 0; i
< sc
->nargs
; ++i
)
585 ipa_adjusted_param adj
;
586 memset (&adj
, 0, sizeof (adj
));
588 tree parm_type
= node
->definition
? TREE_TYPE (parm
) : parm
;
590 adj
.prev_clone_index
= i
;
592 sc
->args
[i
].orig_arg
= node
->definition
? parm
: NULL_TREE
;
593 sc
->args
[i
].orig_type
= parm_type
;
595 switch (sc
->args
[i
].arg_type
)
598 /* No adjustment necessary for scalar arguments. */
599 adj
.op
= IPA_PARAM_OP_COPY
;
601 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
:
602 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP
:
603 if (node
->definition
)
604 sc
->args
[i
].simd_array
605 = create_tmp_simd_array (IDENTIFIER_POINTER (DECL_NAME (parm
)),
606 TREE_TYPE (parm_type
),
608 adj
.op
= IPA_PARAM_OP_COPY
;
610 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP
:
611 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP
:
612 case SIMD_CLONE_ARG_TYPE_VECTOR
:
613 if (INTEGRAL_TYPE_P (parm_type
) || POINTER_TYPE_P (parm_type
))
614 veclen
= sc
->vecsize_int
;
616 veclen
= sc
->vecsize_float
;
617 veclen
/= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (parm_type
));
618 if (veclen
> sc
->simdlen
)
619 veclen
= sc
->simdlen
;
620 adj
.op
= IPA_PARAM_OP_NEW
;
621 adj
.param_prefix_index
= IPA_PARAM_PREFIX_SIMD
;
622 if (POINTER_TYPE_P (parm_type
))
623 adj
.type
= build_vector_type (pointer_sized_int_node
, veclen
);
625 adj
.type
= build_vector_type (parm_type
, veclen
);
626 sc
->args
[i
].vector_type
= adj
.type
;
627 for (j
= veclen
; j
< sc
->simdlen
; j
+= veclen
)
629 vec_safe_push (new_params
, adj
);
632 memset (&adj
, 0, sizeof (adj
));
633 adj
.op
= IPA_PARAM_OP_NEW
;
635 adj
.param_prefix_index
= IPA_PARAM_PREFIX_SIMD
;
637 adj
.prev_clone_index
= i
;
638 adj
.type
= sc
->args
[i
].vector_type
;
642 if (node
->definition
)
643 sc
->args
[i
].simd_array
644 = create_tmp_simd_array (DECL_NAME (parm
)
645 ? IDENTIFIER_POINTER (DECL_NAME (parm
))
646 : NULL
, parm_type
, sc
->simdlen
);
648 vec_safe_push (new_params
, adj
);
653 tree base_type
= simd_clone_compute_base_data_type (sc
->origin
, sc
);
654 ipa_adjusted_param adj
;
655 memset (&adj
, 0, sizeof (adj
));
656 adj
.op
= IPA_PARAM_OP_NEW
;
658 adj
.param_prefix_index
= IPA_PARAM_PREFIX_MASK
;
661 adj
.prev_clone_index
= i
;
662 if (INTEGRAL_TYPE_P (base_type
) || POINTER_TYPE_P (base_type
))
663 veclen
= sc
->vecsize_int
;
665 veclen
= sc
->vecsize_float
;
666 veclen
/= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (base_type
));
667 if (veclen
> sc
->simdlen
)
668 veclen
= sc
->simdlen
;
669 if (sc
->mask_mode
!= VOIDmode
)
671 = lang_hooks
.types
.type_for_mode (sc
->mask_mode
, 1);
672 else if (POINTER_TYPE_P (base_type
))
673 adj
.type
= build_vector_type (pointer_sized_int_node
, veclen
);
675 adj
.type
= build_vector_type (base_type
, veclen
);
676 vec_safe_push (new_params
, adj
);
678 for (j
= veclen
; j
< sc
->simdlen
; j
+= veclen
)
679 vec_safe_push (new_params
, adj
);
681 /* We have previously allocated one extra entry for the mask. Use
684 if (sc
->mask_mode
!= VOIDmode
)
685 base_type
= boolean_type_node
;
686 if (node
->definition
)
689 = build_decl (UNKNOWN_LOCATION
, PARM_DECL
, NULL
, base_type
);
690 if (sc
->mask_mode
== VOIDmode
)
691 sc
->args
[i
].simd_array
692 = create_tmp_simd_array ("mask", base_type
, sc
->simdlen
);
693 else if (veclen
< sc
->simdlen
)
694 sc
->args
[i
].simd_array
695 = create_tmp_simd_array ("mask", adj
.type
, sc
->simdlen
/ veclen
);
697 sc
->args
[i
].simd_array
= NULL_TREE
;
699 sc
->args
[i
].orig_type
= base_type
;
700 sc
->args
[i
].arg_type
= SIMD_CLONE_ARG_TYPE_MASK
;
703 if (node
->definition
)
705 ipa_param_body_adjustments
*adjustments
706 = new ipa_param_body_adjustments (new_params
, node
->decl
);
708 adjustments
->modify_formal_parameters ();
713 tree new_arg_types
= NULL_TREE
, new_reversed
;
714 bool last_parm_void
= false;
715 if (args
.length () > 0 && args
.last () == void_type_node
)
716 last_parm_void
= true;
718 gcc_assert (TYPE_ARG_TYPES (TREE_TYPE (node
->decl
)));
719 j
= vec_safe_length (new_params
);
720 for (i
= 0; i
< j
; i
++)
722 struct ipa_adjusted_param
*adj
= &(*new_params
)[i
];
724 if (adj
->op
== IPA_PARAM_OP_COPY
)
725 ptype
= args
[adj
->base_index
];
728 new_arg_types
= tree_cons (NULL_TREE
, ptype
, new_arg_types
);
730 new_reversed
= nreverse (new_arg_types
);
734 TREE_CHAIN (new_arg_types
) = void_list_node
;
736 new_reversed
= void_list_node
;
738 TYPE_ARG_TYPES (TREE_TYPE (node
->decl
)) = new_reversed
;
743 /* Initialize and copy the function arguments in NODE to their
744 corresponding local simd arrays. Returns a fresh gimple_seq with
745 the instruction sequence generated. */
748 simd_clone_init_simd_arrays (struct cgraph_node
*node
,
749 ipa_param_body_adjustments
*adjustments
)
751 gimple_seq seq
= NULL
;
752 unsigned i
= 0, j
= 0, k
;
754 for (tree arg
= DECL_ARGUMENTS (node
->decl
);
756 arg
= DECL_CHAIN (arg
), i
++, j
++)
758 if ((*adjustments
->m_adj_params
)[j
].op
== IPA_PARAM_OP_COPY
759 || POINTER_TYPE_P (TREE_TYPE (arg
)))
762 node
->simdclone
->args
[i
].vector_arg
= arg
;
764 tree array
= node
->simdclone
->args
[i
].simd_array
;
765 if (node
->simdclone
->mask_mode
!= VOIDmode
766 && node
->simdclone
->args
[i
].arg_type
== SIMD_CLONE_ARG_TYPE_MASK
)
768 if (array
== NULL_TREE
)
771 = tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (array
))));
772 for (k
= 0; k
<= l
; k
++)
776 arg
= DECL_CHAIN (arg
);
779 tree t
= build4 (ARRAY_REF
, TREE_TYPE (TREE_TYPE (array
)),
780 array
, size_int (k
), NULL
, NULL
);
781 t
= build2 (MODIFY_EXPR
, TREE_TYPE (t
), t
, arg
);
782 gimplify_and_add (t
, &seq
);
786 if (simd_clone_subparts (TREE_TYPE (arg
)) == node
->simdclone
->simdlen
)
788 tree ptype
= build_pointer_type (TREE_TYPE (TREE_TYPE (array
)));
789 tree ptr
= build_fold_addr_expr (array
);
790 tree t
= build2 (MEM_REF
, TREE_TYPE (arg
), ptr
,
791 build_int_cst (ptype
, 0));
792 t
= build2 (MODIFY_EXPR
, TREE_TYPE (t
), t
, arg
);
793 gimplify_and_add (t
, &seq
);
797 unsigned int simdlen
= simd_clone_subparts (TREE_TYPE (arg
));
798 tree ptype
= build_pointer_type (TREE_TYPE (TREE_TYPE (array
)));
799 for (k
= 0; k
< node
->simdclone
->simdlen
; k
+= simdlen
)
801 tree ptr
= build_fold_addr_expr (array
);
805 arg
= DECL_CHAIN (arg
);
808 tree elemtype
= TREE_TYPE (TREE_TYPE (arg
));
809 elemsize
= GET_MODE_SIZE (SCALAR_TYPE_MODE (elemtype
));
810 tree t
= build2 (MEM_REF
, TREE_TYPE (arg
), ptr
,
811 build_int_cst (ptype
, k
* elemsize
));
812 t
= build2 (MODIFY_EXPR
, TREE_TYPE (t
), t
, arg
);
813 gimplify_and_add (t
, &seq
);
820 /* Callback info for ipa_simd_modify_stmt_ops below. */
822 struct modify_stmt_info
{
823 ipa_param_body_adjustments
*adjustments
;
826 /* True if the parent statement was modified by
827 ipa_simd_modify_stmt_ops. */
831 /* Callback for walk_gimple_op.
833 Adjust operands from a given statement as specified in the
834 adjustments vector in the callback data. */
837 ipa_simd_modify_stmt_ops (tree
*tp
, int *walk_subtrees
, void *data
)
839 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
840 struct modify_stmt_info
*info
= (struct modify_stmt_info
*) wi
->info
;
842 if (TREE_CODE (*tp
) == ADDR_EXPR
)
843 tp
= &TREE_OPERAND (*tp
, 0);
845 if (TREE_CODE (*tp
) == BIT_FIELD_REF
846 || TREE_CODE (*tp
) == IMAGPART_EXPR
847 || TREE_CODE (*tp
) == REALPART_EXPR
)
848 tp
= &TREE_OPERAND (*tp
, 0);
850 tree repl
= NULL_TREE
;
851 ipa_param_body_replacement
*pbr
= NULL
;
853 if (TREE_CODE (*tp
) == PARM_DECL
)
855 pbr
= info
->adjustments
->get_expr_replacement (*tp
, true);
859 else if (TYPE_P (*tp
))
863 repl
= unshare_expr (repl
);
869 bool modified
= info
->modified
;
870 info
->modified
= false;
871 walk_tree (tp
, ipa_simd_modify_stmt_ops
, wi
, wi
->pset
);
874 info
->modified
= modified
;
877 info
->modified
= modified
;
886 if (gimple_code (info
->stmt
) == GIMPLE_PHI
888 && TREE_CODE (*orig_tp
) == ADDR_EXPR
889 && TREE_CODE (TREE_OPERAND (*orig_tp
, 0)) == PARM_DECL
892 gcc_assert (TREE_CODE (pbr
->dummy
) == SSA_NAME
);
893 *orig_tp
= pbr
->dummy
;
894 info
->modified
= true;
898 repl
= build_fold_addr_expr (repl
);
900 if (is_gimple_debug (info
->stmt
))
902 tree vexpr
= make_node (DEBUG_EXPR_DECL
);
903 stmt
= gimple_build_debug_source_bind (vexpr
, repl
, NULL
);
904 DECL_ARTIFICIAL (vexpr
) = 1;
905 TREE_TYPE (vexpr
) = TREE_TYPE (repl
);
906 SET_DECL_MODE (vexpr
, TYPE_MODE (TREE_TYPE (repl
)));
911 stmt
= gimple_build_assign (make_ssa_name (TREE_TYPE (repl
)), repl
);
912 repl
= gimple_assign_lhs (stmt
);
914 gimple_stmt_iterator gsi
;
915 if (gimple_code (info
->stmt
) == GIMPLE_PHI
)
917 if (info
->after_stmt
)
918 gsi
= gsi_for_stmt (info
->after_stmt
);
920 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
921 /* Cache SSA_NAME for next time. */
923 && TREE_CODE (*orig_tp
) == ADDR_EXPR
924 && TREE_CODE (TREE_OPERAND (*orig_tp
, 0)) == PARM_DECL
)
926 gcc_assert (!pbr
->dummy
);
931 gsi
= gsi_for_stmt (info
->stmt
);
932 if (info
->after_stmt
)
933 gsi_insert_after (&gsi
, stmt
, GSI_SAME_STMT
);
935 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
936 if (gimple_code (info
->stmt
) == GIMPLE_PHI
)
937 info
->after_stmt
= stmt
;
940 else if (!useless_type_conversion_p (TREE_TYPE (*tp
), TREE_TYPE (repl
)))
942 tree vce
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (*tp
), repl
);
948 info
->modified
= true;
952 /* Traverse the function body and perform all modifications as
953 described in ADJUSTMENTS. At function return, ADJUSTMENTS will be
954 modified such that the replacement/reduction value will now be an
955 offset into the corresponding simd_array.
957 This function will replace all function argument uses with their
958 corresponding simd array elements, and ajust the return values
962 ipa_simd_modify_function_body (struct cgraph_node
*node
,
963 ipa_param_body_adjustments
*adjustments
,
964 tree retval_array
, tree iter
)
970 /* Register replacements for every function argument use to an offset into
971 the corresponding simd_array. */
972 for (i
= 0, j
= 0; i
< node
->simdclone
->nargs
; ++i
, ++j
)
974 if (!node
->simdclone
->args
[i
].vector_arg
975 || (*adjustments
->m_adj_params
)[j
].user_flag
)
978 tree basetype
= TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
);
979 tree vectype
= TREE_TYPE (node
->simdclone
->args
[i
].vector_arg
);
980 tree r
= build4 (ARRAY_REF
, basetype
, node
->simdclone
->args
[i
].simd_array
,
981 iter
, NULL_TREE
, NULL_TREE
);
982 adjustments
->register_replacement (&(*adjustments
->m_adj_params
)[j
], r
);
984 if (simd_clone_subparts (vectype
) < node
->simdclone
->simdlen
)
985 j
+= node
->simdclone
->simdlen
/ simd_clone_subparts (vectype
) - 1;
989 FOR_EACH_SSA_NAME (i
, name
, cfun
)
992 if (SSA_NAME_VAR (name
)
993 && TREE_CODE (SSA_NAME_VAR (name
)) == PARM_DECL
995 = adjustments
->get_replacement_ssa_base (SSA_NAME_VAR (name
))))
997 if (SSA_NAME_IS_DEFAULT_DEF (name
))
999 tree old_decl
= SSA_NAME_VAR (name
);
1000 bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1001 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
1002 tree repl
= adjustments
->lookup_replacement (old_decl
, 0);
1003 gcc_checking_assert (repl
);
1004 repl
= unshare_expr (repl
);
1005 set_ssa_default_def (cfun
, old_decl
, NULL_TREE
);
1006 SET_SSA_NAME_VAR_OR_IDENTIFIER (name
, base_var
);
1007 SSA_NAME_IS_DEFAULT_DEF (name
) = 0;
1008 gimple
*stmt
= gimple_build_assign (name
, repl
);
1009 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
1012 SET_SSA_NAME_VAR_OR_IDENTIFIER (name
, base_var
);
1016 struct modify_stmt_info info
;
1017 info
.adjustments
= adjustments
;
1019 FOR_EACH_BB_FN (bb
, DECL_STRUCT_FUNCTION (node
->decl
))
1021 gimple_stmt_iterator gsi
;
1023 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1025 gphi
*phi
= as_a
<gphi
*> (gsi_stmt (gsi
));
1026 int i
, n
= gimple_phi_num_args (phi
);
1028 info
.after_stmt
= NULL
;
1029 struct walk_stmt_info wi
;
1030 memset (&wi
, 0, sizeof (wi
));
1031 info
.modified
= false;
1033 for (i
= 0; i
< n
; ++i
)
1035 int walk_subtrees
= 1;
1036 tree arg
= gimple_phi_arg_def (phi
, i
);
1038 ipa_simd_modify_stmt_ops (&op
, &walk_subtrees
, &wi
);
1041 SET_PHI_ARG_DEF (phi
, i
, op
);
1042 gcc_assert (TREE_CODE (op
) == SSA_NAME
);
1043 if (gimple_phi_arg_edge (phi
, i
)->flags
& EDGE_ABNORMAL
)
1044 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
) = 1;
1049 gsi
= gsi_start_bb (bb
);
1050 while (!gsi_end_p (gsi
))
1052 gimple
*stmt
= gsi_stmt (gsi
);
1054 info
.after_stmt
= NULL
;
1055 struct walk_stmt_info wi
;
1057 memset (&wi
, 0, sizeof (wi
));
1058 info
.modified
= false;
1060 walk_gimple_op (stmt
, ipa_simd_modify_stmt_ops
, &wi
);
1062 if (greturn
*return_stmt
= dyn_cast
<greturn
*> (stmt
))
1064 tree retval
= gimple_return_retval (return_stmt
);
1065 edge e
= find_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
));
1066 e
->flags
|= EDGE_FALLTHRU
;
1069 gsi_remove (&gsi
, true);
1073 /* Replace `return foo' with `retval_array[iter] = foo'. */
1074 tree ref
= build4 (ARRAY_REF
, TREE_TYPE (retval
),
1075 retval_array
, iter
, NULL
, NULL
);
1076 stmt
= gimple_build_assign (ref
, retval
);
1077 gsi_replace (&gsi
, stmt
, true);
1078 info
.modified
= true;
1084 /* If the above changed the var of a debug bind into something
1085 different, remove the debug stmt. We could also for all the
1086 replaced parameters add VAR_DECLs for debug info purposes,
1087 add debug stmts for those to be the simd array accesses and
1088 replace debug stmt var operand with that var. Debugging of
1089 vectorized loops doesn't work too well, so don't bother for
1091 if ((gimple_debug_bind_p (stmt
)
1092 && !DECL_P (gimple_debug_bind_get_var (stmt
)))
1093 || (gimple_debug_source_bind_p (stmt
)
1094 && !DECL_P (gimple_debug_source_bind_get_var (stmt
))))
1096 gsi_remove (&gsi
, true);
1099 if (maybe_clean_eh_stmt (stmt
))
1100 gimple_purge_dead_eh_edges (gimple_bb (stmt
));
1107 /* Helper function of simd_clone_adjust, return linear step addend
1111 simd_clone_linear_addend (struct cgraph_node
*node
, unsigned int i
,
1112 tree addtype
, basic_block entry_bb
)
1114 tree ptype
= NULL_TREE
;
1115 switch (node
->simdclone
->args
[i
].arg_type
)
1117 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP
:
1118 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP
:
1119 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP
:
1120 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
:
1121 return build_int_cst (addtype
, node
->simdclone
->args
[i
].linear_step
);
1122 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP
:
1123 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP
:
1124 ptype
= TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
);
1126 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP
:
1127 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP
:
1128 ptype
= TREE_TYPE (TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
));
1134 unsigned int idx
= node
->simdclone
->args
[i
].linear_step
;
1135 tree arg
= node
->simdclone
->args
[idx
].orig_arg
;
1136 gcc_assert (is_gimple_reg_type (TREE_TYPE (arg
)));
1137 gimple_stmt_iterator gsi
= gsi_after_labels (entry_bb
);
1140 if (is_gimple_reg (arg
))
1141 ret
= get_or_create_ssa_default_def (cfun
, arg
);
1144 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (arg
)), arg
);
1145 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1146 ret
= gimple_assign_lhs (g
);
1148 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
1150 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (TREE_TYPE (arg
))),
1151 build_simple_mem_ref (ret
));
1152 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1153 ret
= gimple_assign_lhs (g
);
1155 if (!useless_type_conversion_p (addtype
, TREE_TYPE (ret
)))
1157 g
= gimple_build_assign (make_ssa_name (addtype
), NOP_EXPR
, ret
);
1158 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1159 ret
= gimple_assign_lhs (g
);
1161 if (POINTER_TYPE_P (ptype
))
1163 tree size
= TYPE_SIZE_UNIT (TREE_TYPE (ptype
));
1164 if (size
&& TREE_CODE (size
) == INTEGER_CST
)
1166 g
= gimple_build_assign (make_ssa_name (addtype
), MULT_EXPR
,
1167 ret
, fold_convert (addtype
, size
));
1168 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1169 ret
= gimple_assign_lhs (g
);
1175 /* Adjust the argument types in NODE to their appropriate vector
1179 simd_clone_adjust (struct cgraph_node
*node
)
1181 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1183 TREE_TYPE (node
->decl
) = build_distinct_type_copy (TREE_TYPE (node
->decl
));
1184 targetm
.simd_clone
.adjust (node
);
1186 tree retval
= simd_clone_adjust_return_type (node
);
1187 ipa_param_body_adjustments
*adjustments
1188 = simd_clone_adjust_argument_types (node
);
1189 gcc_assert (adjustments
);
1191 push_gimplify_context ();
1193 gimple_seq seq
= simd_clone_init_simd_arrays (node
, adjustments
);
1195 /* Adjust all uses of vector arguments accordingly. Adjust all
1196 return values accordingly. */
1197 tree iter
= create_tmp_var (unsigned_type_node
, "iter");
1198 tree iter1
= make_ssa_name (iter
);
1199 tree iter2
= NULL_TREE
;
1200 ipa_simd_modify_function_body (node
, adjustments
, retval
, iter1
);
1203 /* Initialize the iteration variable. */
1204 basic_block entry_bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1205 basic_block body_bb
= split_block_after_labels (entry_bb
)->dest
;
1206 gimple_stmt_iterator gsi
= gsi_after_labels (entry_bb
);
1207 /* Insert the SIMD array and iv initialization at function
1209 gsi_insert_seq_before (&gsi
, seq
, GSI_NEW_STMT
);
1211 pop_gimplify_context (NULL
);
1214 basic_block incr_bb
= NULL
;
1215 class loop
*loop
= NULL
;
1217 /* Create a new BB right before the original exit BB, to hold the
1218 iteration increment and the condition/branch. */
1219 if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
))
1221 basic_block orig_exit
= EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun
), 0)->src
;
1222 incr_bb
= create_empty_bb (orig_exit
);
1223 incr_bb
->count
= profile_count::zero ();
1224 add_bb_to_loop (incr_bb
, body_bb
->loop_father
);
1225 while (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
))
1227 edge e
= EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1228 redirect_edge_succ (e
, incr_bb
);
1229 incr_bb
->count
+= e
->count ();
1232 else if (node
->simdclone
->inbranch
)
1234 incr_bb
= create_empty_bb (entry_bb
);
1235 incr_bb
->count
= profile_count::zero ();
1236 add_bb_to_loop (incr_bb
, body_bb
->loop_father
);
1241 make_single_succ_edge (incr_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1242 gsi
= gsi_last_bb (incr_bb
);
1243 iter2
= make_ssa_name (iter
);
1244 g
= gimple_build_assign (iter2
, PLUS_EXPR
, iter1
,
1245 build_int_cst (unsigned_type_node
, 1));
1246 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1248 /* Mostly annotate the loop for the vectorizer (the rest is done
1250 loop
= alloc_loop ();
1251 cfun
->has_force_vectorize_loops
= true;
1252 loop
->safelen
= node
->simdclone
->simdlen
;
1253 loop
->force_vectorize
= true;
1254 loop
->header
= body_bb
;
1257 /* Branch around the body if the mask applies. */
1258 if (node
->simdclone
->inbranch
)
1260 gsi
= gsi_last_bb (loop
->header
);
1262 = node
->simdclone
->args
[node
->simdclone
->nargs
- 1].simd_array
;
1264 if (node
->simdclone
->mask_mode
!= VOIDmode
)
1267 if (mask_array
== NULL_TREE
)
1269 tree arg
= node
->simdclone
->args
[node
->simdclone
->nargs
1271 mask
= get_or_create_ssa_default_def (cfun
, arg
);
1276 tree maskt
= TREE_TYPE (mask_array
);
1277 int c
= tree_to_uhwi (TYPE_MAX_VALUE (TYPE_DOMAIN (maskt
)));
1278 c
= node
->simdclone
->simdlen
/ (c
+ 1);
1279 int s
= exact_log2 (c
);
1282 tree idx
= make_ssa_name (TREE_TYPE (iter1
));
1283 g
= gimple_build_assign (idx
, RSHIFT_EXPR
, iter1
,
1284 build_int_cst (NULL_TREE
, s
));
1285 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1286 mask
= make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array
)));
1287 tree aref
= build4 (ARRAY_REF
,
1288 TREE_TYPE (TREE_TYPE (mask_array
)),
1289 mask_array
, idx
, NULL
, NULL
);
1290 g
= gimple_build_assign (mask
, aref
);
1291 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1292 shift_cnt
= make_ssa_name (TREE_TYPE (iter1
));
1293 g
= gimple_build_assign (shift_cnt
, BIT_AND_EXPR
, iter1
,
1294 build_int_cst (TREE_TYPE (iter1
), c
));
1295 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1297 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (mask
)),
1298 RSHIFT_EXPR
, mask
, shift_cnt
);
1299 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1300 mask
= gimple_assign_lhs (g
);
1301 g
= gimple_build_assign (make_ssa_name (TREE_TYPE (mask
)),
1303 build_int_cst (TREE_TYPE (mask
), 1));
1304 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1305 mask
= gimple_assign_lhs (g
);
1309 mask
= make_ssa_name (TREE_TYPE (TREE_TYPE (mask_array
)));
1310 tree aref
= build4 (ARRAY_REF
,
1311 TREE_TYPE (TREE_TYPE (mask_array
)),
1312 mask_array
, iter1
, NULL
, NULL
);
1313 g
= gimple_build_assign (mask
, aref
);
1314 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1315 int bitsize
= GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (aref
)));
1316 if (!INTEGRAL_TYPE_P (TREE_TYPE (aref
)))
1318 aref
= build1 (VIEW_CONVERT_EXPR
,
1319 build_nonstandard_integer_type (bitsize
, 0),
1321 mask
= make_ssa_name (TREE_TYPE (aref
));
1322 g
= gimple_build_assign (mask
, aref
);
1323 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1327 g
= gimple_build_cond (EQ_EXPR
, mask
, build_zero_cst (TREE_TYPE (mask
)),
1329 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1330 edge e
= make_edge (loop
->header
, incr_bb
, EDGE_TRUE_VALUE
);
1331 e
->probability
= profile_probability::unlikely ().guessed ();
1332 incr_bb
->count
+= e
->count ();
1333 edge fallthru
= FALLTHRU_EDGE (loop
->header
);
1334 fallthru
->flags
= EDGE_FALSE_VALUE
;
1335 fallthru
->probability
= profile_probability::likely ().guessed ();
1338 basic_block latch_bb
= NULL
;
1339 basic_block new_exit_bb
= NULL
;
1341 /* Generate the condition. */
1344 gsi
= gsi_last_bb (incr_bb
);
1345 g
= gimple_build_cond (LT_EXPR
, iter2
,
1346 build_int_cst (unsigned_type_node
,
1347 node
->simdclone
->simdlen
),
1349 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1350 edge e
= split_block (incr_bb
, gsi_stmt (gsi
));
1352 new_exit_bb
= split_block_after_labels (latch_bb
)->dest
;
1353 loop
->latch
= latch_bb
;
1355 redirect_edge_succ (FALLTHRU_EDGE (latch_bb
), body_bb
);
1357 edge new_e
= make_edge (incr_bb
, new_exit_bb
, EDGE_FALSE_VALUE
);
1359 /* FIXME: Do we need to distribute probabilities for the conditional? */
1360 new_e
->probability
= profile_probability::guessed_never ();
1361 /* The successor of incr_bb is already pointing to latch_bb; just
1363 make_edge (incr_bb, latch_bb, EDGE_TRUE_VALUE); */
1364 FALLTHRU_EDGE (incr_bb
)->flags
= EDGE_TRUE_VALUE
;
1367 gphi
*phi
= create_phi_node (iter1
, body_bb
);
1368 edge preheader_edge
= find_edge (entry_bb
, body_bb
);
1369 edge latch_edge
= NULL
;
1370 add_phi_arg (phi
, build_zero_cst (unsigned_type_node
), preheader_edge
,
1374 latch_edge
= single_succ_edge (latch_bb
);
1375 add_phi_arg (phi
, iter2
, latch_edge
, UNKNOWN_LOCATION
);
1377 /* Generate the new return. */
1378 gsi
= gsi_last_bb (new_exit_bb
);
1380 && TREE_CODE (retval
) == VIEW_CONVERT_EXPR
1381 && TREE_CODE (TREE_OPERAND (retval
, 0)) == RESULT_DECL
)
1382 retval
= TREE_OPERAND (retval
, 0);
1385 retval
= build1 (VIEW_CONVERT_EXPR
,
1386 TREE_TYPE (TREE_TYPE (node
->decl
)),
1388 retval
= force_gimple_operand_gsi (&gsi
, retval
, true, NULL
,
1389 false, GSI_CONTINUE_LINKING
);
1391 g
= gimple_build_return (retval
);
1392 gsi_insert_after (&gsi
, g
, GSI_CONTINUE_LINKING
);
1395 /* Handle aligned clauses by replacing default defs of the aligned
1396 uniform args with __builtin_assume_aligned (arg_N(D), alignment)
1397 lhs. Handle linear by adding PHIs. */
1398 for (unsigned i
= 0; i
< node
->simdclone
->nargs
; i
++)
1399 if (node
->simdclone
->args
[i
].arg_type
== SIMD_CLONE_ARG_TYPE_UNIFORM
1400 && (TREE_ADDRESSABLE (node
->simdclone
->args
[i
].orig_arg
)
1401 || !is_gimple_reg_type
1402 (TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
))))
1404 tree orig_arg
= node
->simdclone
->args
[i
].orig_arg
;
1405 if (is_gimple_reg_type (TREE_TYPE (orig_arg
)))
1406 iter1
= make_ssa_name (TREE_TYPE (orig_arg
));
1409 iter1
= create_tmp_var_raw (TREE_TYPE (orig_arg
));
1410 gimple_add_tmp_var (iter1
);
1412 gsi
= gsi_after_labels (entry_bb
);
1413 g
= gimple_build_assign (iter1
, orig_arg
);
1414 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1415 gsi
= gsi_after_labels (body_bb
);
1416 g
= gimple_build_assign (orig_arg
, iter1
);
1417 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1419 else if (node
->simdclone
->args
[i
].arg_type
== SIMD_CLONE_ARG_TYPE_UNIFORM
1420 && DECL_BY_REFERENCE (node
->simdclone
->args
[i
].orig_arg
)
1421 && TREE_CODE (TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
))
1424 (TREE_TYPE (TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
))))
1426 tree orig_arg
= node
->simdclone
->args
[i
].orig_arg
;
1427 tree def
= ssa_default_def (cfun
, orig_arg
);
1428 if (def
&& !has_zero_uses (def
))
1430 iter1
= create_tmp_var_raw (TREE_TYPE (TREE_TYPE (orig_arg
)));
1431 gimple_add_tmp_var (iter1
);
1432 gsi
= gsi_after_labels (entry_bb
);
1433 g
= gimple_build_assign (iter1
, build_simple_mem_ref (def
));
1434 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1435 gsi
= gsi_after_labels (body_bb
);
1436 g
= gimple_build_assign (build_simple_mem_ref (def
), iter1
);
1437 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1440 else if (node
->simdclone
->args
[i
].alignment
1441 && node
->simdclone
->args
[i
].arg_type
1442 == SIMD_CLONE_ARG_TYPE_UNIFORM
1443 && (node
->simdclone
->args
[i
].alignment
1444 & (node
->simdclone
->args
[i
].alignment
- 1)) == 0
1445 && TREE_CODE (TREE_TYPE (node
->simdclone
->args
[i
].orig_arg
))
1448 unsigned int alignment
= node
->simdclone
->args
[i
].alignment
;
1449 tree orig_arg
= node
->simdclone
->args
[i
].orig_arg
;
1450 tree def
= ssa_default_def (cfun
, orig_arg
);
1451 if (def
&& !has_zero_uses (def
))
1453 tree fn
= builtin_decl_explicit (BUILT_IN_ASSUME_ALIGNED
);
1454 gimple_seq seq
= NULL
;
1455 bool need_cvt
= false;
1457 = gimple_build_call (fn
, 2, def
, size_int (alignment
));
1459 if (!useless_type_conversion_p (TREE_TYPE (orig_arg
),
1462 tree t
= make_ssa_name (need_cvt
? ptr_type_node
: orig_arg
);
1463 gimple_call_set_lhs (g
, t
);
1464 gimple_seq_add_stmt_without_update (&seq
, g
);
1467 t
= make_ssa_name (orig_arg
);
1468 g
= gimple_build_assign (t
, NOP_EXPR
, gimple_call_lhs (g
));
1469 gimple_seq_add_stmt_without_update (&seq
, g
);
1471 gsi_insert_seq_on_edge_immediate
1472 (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), seq
);
1474 entry_bb
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1475 node
->create_edge (cgraph_node::get_create (fn
),
1476 call
, entry_bb
->count
);
1478 imm_use_iterator iter
;
1479 use_operand_p use_p
;
1481 tree repl
= gimple_get_lhs (g
);
1482 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, def
)
1483 if (is_gimple_debug (use_stmt
) || use_stmt
== call
)
1486 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1487 SET_USE (use_p
, repl
);
1490 else if ((node
->simdclone
->args
[i
].arg_type
1491 == SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP
)
1492 || (node
->simdclone
->args
[i
].arg_type
1493 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP
)
1494 || (node
->simdclone
->args
[i
].arg_type
1495 == SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP
)
1496 || (node
->simdclone
->args
[i
].arg_type
1497 == SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP
))
1499 tree orig_arg
= node
->simdclone
->args
[i
].orig_arg
;
1500 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (orig_arg
))
1501 || POINTER_TYPE_P (TREE_TYPE (orig_arg
)));
1502 tree def
= NULL_TREE
;
1503 if (TREE_ADDRESSABLE (orig_arg
))
1505 def
= make_ssa_name (TREE_TYPE (orig_arg
));
1506 iter1
= make_ssa_name (TREE_TYPE (orig_arg
));
1508 iter2
= make_ssa_name (TREE_TYPE (orig_arg
));
1509 gsi
= gsi_after_labels (entry_bb
);
1510 g
= gimple_build_assign (def
, orig_arg
);
1511 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1515 def
= ssa_default_def (cfun
, orig_arg
);
1516 if (!def
|| has_zero_uses (def
))
1520 iter1
= make_ssa_name (orig_arg
);
1522 iter2
= make_ssa_name (orig_arg
);
1527 phi
= create_phi_node (iter1
, body_bb
);
1528 add_phi_arg (phi
, def
, preheader_edge
, UNKNOWN_LOCATION
);
1531 add_phi_arg (phi
, iter2
, latch_edge
, UNKNOWN_LOCATION
);
1532 enum tree_code code
= INTEGRAL_TYPE_P (TREE_TYPE (orig_arg
))
1533 ? PLUS_EXPR
: POINTER_PLUS_EXPR
;
1534 tree addtype
= INTEGRAL_TYPE_P (TREE_TYPE (orig_arg
))
1535 ? TREE_TYPE (orig_arg
) : sizetype
;
1536 tree addcst
= simd_clone_linear_addend (node
, i
, addtype
,
1538 gsi
= gsi_last_bb (incr_bb
);
1539 g
= gimple_build_assign (iter2
, code
, iter1
, addcst
);
1540 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1543 imm_use_iterator iter
;
1544 use_operand_p use_p
;
1546 if (TREE_ADDRESSABLE (orig_arg
))
1548 gsi
= gsi_after_labels (body_bb
);
1549 g
= gimple_build_assign (orig_arg
, iter1
);
1550 gsi_insert_before (&gsi
, g
, GSI_NEW_STMT
);
1553 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, def
)
1554 if (use_stmt
== phi
)
1557 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1558 SET_USE (use_p
, iter1
);
1561 else if (node
->simdclone
->args
[i
].arg_type
1562 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP
1563 || (node
->simdclone
->args
[i
].arg_type
1564 == SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP
))
1566 tree orig_arg
= node
->simdclone
->args
[i
].orig_arg
;
1567 tree def
= ssa_default_def (cfun
, orig_arg
);
1568 gcc_assert (!TREE_ADDRESSABLE (orig_arg
)
1569 && TREE_CODE (TREE_TYPE (orig_arg
)) == REFERENCE_TYPE
);
1570 if (def
&& !has_zero_uses (def
))
1572 tree rtype
= TREE_TYPE (TREE_TYPE (orig_arg
));
1573 iter1
= make_ssa_name (orig_arg
);
1575 iter2
= make_ssa_name (orig_arg
);
1576 tree iter3
= make_ssa_name (rtype
);
1577 tree iter4
= make_ssa_name (rtype
);
1578 tree iter5
= incr_bb
? make_ssa_name (rtype
) : NULL_TREE
;
1579 gsi
= gsi_after_labels (entry_bb
);
1581 = gimple_build_assign (iter3
, build_simple_mem_ref (def
));
1582 gsi_insert_before (&gsi
, load
, GSI_NEW_STMT
);
1584 tree array
= node
->simdclone
->args
[i
].simd_array
;
1585 TREE_ADDRESSABLE (array
) = 1;
1586 tree ptr
= build_fold_addr_expr (array
);
1587 phi
= create_phi_node (iter1
, body_bb
);
1588 add_phi_arg (phi
, ptr
, preheader_edge
, UNKNOWN_LOCATION
);
1591 add_phi_arg (phi
, iter2
, latch_edge
, UNKNOWN_LOCATION
);
1592 g
= gimple_build_assign (iter2
, POINTER_PLUS_EXPR
, iter1
,
1593 TYPE_SIZE_UNIT (TREE_TYPE (iter3
)));
1594 gsi
= gsi_last_bb (incr_bb
);
1595 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1598 phi
= create_phi_node (iter4
, body_bb
);
1599 add_phi_arg (phi
, iter3
, preheader_edge
, UNKNOWN_LOCATION
);
1602 add_phi_arg (phi
, iter5
, latch_edge
, UNKNOWN_LOCATION
);
1603 enum tree_code code
= INTEGRAL_TYPE_P (TREE_TYPE (iter3
))
1604 ? PLUS_EXPR
: POINTER_PLUS_EXPR
;
1605 tree addtype
= INTEGRAL_TYPE_P (TREE_TYPE (iter3
))
1606 ? TREE_TYPE (iter3
) : sizetype
;
1607 tree addcst
= simd_clone_linear_addend (node
, i
, addtype
,
1609 g
= gimple_build_assign (iter5
, code
, iter4
, addcst
);
1610 gsi
= gsi_last_bb (incr_bb
);
1611 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1614 g
= gimple_build_assign (build_simple_mem_ref (iter1
), iter4
);
1615 gsi
= gsi_after_labels (body_bb
);
1616 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1618 imm_use_iterator iter
;
1619 use_operand_p use_p
;
1621 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, def
)
1622 if (use_stmt
== load
)
1625 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1626 SET_USE (use_p
, iter1
);
1628 if (!TYPE_READONLY (rtype
) && incr_bb
)
1630 tree v
= make_ssa_name (rtype
);
1631 tree aref
= build4 (ARRAY_REF
, rtype
, array
,
1632 size_zero_node
, NULL_TREE
,
1634 gsi
= gsi_after_labels (new_exit_bb
);
1635 g
= gimple_build_assign (v
, aref
);
1636 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1637 g
= gimple_build_assign (build_simple_mem_ref (def
), v
);
1638 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
1643 calculate_dominance_info (CDI_DOMINATORS
);
1645 add_loop (loop
, loop
->header
->loop_father
);
1646 update_ssa (TODO_update_ssa
);
1651 /* If the function in NODE is tagged as an elemental SIMD function,
1652 create the appropriate SIMD clones. */
1655 expand_simd_clones (struct cgraph_node
*node
)
1657 tree attr
= lookup_attribute ("omp declare simd",
1658 DECL_ATTRIBUTES (node
->decl
));
1659 if (attr
== NULL_TREE
1661 || lookup_attribute ("noclone", DECL_ATTRIBUTES (node
->decl
)))
1665 #pragma omp declare simd
1667 in C, there we don't know the argument types at all. */
1668 if (!node
->definition
1669 && TYPE_ARG_TYPES (TREE_TYPE (node
->decl
)) == NULL_TREE
)
1672 /* Call this before creating clone_info, as it might ggc_collect. */
1673 if (node
->definition
&& node
->has_gimple_body_p ())
1678 /* Start with parsing the "omp declare simd" attribute(s). */
1679 bool inbranch_clause_specified
;
1680 struct cgraph_simd_clone
*clone_info
1681 = simd_clone_clauses_extract (node
, TREE_VALUE (attr
),
1682 &inbranch_clause_specified
);
1683 if (clone_info
== NULL
)
1686 int orig_simdlen
= clone_info
->simdlen
;
1687 tree base_type
= simd_clone_compute_base_data_type (node
, clone_info
);
1688 /* The target can return 0 (no simd clones should be created),
1689 1 (just one ISA of simd clones should be created) or higher
1690 count of ISA variants. In that case, clone_info is initialized
1691 for the first ISA variant. */
1693 = targetm
.simd_clone
.compute_vecsize_and_simdlen (node
, clone_info
,
1698 /* Loop over all COUNT ISA variants, and if !INBRANCH_CLAUSE_SPECIFIED,
1699 also create one inbranch and one !inbranch clone of it. */
1700 for (int i
= 0; i
< count
* 2; i
++)
1702 struct cgraph_simd_clone
*clone
= clone_info
;
1703 if (inbranch_clause_specified
&& (i
& 1) != 0)
1708 clone
= simd_clone_struct_alloc (clone_info
->nargs
1710 simd_clone_struct_copy (clone
, clone_info
);
1711 /* Undo changes targetm.simd_clone.compute_vecsize_and_simdlen
1712 and simd_clone_adjust_argument_types did to the first
1714 clone
->nargs
-= clone_info
->inbranch
;
1715 clone
->simdlen
= orig_simdlen
;
1716 /* And call the target hook again to get the right ISA. */
1717 targetm
.simd_clone
.compute_vecsize_and_simdlen (node
, clone
,
1721 clone
->inbranch
= 1;
1724 /* simd_clone_mangle might fail if such a clone has been created
1726 tree id
= simd_clone_mangle (node
, clone
);
1727 if (id
== NULL_TREE
)
1730 clone
->nargs
+= clone
->inbranch
;
1734 /* Only when we are sure we want to create the clone actually
1735 clone the function (or definitions) or create another
1736 extern FUNCTION_DECL (for prototypes without definitions). */
1737 struct cgraph_node
*n
= simd_clone_create (node
);
1741 clone
->nargs
+= clone
->inbranch
;
1745 n
->simdclone
= clone
;
1746 clone
->origin
= node
;
1747 clone
->next_clone
= NULL
;
1748 if (node
->simd_clones
== NULL
)
1750 clone
->prev_clone
= n
;
1751 node
->simd_clones
= n
;
1755 clone
->prev_clone
= node
->simd_clones
->simdclone
->prev_clone
;
1756 clone
->prev_clone
->simdclone
->next_clone
= n
;
1757 node
->simd_clones
->simdclone
->prev_clone
= n
;
1759 symtab
->change_decl_assembler_name (n
->decl
, id
);
1760 /* And finally adjust the return type, parameters and for
1761 definitions also function body. */
1762 if (node
->definition
)
1763 simd_clone_adjust (n
);
1767 = build_distinct_type_copy (TREE_TYPE (n
->decl
));
1768 targetm
.simd_clone
.adjust (n
);
1769 simd_clone_adjust_return_type (n
);
1770 simd_clone_adjust_argument_types (n
);
1774 while ((attr
= lookup_attribute ("omp declare simd", TREE_CHAIN (attr
))));
1777 /* Entry point for IPA simd clone creation pass. */
1780 ipa_omp_simd_clone (void)
1782 struct cgraph_node
*node
;
1783 FOR_EACH_FUNCTION (node
)
1784 expand_simd_clones (node
);
1790 const pass_data pass_data_omp_simd_clone
=
1792 SIMPLE_IPA_PASS
, /* type */
1793 "simdclone", /* name */
1794 OPTGROUP_OMP
, /* optinfo_flags */
1795 TV_NONE
, /* tv_id */
1796 ( PROP_ssa
| PROP_cfg
), /* properties_required */
1797 0, /* properties_provided */
1798 0, /* properties_destroyed */
1799 0, /* todo_flags_start */
1800 0, /* todo_flags_finish */
1803 class pass_omp_simd_clone
: public simple_ipa_opt_pass
1806 pass_omp_simd_clone(gcc::context
*ctxt
)
1807 : simple_ipa_opt_pass(pass_data_omp_simd_clone
, ctxt
)
1810 /* opt_pass methods: */
1811 virtual bool gate (function
*);
1812 virtual unsigned int execute (function
*) { return ipa_omp_simd_clone (); }
1816 pass_omp_simd_clone::gate (function
*)
1818 return targetm
.simd_clone
.compute_vecsize_and_simdlen
!= NULL
;
1823 simple_ipa_opt_pass
*
1824 make_pass_omp_simd_clone (gcc::context
*ctxt
)
1826 return new pass_omp_simd_clone (ctxt
);