Line data Source code
1 : /* Analysis Utilities for Loop Vectorization.
2 : Copyright (C) 2006-2026 Free Software Foundation, Inc.
3 : Contributed by Dorit Nuzman <dorit@il.ibm.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "gimple-iterator.h"
29 : #include "gimple-fold.h"
30 : #include "ssa.h"
31 : #include "expmed.h"
32 : #include "optabs-tree.h"
33 : #include "insn-config.h"
34 : #include "recog.h" /* FIXME: for insn_data */
35 : #include "fold-const.h"
36 : #include "stor-layout.h"
37 : #include "tree-eh.h"
38 : #include "gimplify.h"
39 : #include "gimple-iterator.h"
40 : #include "gimple-fold.h"
41 : #include "gimplify-me.h"
42 : #include "cfgloop.h"
43 : #include "tree-vectorizer.h"
44 : #include "dumpfile.h"
45 : #include "builtins.h"
46 : #include "internal-fn.h"
47 : #include "case-cfn-macros.h"
48 : #include "fold-const-call.h"
49 : #include "attribs.h"
50 : #include "cgraph.h"
51 : #include "omp-simd-clone.h"
52 : #include "predict.h"
53 : #include "tree-vector-builder.h"
54 : #include "tree-ssa-loop-ivopts.h"
55 : #include "vec-perm-indices.h"
56 : #include "gimple-range.h"
57 : #include "alias.h"
58 :
59 :
60 : /* TODO: Note the vectorizer still builds COND_EXPRs with GENERIC compares
61 : in the first operand. Disentangling this is future work, the
62 : IL is properly transferred to VEC_COND_EXPRs with separate compares. */
63 :
64 :
65 : /* Return true if we have a useful VR_RANGE range for VAR, storing it
66 : in *MIN_VALUE and *MAX_VALUE if so. Note the range in the dump files. */
67 :
68 : bool
69 12703750 : vect_get_range_info (tree var, wide_int *min_value, wide_int *max_value)
70 : {
71 12703750 : int_range_max vr;
72 12703750 : tree vr_min, vr_max;
73 25407500 : get_range_query (cfun)->range_of_expr (vr, var);
74 12703750 : if (vr.undefined_p ())
75 71 : vr.set_varying (TREE_TYPE (var));
76 12703750 : value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
77 12703750 : *min_value = wi::to_wide (vr_min);
78 12703750 : *max_value = wi::to_wide (vr_max);
79 12703750 : wide_int nonzero = get_nonzero_bits (var);
80 12703750 : signop sgn = TYPE_SIGN (TREE_TYPE (var));
81 12703750 : if (intersect_range_with_nonzero_bits (vr_type, min_value, max_value,
82 : nonzero, sgn) == VR_RANGE)
83 : {
84 6268398 : if (dump_enabled_p ())
85 : {
86 89039 : dump_generic_expr_loc (MSG_NOTE, vect_location, TDF_SLIM, var);
87 89039 : dump_printf (MSG_NOTE, " has range [");
88 89039 : dump_hex (MSG_NOTE, *min_value);
89 89039 : dump_printf (MSG_NOTE, ", ");
90 89039 : dump_hex (MSG_NOTE, *max_value);
91 89039 : dump_printf (MSG_NOTE, "]\n");
92 : }
93 6268398 : return true;
94 : }
95 : else
96 : {
97 6435352 : if (dump_enabled_p ())
98 : {
99 67805 : dump_generic_expr_loc (MSG_NOTE, vect_location, TDF_SLIM, var);
100 67805 : dump_printf (MSG_NOTE, " has no range info\n");
101 : }
102 6435352 : return false;
103 : }
104 12703750 : }
105 :
106 : /* Report that we've found an instance of pattern PATTERN in
107 : statement STMT. */
108 :
109 : static void
110 1313077 : vect_pattern_detected (const char *name, gimple *stmt)
111 : {
112 1313077 : if (dump_enabled_p ())
113 25420 : dump_printf_loc (MSG_NOTE, vect_location, "%s: detected: %G", name, stmt);
114 1313077 : }
115 :
116 : /* Associate pattern statement PATTERN_STMT with ORIG_STMT_INFO and
117 : return the pattern statement's stmt_vec_info. Set its vector type to
118 : VECTYPE if it doesn't have one already. */
119 :
120 : static stmt_vec_info
121 2529495 : vect_init_pattern_stmt (vec_info *vinfo, gimple *pattern_stmt,
122 : stmt_vec_info orig_stmt_info, tree vectype)
123 : {
124 2529495 : stmt_vec_info pattern_stmt_info = vinfo->lookup_stmt (pattern_stmt);
125 2529495 : if (pattern_stmt_info == NULL)
126 1353412 : pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
127 2529495 : gimple_set_bb (pattern_stmt, gimple_bb (orig_stmt_info->stmt));
128 :
129 2529495 : pattern_stmt_info->pattern_stmt_p = true;
130 2529495 : STMT_VINFO_RELATED_STMT (pattern_stmt_info) = orig_stmt_info;
131 2529495 : STMT_VINFO_DEF_TYPE (pattern_stmt_info)
132 2529495 : = STMT_VINFO_DEF_TYPE (orig_stmt_info);
133 2529495 : if (!STMT_VINFO_VECTYPE (pattern_stmt_info))
134 : {
135 2270498 : gcc_assert (!vectype
136 : || is_a <gcond *> (pattern_stmt)
137 : || (VECTOR_BOOLEAN_TYPE_P (vectype)
138 : == vect_use_mask_type_p (orig_stmt_info)));
139 1364065 : STMT_VINFO_VECTYPE (pattern_stmt_info) = vectype;
140 1364065 : pattern_stmt_info->mask_precision = orig_stmt_info->mask_precision;
141 : }
142 2529495 : return pattern_stmt_info;
143 : }
144 :
145 : /* Set the pattern statement of ORIG_STMT_INFO to PATTERN_STMT.
146 : Also set the vector type of PATTERN_STMT to VECTYPE, if it doesn't
147 : have one already. */
148 :
149 : static void
150 1082008 : vect_set_pattern_stmt (vec_info *vinfo, gimple *pattern_stmt,
151 : stmt_vec_info orig_stmt_info, tree vectype)
152 : {
153 1082008 : STMT_VINFO_IN_PATTERN_P (orig_stmt_info) = true;
154 1082008 : STMT_VINFO_RELATED_STMT (orig_stmt_info)
155 0 : = vect_init_pattern_stmt (vinfo, pattern_stmt, orig_stmt_info, vectype);
156 1051701 : }
157 :
158 : /* Add NEW_STMT to STMT_INFO's pattern definition statements. If VECTYPE
159 : is nonnull, record that NEW_STMT's vector type is VECTYPE, which might
160 : be different from the vector type of the final pattern statement.
161 : If VECTYPE is a mask type, SCALAR_TYPE_FOR_MASK is the scalar type
162 : from which it was derived. */
163 :
164 : static inline void
165 1406847 : append_pattern_def_seq (vec_info *vinfo,
166 : stmt_vec_info stmt_info, gimple *new_stmt,
167 : tree vectype = NULL_TREE,
168 : tree scalar_type_for_mask = NULL_TREE)
169 : {
170 2136065 : gcc_assert (!scalar_type_for_mask
171 : == (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype)));
172 1406847 : if (vectype)
173 : {
174 1165669 : stmt_vec_info new_stmt_info = vinfo->add_stmt (new_stmt);
175 1165669 : STMT_VINFO_VECTYPE (new_stmt_info) = vectype;
176 1165669 : if (scalar_type_for_mask)
177 677629 : new_stmt_info->mask_precision
178 1355258 : = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (scalar_type_for_mask));
179 : }
180 1406847 : gimple_seq_add_stmt_without_update (&STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
181 : new_stmt);
182 1406847 : }
183 :
184 :
185 : /* Add NEW_STMT to VINFO's invariant pattern definition statements. These
186 : statements are not vectorized but are materialized as scalar in the loop
187 : preheader. */
188 :
189 : static inline void
190 1387 : append_inv_pattern_def_seq (vec_info *vinfo, gimple *new_stmt)
191 : {
192 1387 : gimple_seq_add_stmt_without_update (&vinfo->inv_pattern_def_seq, new_stmt);
193 : }
194 :
195 : /* The caller wants to perform new operations on vect_external variable
196 : VAR, so that the result of the operations would also be vect_external.
197 : Return the edge on which the operations can be performed, if one exists.
198 : Return null if the operations should instead be treated as part of
199 : the pattern that needs them. */
200 :
201 : static edge
202 8633 : vect_get_external_def_edge (vec_info *vinfo, tree var)
203 : {
204 8633 : edge e = NULL;
205 8633 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
206 : {
207 819 : e = loop_preheader_edge (loop_vinfo->loop);
208 819 : if (!SSA_NAME_IS_DEFAULT_DEF (var))
209 : {
210 623 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (var));
211 623 : if (bb == NULL
212 623 : || !dominated_by_p (CDI_DOMINATORS, e->dest, bb))
213 : e = NULL;
214 : }
215 : }
216 8633 : return e;
217 : }
218 :
219 : /* Return true if the target supports a vector version of CODE,
220 : where CODE is known to map to a direct optab with the given SUBTYPE.
221 : ITYPE specifies the type of (some of) the scalar inputs and OTYPE
222 : specifies the type of the scalar result.
223 :
224 : If CODE allows the inputs and outputs to have different type
225 : (such as for WIDEN_SUM_EXPR), it is the input mode rather
226 : than the output mode that determines the appropriate target pattern.
227 : Operand 0 of the target pattern then specifies the mode that the output
228 : must have.
229 :
230 : When returning true, set *VECOTYPE_OUT to the vector version of OTYPE.
231 : Also set *VECITYPE_OUT to the vector version of ITYPE if VECITYPE_OUT
232 : is nonnull. */
233 :
234 : static bool
235 817 : vect_supportable_direct_optab_p (vec_info *vinfo, tree otype, tree_code code,
236 : tree itype, tree *vecotype_out,
237 : tree *vecitype_out = NULL,
238 : enum optab_subtype subtype = optab_default)
239 : {
240 817 : tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
241 817 : if (!vecitype)
242 : return false;
243 :
244 815 : tree vecotype = get_vectype_for_scalar_type (vinfo, otype);
245 815 : if (!vecotype)
246 : return false;
247 :
248 815 : optab optab = optab_for_tree_code (code, vecitype, subtype);
249 815 : if (!optab)
250 : return false;
251 :
252 815 : insn_code icode = optab_handler (optab, TYPE_MODE (vecitype));
253 815 : if (icode == CODE_FOR_nothing
254 815 : || insn_data[icode].operand[0].mode != TYPE_MODE (vecotype))
255 394 : return false;
256 :
257 421 : *vecotype_out = vecotype;
258 421 : if (vecitype_out)
259 412 : *vecitype_out = vecitype;
260 : return true;
261 : }
262 :
263 : /* Return true if the target supports a vector version of CODE,
264 : where CODE is known to map to a conversion optab with the given SUBTYPE.
265 : ITYPE specifies the type of (some of) the scalar inputs and OTYPE
266 : specifies the type of the scalar result.
267 :
268 : When returning true, set *VECOTYPE_OUT to the vector version of OTYPE.
269 : Also set *VECITYPE_OUT to the vector version of ITYPE if VECITYPE_OUT
270 : is nonnull. */
271 :
272 : static bool
273 3694 : vect_supportable_conv_optab_p (vec_info *vinfo, tree otype, tree_code code,
274 : tree itype, tree *vecotype_out,
275 : tree *vecitype_out = NULL,
276 : enum optab_subtype subtype = optab_default)
277 : {
278 3694 : tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
279 3694 : tree vecotype = get_vectype_for_scalar_type (vinfo, otype);
280 3694 : if (!vecitype || !vecotype)
281 : return false;
282 :
283 3432 : if (!directly_supported_p (code, vecotype, vecitype, subtype))
284 : return false;
285 :
286 720 : *vecotype_out = vecotype;
287 720 : if (vecitype_out)
288 720 : *vecitype_out = vecitype;
289 : return true;
290 : }
291 :
292 : /* Round bit precision PRECISION up to a full element. */
293 :
294 : static unsigned int
295 3322186 : vect_element_precision (unsigned int precision)
296 : {
297 0 : precision = 1 << ceil_log2 (precision);
298 4978236 : return MAX (precision, BITS_PER_UNIT);
299 : }
300 :
301 : /* If OP is defined by a statement that's being considered for vectorization,
302 : return information about that statement, otherwise return NULL. */
303 :
304 : static stmt_vec_info
305 359134 : vect_get_internal_def (vec_info *vinfo, tree op)
306 : {
307 359134 : stmt_vec_info def_stmt_info = vinfo->lookup_def (op);
308 359134 : if (def_stmt_info
309 345648 : && STMT_VINFO_DEF_TYPE (def_stmt_info) == vect_internal_def)
310 327889 : return vect_stmt_to_vectorize (def_stmt_info);
311 : return NULL;
312 : }
313 :
314 : /* Holds information about an input operand after some sign changes
315 : and type promotions have been peeled away. */
316 : class vect_unpromoted_value {
317 : public:
318 : vect_unpromoted_value ();
319 :
320 : void set_op (tree, vect_def_type, stmt_vec_info = NULL);
321 :
322 : /* The value obtained after peeling away zero or more casts. */
323 : tree op;
324 :
325 : /* The type of OP. */
326 : tree type;
327 :
328 : /* The definition type of OP. */
329 : vect_def_type dt;
330 :
331 : /* If OP is the result of peeling at least one cast, and if the cast
332 : of OP itself is a vectorizable statement, CASTER identifies that
333 : statement, otherwise it is null. */
334 : stmt_vec_info caster;
335 : };
336 :
337 307578723 : inline vect_unpromoted_value::vect_unpromoted_value ()
338 307578723 : : op (NULL_TREE),
339 307578723 : type (NULL_TREE),
340 307578723 : dt (vect_uninitialized_def),
341 3298329 : caster (NULL)
342 : {
343 : }
344 :
345 : /* Set the operand to OP_IN, its definition type to DT_IN, and the
346 : statement that casts it to CASTER_IN. */
347 :
348 : inline void
349 11654762 : vect_unpromoted_value::set_op (tree op_in, vect_def_type dt_in,
350 : stmt_vec_info caster_in)
351 : {
352 11654762 : op = op_in;
353 11654762 : type = TREE_TYPE (op);
354 11654762 : dt = dt_in;
355 11654762 : caster = caster_in;
356 11654762 : }
357 :
358 : /* If OP is a vectorizable SSA name, strip a sequence of integer conversions
359 : to reach some vectorizable inner operand OP', continuing as long as it
360 : is possible to convert OP' back to OP using a possible sign change
361 : followed by a possible promotion P. Return this OP', or null if OP is
362 : not a vectorizable SSA name. If there is a promotion P, describe its
363 : input in UNPROM, otherwise describe OP' in UNPROM. If SINGLE_USE_P
364 : is nonnull, set *SINGLE_USE_P to false if any of the SSA names involved
365 : have more than one user.
366 :
367 : A successful return means that it is possible to go from OP' to OP
368 : via UNPROM. The cast from OP' to UNPROM is at most a sign change,
369 : whereas the cast from UNPROM to OP might be a promotion, a sign
370 : change, or a nop.
371 :
372 : E.g. say we have:
373 :
374 : signed short *ptr = ...;
375 : signed short C = *ptr;
376 : unsigned short B = (unsigned short) C; // sign change
377 : signed int A = (signed int) B; // unsigned promotion
378 : ...possible other uses of A...
379 : unsigned int OP = (unsigned int) A; // sign change
380 :
381 : In this case it's possible to go directly from C to OP using:
382 :
383 : OP = (unsigned int) (unsigned short) C;
384 : +------------+ +--------------+
385 : promotion sign change
386 :
387 : so OP' would be C. The input to the promotion is B, so UNPROM
388 : would describe B. */
389 :
390 : static tree
391 8615693 : vect_look_through_possible_promotion (vec_info *vinfo, tree op,
392 : vect_unpromoted_value *unprom,
393 : bool *single_use_p = NULL)
394 : {
395 8615693 : tree op_type = TREE_TYPE (op);
396 8615693 : if (!INTEGRAL_TYPE_P (op_type))
397 : return NULL_TREE;
398 :
399 8564480 : tree res = NULL_TREE;
400 8564480 : unsigned int orig_precision = TYPE_PRECISION (op_type);
401 8564480 : unsigned int min_precision = orig_precision;
402 8564480 : stmt_vec_info caster = NULL;
403 10239742 : while (TREE_CODE (op) == SSA_NAME && INTEGRAL_TYPE_P (op_type))
404 : {
405 : /* See whether OP is simple enough to vectorize. */
406 10007171 : stmt_vec_info def_stmt_info;
407 10007171 : gimple *def_stmt;
408 10007171 : vect_def_type dt;
409 10007171 : if (!vect_is_simple_use (op, vinfo, &dt, &def_stmt_info, &def_stmt))
410 : break;
411 :
412 : /* If OP is the input of a demotion, skip over it to see whether
413 : OP is itself the result of a promotion. If so, the combined
414 : effect of the promotion and the demotion might fit the required
415 : pattern, otherwise neither operation fits.
416 :
417 : This copes with cases such as the result of an arithmetic
418 : operation being truncated before being stored, and where that
419 : arithmetic operation has been recognized as an over-widened one. */
420 9990448 : if (TYPE_PRECISION (op_type) <= min_precision)
421 : {
422 : /* Use OP as the UNPROM described above if we haven't yet
423 : found a promotion, or if using the new input preserves the
424 : sign of the previous promotion. */
425 9860840 : if (!res
426 1426193 : || TYPE_PRECISION (unprom->type) == orig_precision
427 38202 : || TYPE_SIGN (unprom->type) == TYPE_SIGN (op_type)
428 9896181 : || (TYPE_UNSIGNED (op_type)
429 24872 : && TYPE_PRECISION (op_type) < TYPE_PRECISION (unprom->type)))
430 : {
431 9825938 : unprom->set_op (op, dt, caster);
432 9825938 : min_precision = TYPE_PRECISION (op_type);
433 : }
434 : /* Stop if we've already seen a promotion and if this
435 : conversion does more than change the sign. */
436 34902 : else if (TYPE_PRECISION (op_type)
437 34902 : != TYPE_PRECISION (unprom->type))
438 : break;
439 :
440 : /* The sequence now extends to OP. */
441 : res = op;
442 : }
443 :
444 : /* See whether OP is defined by a cast. Record it as CASTER if
445 : the cast is potentially vectorizable. */
446 9990404 : if (!def_stmt)
447 : break;
448 9777889 : caster = def_stmt_info;
449 :
450 : /* Ignore pattern statements, since we don't link uses for them. */
451 9777889 : if (caster
452 9777889 : && single_use_p
453 2004390 : && !STMT_VINFO_RELATED_STMT (caster)
454 11635002 : && !has_single_use (res))
455 1114638 : *single_use_p = false;
456 :
457 18109798 : gassign *assign = dyn_cast <gassign *> (def_stmt);
458 6191158 : if (!assign || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
459 : break;
460 :
461 : /* Continue with the input to the cast. */
462 1675262 : op = gimple_assign_rhs1 (def_stmt);
463 1675262 : op_type = TREE_TYPE (op);
464 : }
465 : return res;
466 : }
467 :
468 : /* OP is an integer operand to an operation that returns TYPE, and we
469 : want to treat the operation as a widening one. So far we can treat
470 : it as widening from *COMMON_TYPE.
471 :
472 : Return true if OP is suitable for such a widening operation,
473 : either widening from *COMMON_TYPE or from some supertype of it.
474 : Update *COMMON_TYPE to the supertype in the latter case.
475 :
476 : SHIFT_P is true if OP is a shift amount. */
477 :
478 : static bool
479 320067 : vect_joust_widened_integer (tree type, bool shift_p, tree op,
480 : tree *common_type)
481 : {
482 : /* Calculate the minimum precision required by OP, without changing
483 : the sign of either operand. */
484 320067 : unsigned int precision;
485 320067 : if (shift_p)
486 : {
487 13252 : if (!wi::leu_p (wi::to_widest (op), TYPE_PRECISION (type) / 2))
488 : return false;
489 10671 : precision = TREE_INT_CST_LOW (op);
490 : }
491 : else
492 : {
493 306815 : precision = wi::min_precision (wi::to_widest (op),
494 306815 : TYPE_SIGN (*common_type));
495 306815 : if (precision * 2 > TYPE_PRECISION (type))
496 : return false;
497 : }
498 :
499 : /* If OP requires a wider type, switch to that type. The checks
500 : above ensure that this is still narrower than the result. */
501 303282 : precision = vect_element_precision (precision);
502 303282 : if (TYPE_PRECISION (*common_type) < precision)
503 6486 : *common_type = build_nonstandard_integer_type
504 6486 : (precision, TYPE_UNSIGNED (*common_type));
505 : return true;
506 : }
507 :
508 : /* Return true if the common supertype of NEW_TYPE and *COMMON_TYPE
509 : is narrower than type, storing the supertype in *COMMON_TYPE if so. */
510 :
511 : static bool
512 35254 : vect_joust_widened_type (tree type, tree new_type, tree *common_type)
513 : {
514 35254 : if (types_compatible_p (*common_type, new_type))
515 : return true;
516 :
517 : /* See if *COMMON_TYPE can hold all values of NEW_TYPE. */
518 3700 : if ((TYPE_PRECISION (new_type) < TYPE_PRECISION (*common_type))
519 3700 : && (TYPE_UNSIGNED (new_type) || !TYPE_UNSIGNED (*common_type)))
520 : return true;
521 :
522 : /* See if NEW_TYPE can hold all values of *COMMON_TYPE. */
523 3192 : if (TYPE_PRECISION (*common_type) < TYPE_PRECISION (new_type)
524 3192 : && (TYPE_UNSIGNED (*common_type) || !TYPE_UNSIGNED (new_type)))
525 : {
526 340 : *common_type = new_type;
527 340 : return true;
528 : }
529 :
530 : /* We have mismatched signs, with the signed type being
531 : no wider than the unsigned type. In this case we need
532 : a wider signed type. */
533 2852 : unsigned int precision = MAX (TYPE_PRECISION (*common_type),
534 : TYPE_PRECISION (new_type));
535 2852 : precision *= 2;
536 :
537 2852 : if (precision * 2 > TYPE_PRECISION (type))
538 : return false;
539 :
540 40 : *common_type = build_nonstandard_integer_type (precision, false);
541 40 : return true;
542 : }
543 :
544 : /* Check whether STMT_INFO can be viewed as a tree of integer operations
545 : in which each node either performs CODE or WIDENED_CODE, and where
546 : each leaf operand is narrower than the result of STMT_INFO. MAX_NOPS
547 : specifies the maximum number of leaf operands. SHIFT_P says whether
548 : CODE and WIDENED_CODE are some sort of shift.
549 :
550 : If STMT_INFO is such a tree, return the number of leaf operands
551 : and describe them in UNPROM[0] onwards. Also set *COMMON_TYPE
552 : to a type that (a) is narrower than the result of STMT_INFO and
553 : (b) can hold all leaf operand values.
554 :
555 : If SUBTYPE then allow that the signs of the operands
556 : may differ in signs but not in precision. SUBTYPE is updated to reflect
557 : this.
558 :
559 : Return 0 if STMT_INFO isn't such a tree, or if no such COMMON_TYPE
560 : exists. */
561 :
562 : static unsigned int
563 129305032 : vect_widened_op_tree (vec_info *vinfo, stmt_vec_info stmt_info, tree_code code,
564 : code_helper widened_code, bool shift_p,
565 : unsigned int max_nops,
566 : vect_unpromoted_value *unprom, tree *common_type,
567 : enum optab_subtype *subtype = NULL)
568 : {
569 : /* Check for an integer operation with the right code. */
570 129305032 : gimple* stmt = stmt_info->stmt;
571 129305032 : if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
572 : return 0;
573 :
574 106326222 : code_helper rhs_code;
575 106326222 : if (is_gimple_assign (stmt))
576 91237168 : rhs_code = gimple_assign_rhs_code (stmt);
577 15089054 : else if (is_gimple_call (stmt))
578 15089054 : rhs_code = gimple_call_combined_fn (stmt);
579 : else
580 : return 0;
581 :
582 106326222 : if (rhs_code != code
583 106326222 : && rhs_code != widened_code)
584 : return 0;
585 :
586 6769551 : tree lhs = gimple_get_lhs (stmt);
587 6769551 : tree type = TREE_TYPE (lhs);
588 6769551 : if (!INTEGRAL_TYPE_P (type))
589 : return 0;
590 :
591 : /* Assume that both operands will be leaf operands. */
592 5889906 : max_nops -= 2;
593 :
594 : /* Check the operands. */
595 5889906 : unsigned int next_op = 0;
596 6660106 : for (unsigned int i = 0; i < 2; ++i)
597 : {
598 6323959 : vect_unpromoted_value *this_unprom = &unprom[next_op];
599 6323959 : unsigned int nops = 1;
600 6323959 : tree op = gimple_arg (stmt, i);
601 6323959 : if (i == 1 && TREE_CODE (op) == INTEGER_CST)
602 : {
603 : /* We already have a common type from earlier operands.
604 : Update it to account for OP. */
605 320067 : this_unprom->set_op (op, vect_constant_def);
606 320067 : if (!vect_joust_widened_integer (type, shift_p, op, common_type))
607 : return 0;
608 : }
609 : else
610 : {
611 : /* Only allow shifts by constants. */
612 6003892 : if (shift_p && i == 1)
613 : return 0;
614 :
615 5997834 : if (rhs_code != code)
616 : {
617 : /* If rhs_code is widened_code, don't look through further
618 : possible promotions, there is a promotion already embedded
619 : in the WIDEN_*_EXPR. */
620 1664 : if (TREE_CODE (op) != SSA_NAME
621 1664 : || !INTEGRAL_TYPE_P (TREE_TYPE (op)))
622 0 : return 0;
623 :
624 1664 : stmt_vec_info def_stmt_info;
625 1664 : gimple *def_stmt;
626 1664 : vect_def_type dt;
627 1664 : if (!vect_is_simple_use (op, vinfo, &dt, &def_stmt_info,
628 : &def_stmt))
629 : return 0;
630 1664 : this_unprom->set_op (op, dt, NULL);
631 : }
632 5996170 : else if (!vect_look_through_possible_promotion (vinfo, op,
633 : this_unprom))
634 : return 0;
635 :
636 5872308 : if (TYPE_PRECISION (this_unprom->type) == TYPE_PRECISION (type))
637 : {
638 : /* The operand isn't widened. If STMT_INFO has the code
639 : for an unwidened operation, recursively check whether
640 : this operand is a node of the tree. */
641 5398811 : if (rhs_code != code
642 5398811 : || max_nops == 0
643 5399277 : || this_unprom->dt != vect_internal_def)
644 : return 0;
645 :
646 : /* Give back the leaf slot allocated above now that we're
647 : not treating this as a leaf operand. */
648 466 : max_nops += 1;
649 :
650 : /* Recursively process the definition of the operand. */
651 466 : stmt_vec_info def_stmt_info
652 466 : = vect_get_internal_def (vinfo, this_unprom->op);
653 :
654 466 : nops = vect_widened_op_tree (vinfo, def_stmt_info, code,
655 : widened_code, shift_p, max_nops,
656 : this_unprom, common_type,
657 : subtype);
658 466 : if (nops == 0)
659 : return 0;
660 :
661 311 : max_nops -= nops;
662 : }
663 : else
664 : {
665 : /* Make sure that the operand is narrower than the result. */
666 473497 : if (TYPE_PRECISION (this_unprom->type) * 2
667 473497 : > TYPE_PRECISION (type))
668 : return 0;
669 :
670 : /* Update COMMON_TYPE for the new operand. */
671 469134 : if (i == 0)
672 433880 : *common_type = this_unprom->type;
673 35254 : else if (!vect_joust_widened_type (type, this_unprom->type,
674 : common_type))
675 : {
676 2812 : if (subtype)
677 : {
678 : /* See if we can sign extend the smaller type. */
679 285 : if (TYPE_PRECISION (this_unprom->type)
680 285 : > TYPE_PRECISION (*common_type))
681 27 : *common_type = this_unprom->type;
682 285 : *subtype = optab_vector_mixed_sign;
683 : }
684 : else
685 : return 0;
686 : }
687 : }
688 : }
689 770200 : next_op += nops;
690 : }
691 : return next_op;
692 : }
693 :
694 : /* Helper to return a new temporary for pattern of TYPE for STMT. If STMT
695 : is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */
696 :
697 : static tree
698 2152329 : vect_recog_temp_ssa_var (tree type, gimple *stmt = NULL)
699 : {
700 0 : return make_temp_ssa_name (type, stmt, "patt");
701 : }
702 :
703 : /* STMT2_INFO describes a type conversion that could be split into STMT1
704 : followed by a version of STMT2_INFO that takes NEW_RHS as its first
705 : input. Try to do this using pattern statements, returning true on
706 : success. */
707 :
708 : static bool
709 30767 : vect_split_statement (vec_info *vinfo, stmt_vec_info stmt2_info, tree new_rhs,
710 : gimple *stmt1, tree vectype)
711 : {
712 30767 : if (is_pattern_stmt_p (stmt2_info))
713 : {
714 : /* STMT2_INFO is part of a pattern. Get the statement to which
715 : the pattern is attached. */
716 460 : stmt_vec_info orig_stmt2_info = STMT_VINFO_RELATED_STMT (stmt2_info);
717 460 : vect_init_pattern_stmt (vinfo, stmt1, orig_stmt2_info, vectype);
718 :
719 460 : if (dump_enabled_p ())
720 19 : dump_printf_loc (MSG_NOTE, vect_location,
721 : "Splitting pattern statement: %G", stmt2_info->stmt);
722 :
723 : /* Since STMT2_INFO is a pattern statement, we can change it
724 : in-situ without worrying about changing the code for the
725 : containing block. */
726 460 : gimple_assign_set_rhs1 (stmt2_info->stmt, new_rhs);
727 :
728 460 : if (dump_enabled_p ())
729 : {
730 19 : dump_printf_loc (MSG_NOTE, vect_location, "into: %G", stmt1);
731 19 : dump_printf_loc (MSG_NOTE, vect_location, "and: %G",
732 : stmt2_info->stmt);
733 : }
734 :
735 460 : gimple_seq *def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt2_info);
736 460 : if (STMT_VINFO_RELATED_STMT (orig_stmt2_info) == stmt2_info)
737 : /* STMT2_INFO is the actual pattern statement. Add STMT1
738 : to the end of the definition sequence. */
739 457 : gimple_seq_add_stmt_without_update (def_seq, stmt1);
740 : else
741 : {
742 : /* STMT2_INFO belongs to the definition sequence. Insert STMT1
743 : before it. */
744 3 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt2_info->stmt, def_seq);
745 3 : gsi_insert_before_without_update (&gsi, stmt1, GSI_SAME_STMT);
746 : }
747 460 : return true;
748 : }
749 : else
750 : {
751 : /* STMT2_INFO doesn't yet have a pattern. Try to create a
752 : two-statement pattern now. */
753 30307 : gcc_assert (!STMT_VINFO_RELATED_STMT (stmt2_info));
754 30307 : tree lhs_type = TREE_TYPE (gimple_get_lhs (stmt2_info->stmt));
755 30307 : tree lhs_vectype = get_vectype_for_scalar_type (vinfo, lhs_type);
756 30307 : if (!lhs_vectype)
757 : return false;
758 :
759 30307 : if (dump_enabled_p ())
760 1937 : dump_printf_loc (MSG_NOTE, vect_location,
761 : "Splitting statement: %G", stmt2_info->stmt);
762 :
763 : /* Add STMT1 as a singleton pattern definition sequence. */
764 30307 : gimple_seq *def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (stmt2_info);
765 30307 : vect_init_pattern_stmt (vinfo, stmt1, stmt2_info, vectype);
766 30307 : gimple_seq_add_stmt_without_update (def_seq, stmt1);
767 :
768 : /* Build the second of the two pattern statements. */
769 30307 : tree new_lhs = vect_recog_temp_ssa_var (lhs_type, NULL);
770 30307 : gassign *new_stmt2 = gimple_build_assign (new_lhs, NOP_EXPR, new_rhs);
771 30307 : vect_set_pattern_stmt (vinfo, new_stmt2, stmt2_info, lhs_vectype);
772 :
773 30307 : if (dump_enabled_p ())
774 : {
775 1937 : dump_printf_loc (MSG_NOTE, vect_location,
776 : "into pattern statements: %G", stmt1);
777 1937 : dump_printf_loc (MSG_NOTE, vect_location, "and: %G",
778 : (gimple *) new_stmt2);
779 : }
780 :
781 30307 : return true;
782 : }
783 : }
784 :
785 : /* Look for the following pattern
786 : X = x[i]
787 : Y = y[i]
788 : DIFF = X - Y
789 : DAD = ABS_EXPR<DIFF>
790 :
791 : ABS_STMT should point to a statement of code ABS_EXPR or ABSU_EXPR.
792 : HALF_TYPE and UNPROM will be set should the statement be found to
793 : be a widened operation.
794 : DIFF_STMT will be set to the MINUS_EXPR
795 : statement that precedes the ABS_STMT if it is a MINUS_EXPR..
796 : */
797 : static bool
798 22426750 : vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt,
799 : tree *half_type,
800 : vect_unpromoted_value unprom[2],
801 : gassign **diff_stmt)
802 : {
803 22426750 : if (!abs_stmt)
804 : return false;
805 :
806 : /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
807 : inside the loop (in case we are analyzing an outer-loop). */
808 22426750 : enum tree_code code = gimple_assign_rhs_code (abs_stmt);
809 22426750 : if (code != ABS_EXPR && code != ABSU_EXPR)
810 : return false;
811 :
812 33187 : tree abs_oprnd = gimple_assign_rhs1 (abs_stmt);
813 33187 : tree abs_type = TREE_TYPE (abs_oprnd);
814 33187 : if (!abs_oprnd)
815 : return false;
816 25457 : if (!ANY_INTEGRAL_TYPE_P (abs_type)
817 7984 : || TYPE_OVERFLOW_WRAPS (abs_type)
818 41014 : || TYPE_UNSIGNED (abs_type))
819 : return false;
820 :
821 : /* Peel off conversions from the ABS input. This can involve sign
822 : changes (e.g. from an unsigned subtraction to a signed ABS input)
823 : or signed promotion, but it can't include unsigned promotion.
824 : (Note that ABS of an unsigned promotion should have been folded
825 : away before now anyway.) */
826 7827 : vect_unpromoted_value unprom_diff;
827 7827 : abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd,
828 : &unprom_diff);
829 7827 : if (!abs_oprnd)
830 : return false;
831 7573 : if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type)
832 7573 : && TYPE_UNSIGNED (unprom_diff.type))
833 : return false;
834 :
835 : /* We then detect if the operand of abs_expr is defined by a minus_expr. */
836 7573 : stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd);
837 7573 : if (!diff_stmt_vinfo)
838 : return false;
839 :
840 7393 : gassign *diff = dyn_cast <gassign *> (STMT_VINFO_STMT (diff_stmt_vinfo));
841 7393 : if (diff_stmt && diff
842 5973 : && gimple_assign_rhs_code (diff) == MINUS_EXPR
843 9402 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd)))
844 281 : *diff_stmt = diff;
845 :
846 : /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
847 : inside the loop (in case we are analyzing an outer-loop). */
848 7393 : if (vect_widened_op_tree (vinfo, diff_stmt_vinfo,
849 7393 : MINUS_EXPR, IFN_VEC_WIDEN_MINUS,
850 : false, 2, unprom, half_type))
851 : return true;
852 :
853 : return false;
854 : }
855 :
856 : /* Convert UNPROM to TYPE and return the result, adding new statements
857 : to STMT_INFO's pattern definition statements if no better way is
858 : available. VECTYPE is the vector form of TYPE.
859 :
860 : If SUBTYPE then convert the type based on the subtype. */
861 :
862 : static tree
863 492230 : vect_convert_input (vec_info *vinfo, stmt_vec_info stmt_info, tree type,
864 : vect_unpromoted_value *unprom, tree vectype,
865 : enum optab_subtype subtype = optab_default)
866 : {
867 : /* Update the type if the signs differ. */
868 492230 : if (subtype == optab_vector_mixed_sign)
869 : {
870 322 : gcc_assert (!TYPE_UNSIGNED (type));
871 322 : if (TYPE_UNSIGNED (TREE_TYPE (unprom->op)))
872 : {
873 161 : type = unsigned_type_for (type);
874 161 : vectype = unsigned_type_for (vectype);
875 : }
876 : }
877 :
878 : /* Check for a no-op conversion. */
879 492230 : if (types_compatible_p (type, TREE_TYPE (unprom->op)))
880 170586 : return unprom->op;
881 :
882 : /* Allow the caller to create constant vect_unpromoted_values. */
883 321644 : if (TREE_CODE (unprom->op) == INTEGER_CST)
884 199204 : return wide_int_to_tree (type, wi::to_widest (unprom->op));
885 :
886 122440 : tree input = unprom->op;
887 122440 : if (unprom->caster)
888 : {
889 65475 : tree lhs = gimple_get_lhs (unprom->caster->stmt);
890 65475 : tree lhs_type = TREE_TYPE (lhs);
891 :
892 : /* If the result of the existing cast is the right width, use it
893 : instead of the source of the cast. */
894 65475 : if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
895 : input = lhs;
896 : /* If the precision we want is between the source and result
897 : precisions of the existing cast, try splitting the cast into
898 : two and tapping into a mid-way point. */
899 63378 : else if (TYPE_PRECISION (lhs_type) > TYPE_PRECISION (type)
900 63378 : && TYPE_PRECISION (type) > TYPE_PRECISION (unprom->type))
901 : {
902 : /* In order to preserve the semantics of the original cast,
903 : give the mid-way point the same signedness as the input value.
904 :
905 : It would be possible to use a signed type here instead if
906 : TYPE is signed and UNPROM->TYPE is unsigned, but that would
907 : make the sign of the midtype sensitive to the order in
908 : which we process the statements, since the signedness of
909 : TYPE is the signedness required by just one of possibly
910 : many users. Also, unsigned promotions are usually as cheap
911 : as or cheaper than signed ones, so it's better to keep an
912 : unsigned promotion. */
913 30767 : tree midtype = build_nonstandard_integer_type
914 30767 : (TYPE_PRECISION (type), TYPE_UNSIGNED (unprom->type));
915 30767 : tree vec_midtype = get_vectype_for_scalar_type (vinfo, midtype);
916 30767 : if (vec_midtype)
917 : {
918 30767 : input = vect_recog_temp_ssa_var (midtype, NULL);
919 30767 : gassign *new_stmt = gimple_build_assign (input, NOP_EXPR,
920 : unprom->op);
921 30767 : if (!vect_split_statement (vinfo, unprom->caster, input, new_stmt,
922 : vec_midtype))
923 0 : append_pattern_def_seq (vinfo, stmt_info,
924 : new_stmt, vec_midtype);
925 : }
926 : }
927 :
928 : /* See if we can reuse an existing result. */
929 65475 : if (types_compatible_p (type, TREE_TYPE (input)))
930 : return input;
931 : }
932 :
933 : /* We need a new conversion statement. */
934 99859 : tree new_op = vect_recog_temp_ssa_var (type, NULL);
935 99859 : gassign *new_stmt = gimple_build_assign (new_op, NOP_EXPR, input);
936 :
937 : /* If OP is an external value, see if we can insert the new statement
938 : on an incoming edge. */
939 99859 : if (input == unprom->op && unprom->dt == vect_external_def)
940 8618 : if (edge e = vect_get_external_def_edge (vinfo, input))
941 : {
942 804 : basic_block new_bb = gsi_insert_on_edge_immediate (e, new_stmt);
943 804 : gcc_assert (!new_bb);
944 : return new_op;
945 : }
946 :
947 : /* As a (common) last resort, add the statement to the pattern itself. */
948 99055 : append_pattern_def_seq (vinfo, stmt_info, new_stmt, vectype);
949 99055 : return new_op;
950 : }
951 :
952 : /* Invoke vect_convert_input for N elements of UNPROM and store the
953 : result in the corresponding elements of RESULT.
954 :
955 : If SUBTYPE then convert the type based on the subtype. */
956 :
957 : static void
958 250049 : vect_convert_inputs (vec_info *vinfo, stmt_vec_info stmt_info, unsigned int n,
959 : tree *result, tree type, vect_unpromoted_value *unprom,
960 : tree vectype, enum optab_subtype subtype = optab_default)
961 : {
962 742258 : for (unsigned int i = 0; i < n; ++i)
963 : {
964 : unsigned int j;
965 733890 : for (j = 0; j < i; ++j)
966 242160 : if (unprom[j].op == unprom[i].op)
967 : break;
968 :
969 492209 : if (j < i)
970 479 : result[i] = result[j];
971 : else
972 491730 : result[i] = vect_convert_input (vinfo, stmt_info,
973 491730 : type, &unprom[i], vectype, subtype);
974 : }
975 250049 : }
976 :
977 : /* The caller has created a (possibly empty) sequence of pattern definition
978 : statements followed by a single statement PATTERN_STMT. Cast the result
979 : of this final statement to TYPE. If a new statement is needed, add
980 : PATTERN_STMT to the end of STMT_INFO's pattern definition statements
981 : and return the new statement, otherwise return PATTERN_STMT as-is.
982 : VECITYPE is the vector form of PATTERN_STMT's result type. */
983 :
984 : static gimple *
985 276533 : vect_convert_output (vec_info *vinfo, stmt_vec_info stmt_info, tree type,
986 : gimple *pattern_stmt, tree vecitype)
987 : {
988 276533 : tree lhs = gimple_get_lhs (pattern_stmt);
989 276533 : if (!types_compatible_p (type, TREE_TYPE (lhs)))
990 : {
991 246015 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vecitype);
992 246015 : tree cast_var = vect_recog_temp_ssa_var (type, NULL);
993 246015 : pattern_stmt = gimple_build_assign (cast_var, NOP_EXPR, lhs);
994 : }
995 276533 : return pattern_stmt;
996 : }
997 :
998 : /* Return true if STMT_VINFO describes a reduction for which reassociation
999 : is allowed. If STMT_INFO is part of a group, assume that it's part of
1000 : a reduction chain and optimistically assume that all statements
1001 : except the last allow reassociation.
1002 : Also require it to have code CODE and to be a reduction
1003 : in the outermost loop. When returning true, store the operands in
1004 : *OP0_OUT and *OP1_OUT. */
1005 :
1006 : static bool
1007 96452361 : vect_reassociating_reduction_p (vec_info *vinfo,
1008 : stmt_vec_info stmt_info, tree_code code,
1009 : tree *op0_out, tree *op1_out)
1010 : {
1011 96452361 : loop_vec_info loop_info = dyn_cast <loop_vec_info> (vinfo);
1012 13631019 : if (!loop_info)
1013 : return false;
1014 :
1015 13631019 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
1016 14880396 : if (!assign || gimple_assign_rhs_code (assign) != code)
1017 : return false;
1018 :
1019 : /* We don't allow changing the order of the computation in the inner-loop
1020 : when doing outer-loop vectorization. */
1021 2656118 : class loop *loop = LOOP_VINFO_LOOP (loop_info);
1022 98949222 : if (loop && nested_in_vect_loop_p (loop, stmt_info))
1023 : return false;
1024 :
1025 2602649 : if (!vect_is_reduction (stmt_info))
1026 : return false;
1027 :
1028 171008 : if (needs_fold_left_reduction_p (TREE_TYPE (gimple_assign_lhs (assign)),
1029 171008 : code))
1030 : return false;
1031 :
1032 159257 : *op0_out = gimple_assign_rhs1 (assign);
1033 159257 : *op1_out = gimple_assign_rhs2 (assign);
1034 159257 : if (commutative_tree_code (code) && STMT_VINFO_REDUC_IDX (stmt_info) == 0)
1035 63786 : std::swap (*op0_out, *op1_out);
1036 : return true;
1037 : }
1038 :
1039 : /* Return true iff the target has a vector optab implementing the operation
1040 : CODE on type VECTYPE with SUBTYPE. */
1041 :
1042 : static bool
1043 869649 : target_has_vecop_for_code (tree_code code, tree vectype,
1044 : enum optab_subtype subtype = optab_vector)
1045 : {
1046 869649 : optab voptab = optab_for_tree_code (code, vectype, subtype);
1047 869649 : return voptab && can_implement_p (voptab, TYPE_MODE (vectype));
1048 : }
1049 :
1050 : /* match.pd function to match
1051 : (cond (cmp@3 a b) (convert@1 c) (convert@2 d))
1052 : with conditions:
1053 : 1) @1, @2, c, d, a, b are all integral type.
1054 : 2) There's single_use for both @1 and @2.
1055 : 3) a, c have same precision.
1056 : 4) c and @1 have different precision.
1057 : 5) c, d are the same type or they can differ in sign when convert is
1058 : truncation.
1059 :
1060 : record a and c and d and @3. */
1061 :
1062 : extern bool gimple_cond_expr_convert_p (tree, tree*, tree (*)(tree));
1063 :
1064 : /* Function vect_recog_cond_expr_convert
1065 :
1066 : Try to find the following pattern:
1067 :
1068 : TYPE_AB A,B;
1069 : TYPE_CD C,D;
1070 : TYPE_E E;
1071 : TYPE_E op_true = (TYPE_E) A;
1072 : TYPE_E op_false = (TYPE_E) B;
1073 :
1074 : E = C cmp D ? op_true : op_false;
1075 :
1076 : where
1077 : TYPE_PRECISION (TYPE_E) != TYPE_PRECISION (TYPE_CD);
1078 : TYPE_PRECISION (TYPE_AB) == TYPE_PRECISION (TYPE_CD);
1079 : single_use of op_true and op_false.
1080 : TYPE_AB could differ in sign when (TYPE_E) A is a truncation.
1081 :
1082 : Input:
1083 :
1084 : * STMT_VINFO: The stmt from which the pattern search begins.
1085 : here it starts with E = c cmp D ? op_true : op_false;
1086 :
1087 : Output:
1088 :
1089 : TYPE1 E' = C cmp D ? A : B;
1090 : TYPE3 E = (TYPE3) E';
1091 :
1092 : There may extra nop_convert for A or B to handle different signness.
1093 :
1094 : * TYPE_OUT: The vector type of the output of this pattern.
1095 :
1096 : * Return value: A new stmt that will be used to replace the sequence of
1097 : stmts that constitute the pattern. In this case it will be:
1098 : E = (TYPE3)E';
1099 : E' = C cmp D ? A : B; is recorded in pattern definition statements; */
1100 :
1101 : static gimple *
1102 32227591 : vect_recog_cond_expr_convert_pattern (vec_info *vinfo,
1103 : stmt_vec_info stmt_vinfo, tree *type_out)
1104 : {
1105 32227591 : gassign *last_stmt = dyn_cast <gassign *> (stmt_vinfo->stmt);
1106 22520095 : tree lhs, match[4], temp, type, new_lhs, op2, op1;
1107 22520095 : gimple *cond_stmt;
1108 22520095 : gimple *pattern_stmt;
1109 32227562 : enum tree_code code = NOP_EXPR;
1110 :
1111 22520095 : if (!last_stmt)
1112 : return NULL;
1113 :
1114 22520095 : lhs = gimple_assign_lhs (last_stmt);
1115 :
1116 : /* Find E = C cmp D ? (TYPE3) A ? (TYPE3) B;
1117 : TYPE_PRECISION (A) == TYPE_PRECISION (C). */
1118 22520095 : if (!gimple_cond_expr_convert_p (lhs, &match[0], NULL))
1119 : return NULL;
1120 :
1121 29 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs)))
1122 20 : code = INTEGRAL_TYPE_P (TREE_TYPE (match[1])) ? FLOAT_EXPR : CONVERT_EXPR;
1123 9 : else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (match[1])))
1124 0 : code = FIX_TRUNC_EXPR;
1125 :
1126 29 : op1 = match[1];
1127 29 : op2 = match[2];
1128 29 : type = TREE_TYPE (op1);
1129 : /* When op1/op2 is REAL_CST, the conversion must be CONVERT_EXPR from
1130 : SCALAR_FLOAT_TYPE_P which is restricted in gimple_cond_expr_convert_p.
1131 : Otherwise, the conversion could be FLOAT_EXPR, FIX_TRUNC_EXPR
1132 : or CONVERT_EXPR. */
1133 29 : if (TREE_CODE (op1) == REAL_CST)
1134 : {
1135 20 : op1 = const_unop (CONVERT_EXPR, TREE_TYPE (op2), op1);
1136 20 : type = TREE_TYPE (op2);
1137 20 : if (op1 == NULL_TREE)
1138 : return NULL;
1139 : }
1140 9 : else if (TREE_CODE (op2) == REAL_CST)
1141 : {
1142 0 : op2 = const_unop (FLOAT_EXPR, TREE_TYPE (op1), op2);
1143 0 : if (op2 == NULL_TREE)
1144 : return NULL;
1145 : }
1146 9 : else if (code == NOP_EXPR)
1147 : {
1148 9 : if (TYPE_SIGN (type) != TYPE_SIGN (TREE_TYPE (match[2])))
1149 : {
1150 9 : op2 = vect_recog_temp_ssa_var (type, NULL);
1151 9 : gimple* nop_stmt = gimple_build_assign (op2, NOP_EXPR, match[2]);
1152 9 : append_pattern_def_seq (vinfo, stmt_vinfo, nop_stmt);
1153 : }
1154 : }
1155 :
1156 29 : vect_pattern_detected ("vect_recog_cond_expr_convert_pattern", last_stmt);
1157 :
1158 29 : temp = vect_recog_temp_ssa_var (type, NULL);
1159 29 : cond_stmt = gimple_build_assign (temp, build3 (COND_EXPR, type, match[3],
1160 : op1, op2));
1161 29 : append_pattern_def_seq (vinfo, stmt_vinfo, cond_stmt);
1162 29 : new_lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
1163 29 : pattern_stmt = gimple_build_assign (new_lhs, code, temp);
1164 29 : *type_out = NULL_TREE;
1165 :
1166 29 : if (dump_enabled_p ())
1167 20 : dump_printf_loc (MSG_NOTE, vect_location,
1168 : "created pattern stmt: %G", pattern_stmt);
1169 : return pattern_stmt;
1170 : }
1171 :
1172 : /* Function vect_recog_dot_prod_pattern
1173 :
1174 : Try to find the following pattern:
1175 :
1176 : type1a x_t
1177 : type1b y_t;
1178 : TYPE1 prod;
1179 : TYPE2 sum = init;
1180 : loop:
1181 : sum_0 = phi <init, sum_1>
1182 : S1 x_t = ...
1183 : S2 y_t = ...
1184 : S3 x_T = (TYPE1) x_t;
1185 : S4 y_T = (TYPE1) y_t;
1186 : S5 prod = x_T * y_T;
1187 : [S6 prod = (TYPE2) prod; #optional]
1188 : S7 sum_1 = prod + sum_0;
1189 :
1190 : where 'TYPE1' is exactly double the size of type 'type1a' and 'type1b',
1191 : the sign of 'TYPE1' must be one of 'type1a' or 'type1b' but the sign of
1192 : 'type1a' and 'type1b' can differ.
1193 :
1194 : Input:
1195 :
1196 : * STMT_VINFO: The stmt from which the pattern search begins. In the
1197 : example, when this function is called with S7, the pattern {S3,S4,S5,S6,S7}
1198 : will be detected.
1199 :
1200 : Output:
1201 :
1202 : * TYPE_OUT: The type of the output of this pattern.
1203 :
1204 : * Return value: A new stmt that will be used to replace the sequence of
1205 : stmts that constitute the pattern. In this case it will be:
1206 : WIDEN_DOT_PRODUCT <x_t, y_t, sum_0>
1207 :
1208 : Note: The dot-prod idiom is a widening reduction pattern that is
1209 : vectorized without preserving all the intermediate results. It
1210 : produces only N/2 (widened) results (by summing up pairs of
1211 : intermediate results) rather than all N results. Therefore, we
1212 : cannot allow this pattern when we want to get all the results and in
1213 : the correct order (as is the case when this computation is in an
1214 : inner-loop nested in an outer-loop that us being vectorized). */
1215 :
1216 : static gimple *
1217 32151401 : vect_recog_dot_prod_pattern (vec_info *vinfo,
1218 : stmt_vec_info stmt_vinfo, tree *type_out)
1219 : {
1220 32151401 : tree oprnd0, oprnd1;
1221 32151401 : gimple *last_stmt = stmt_vinfo->stmt;
1222 32151401 : tree type, half_type;
1223 32151401 : gimple *pattern_stmt;
1224 32151401 : tree var;
1225 :
1226 : /* Look for the following pattern
1227 : DX = (TYPE1) X;
1228 : DY = (TYPE1) Y;
1229 : DPROD = DX * DY;
1230 : DDPROD = (TYPE2) DPROD;
1231 : sum_1 = DDPROD + sum_0;
1232 : In which
1233 : - DX is double the size of X
1234 : - DY is double the size of Y
1235 : - DX, DY, DPROD all have the same type but the sign
1236 : between X, Y and DPROD can differ.
1237 : - sum is the same size of DPROD or bigger
1238 : - sum has been recognized as a reduction variable.
1239 :
1240 : This is equivalent to:
1241 : DPROD = X w* Y; #widen mult
1242 : sum_1 = DPROD w+ sum_0; #widen summation
1243 : or
1244 : DPROD = X w* Y; #widen mult
1245 : sum_1 = DPROD + sum_0; #summation
1246 : */
1247 :
1248 : /* Starting from LAST_STMT, follow the defs of its uses in search
1249 : of the above pattern. */
1250 :
1251 32151401 : if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
1252 : &oprnd0, &oprnd1))
1253 : return NULL;
1254 :
1255 53703 : type = TREE_TYPE (gimple_get_lhs (last_stmt));
1256 :
1257 53703 : vect_unpromoted_value unprom_mult;
1258 53703 : oprnd0 = vect_look_through_possible_promotion (vinfo, oprnd0, &unprom_mult);
1259 :
1260 : /* So far so good. Since last_stmt was detected as a (summation) reduction,
1261 : we know that oprnd1 is the reduction variable (defined by a loop-header
1262 : phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
1263 : Left to check that oprnd0 is defined by a (widen_)mult_expr */
1264 53703 : if (!oprnd0)
1265 : return NULL;
1266 :
1267 36420 : stmt_vec_info mult_vinfo = vect_get_internal_def (vinfo, oprnd0);
1268 36420 : if (!mult_vinfo)
1269 : return NULL;
1270 :
1271 : /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
1272 : inside the loop (in case we are analyzing an outer-loop). */
1273 106725 : vect_unpromoted_value unprom0[2];
1274 35575 : enum optab_subtype subtype = optab_vector;
1275 35575 : if (!vect_widened_op_tree (vinfo, mult_vinfo, MULT_EXPR, WIDEN_MULT_EXPR,
1276 : false, 2, unprom0, &half_type, &subtype))
1277 : return NULL;
1278 :
1279 : /* If there are two widening operations, make sure they agree on the sign
1280 : of the extension. The result of an optab_vector_mixed_sign operation
1281 : is signed; otherwise, the result has the same sign as the operands. */
1282 1371 : if (TYPE_PRECISION (unprom_mult.type) != TYPE_PRECISION (type)
1283 2100 : && (subtype == optab_vector_mixed_sign
1284 729 : ? TYPE_UNSIGNED (unprom_mult.type)
1285 526 : : TYPE_SIGN (unprom_mult.type) != TYPE_SIGN (half_type)))
1286 : return NULL;
1287 :
1288 1290 : vect_pattern_detected ("vect_recog_dot_prod_pattern", last_stmt);
1289 :
1290 : /* If the inputs have mixed signs, canonicalize on using the signed
1291 : input type for analysis. This also helps when emulating mixed-sign
1292 : operations using signed operations. */
1293 1290 : if (subtype == optab_vector_mixed_sign)
1294 240 : half_type = signed_type_for (half_type);
1295 :
1296 1290 : tree half_vectype;
1297 1290 : if (!vect_supportable_conv_optab_p (vinfo, type, DOT_PROD_EXPR, half_type,
1298 : type_out, &half_vectype, subtype))
1299 : {
1300 : /* We can emulate a mixed-sign dot-product using a sequence of
1301 : signed dot-products; see vect_emulate_mixed_dot_prod for details. */
1302 585 : if (subtype != optab_vector_mixed_sign
1303 585 : || !vect_supportable_conv_optab_p (vinfo, signed_type_for (type),
1304 : DOT_PROD_EXPR, half_type,
1305 : type_out, &half_vectype,
1306 : optab_vector))
1307 570 : return NULL;
1308 :
1309 15 : *type_out = signed_or_unsigned_type_for (TYPE_UNSIGNED (type),
1310 : *type_out);
1311 : }
1312 :
1313 : /* Get the inputs in the appropriate types. */
1314 720 : tree mult_oprnd[2];
1315 720 : vect_convert_inputs (vinfo, stmt_vinfo, 2, mult_oprnd, half_type,
1316 : unprom0, half_vectype, subtype);
1317 :
1318 720 : var = vect_recog_temp_ssa_var (type, NULL);
1319 720 : pattern_stmt = gimple_build_assign (var, DOT_PROD_EXPR,
1320 : mult_oprnd[0], mult_oprnd[1], oprnd1);
1321 :
1322 720 : return pattern_stmt;
1323 : }
1324 :
1325 :
1326 : /* Function vect_recog_sad_pattern
1327 :
1328 : Try to find the following Sum of Absolute Difference (SAD) pattern:
1329 :
1330 : type x_t, y_t;
1331 : signed TYPE1 diff, abs_diff;
1332 : TYPE2 sum = init;
1333 : loop:
1334 : sum_0 = phi <init, sum_1>
1335 : S1 x_t = ...
1336 : S2 y_t = ...
1337 : S3 x_T = (TYPE1) x_t;
1338 : S4 y_T = (TYPE1) y_t;
1339 : S5 diff = x_T - y_T;
1340 : S6 abs_diff = ABS_EXPR <diff>;
1341 : [S7 abs_diff = (TYPE2) abs_diff; #optional]
1342 : S8 sum_1 = abs_diff + sum_0;
1343 :
1344 : where 'TYPE1' is at least double the size of type 'type', and 'TYPE2' is the
1345 : same size of 'TYPE1' or bigger. This is a special case of a reduction
1346 : computation.
1347 :
1348 : Input:
1349 :
1350 : * STMT_VINFO: The stmt from which the pattern search begins. In the
1351 : example, when this function is called with S8, the pattern
1352 : {S3,S4,S5,S6,S7,S8} will be detected.
1353 :
1354 : Output:
1355 :
1356 : * TYPE_OUT: The type of the output of this pattern.
1357 :
1358 : * Return value: A new stmt that will be used to replace the sequence of
1359 : stmts that constitute the pattern. In this case it will be:
1360 : SAD_EXPR <x_t, y_t, sum_0>
1361 : */
1362 :
1363 : static gimple *
1364 32150686 : vect_recog_sad_pattern (vec_info *vinfo,
1365 : stmt_vec_info stmt_vinfo, tree *type_out)
1366 : {
1367 32150686 : gimple *last_stmt = stmt_vinfo->stmt;
1368 32150686 : tree half_type;
1369 :
1370 : /* Look for the following pattern
1371 : DX = (TYPE1) X;
1372 : DY = (TYPE1) Y;
1373 : DDIFF = DX - DY;
1374 : DAD = ABS_EXPR <DDIFF>;
1375 : DDPROD = (TYPE2) DPROD;
1376 : sum_1 = DAD + sum_0;
1377 : In which
1378 : - DX is at least double the size of X
1379 : - DY is at least double the size of Y
1380 : - DX, DY, DDIFF, DAD all have the same type
1381 : - sum is the same size of DAD or bigger
1382 : - sum has been recognized as a reduction variable.
1383 :
1384 : This is equivalent to:
1385 : DDIFF = X w- Y; #widen sub
1386 : DAD = ABS_EXPR <DDIFF>;
1387 : sum_1 = DAD w+ sum_0; #widen summation
1388 : or
1389 : DDIFF = X w- Y; #widen sub
1390 : DAD = ABS_EXPR <DDIFF>;
1391 : sum_1 = DAD + sum_0; #summation
1392 : */
1393 :
1394 : /* Starting from LAST_STMT, follow the defs of its uses in search
1395 : of the above pattern. */
1396 :
1397 32150686 : tree plus_oprnd0, plus_oprnd1;
1398 32150686 : if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
1399 : &plus_oprnd0, &plus_oprnd1))
1400 : return NULL;
1401 :
1402 52983 : tree sum_type = TREE_TYPE (gimple_get_lhs (last_stmt));
1403 :
1404 : /* Any non-truncating sequence of conversions is OK here, since
1405 : with a successful match, the result of the ABS(U) is known to fit
1406 : within the nonnegative range of the result type. (It cannot be the
1407 : negative of the minimum signed value due to the range of the widening
1408 : MINUS_EXPR.) */
1409 52983 : vect_unpromoted_value unprom_abs;
1410 52983 : plus_oprnd0 = vect_look_through_possible_promotion (vinfo, plus_oprnd0,
1411 : &unprom_abs);
1412 :
1413 : /* So far so good. Since last_stmt was detected as a (summation) reduction,
1414 : we know that plus_oprnd1 is the reduction variable (defined by a loop-header
1415 : phi), and plus_oprnd0 is an ssa-name defined by a stmt in the loop body.
1416 : Then check that plus_oprnd0 is defined by an abs_expr. */
1417 :
1418 52983 : if (!plus_oprnd0)
1419 : return NULL;
1420 :
1421 35700 : stmt_vec_info abs_stmt_vinfo = vect_get_internal_def (vinfo, plus_oprnd0);
1422 35700 : if (!abs_stmt_vinfo)
1423 : return NULL;
1424 :
1425 : /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
1426 : inside the loop (in case we are analyzing an outer-loop). */
1427 34855 : gassign *abs_stmt = dyn_cast <gassign *> (abs_stmt_vinfo->stmt);
1428 104565 : vect_unpromoted_value unprom[2];
1429 :
1430 34855 : if (!abs_stmt)
1431 : {
1432 32150576 : gcall *abd_stmt = dyn_cast <gcall *> (abs_stmt_vinfo->stmt);
1433 302 : if (!abd_stmt
1434 302 : || !gimple_call_internal_p (abd_stmt)
1435 0 : || gimple_call_num_args (abd_stmt) != 2)
1436 : return NULL;
1437 :
1438 0 : tree abd_oprnd0 = gimple_call_arg (abd_stmt, 0);
1439 0 : tree abd_oprnd1 = gimple_call_arg (abd_stmt, 1);
1440 :
1441 0 : if (gimple_call_internal_fn (abd_stmt) == IFN_ABD
1442 0 : || gimple_call_internal_fn (abd_stmt) == IFN_VEC_WIDEN_ABD)
1443 : {
1444 0 : unprom[0].op = abd_oprnd0;
1445 0 : unprom[0].type = TREE_TYPE (abd_oprnd0);
1446 0 : unprom[1].op = abd_oprnd1;
1447 0 : unprom[1].type = TREE_TYPE (abd_oprnd1);
1448 : }
1449 : else
1450 : return NULL;
1451 :
1452 0 : half_type = unprom[0].type;
1453 : }
1454 34488 : else if (!vect_recog_absolute_difference (vinfo, abs_stmt, &half_type,
1455 : unprom, NULL))
1456 : return NULL;
1457 :
1458 806 : vect_pattern_detected ("vect_recog_sad_pattern", last_stmt);
1459 :
1460 806 : tree half_vectype;
1461 806 : if (!vect_supportable_direct_optab_p (vinfo, sum_type, SAD_EXPR, half_type,
1462 : type_out, &half_vectype))
1463 : return NULL;
1464 :
1465 : /* Get the inputs to the SAD_EXPR in the appropriate types. */
1466 412 : tree sad_oprnd[2];
1467 412 : vect_convert_inputs (vinfo, stmt_vinfo, 2, sad_oprnd, half_type,
1468 : unprom, half_vectype);
1469 :
1470 412 : tree var = vect_recog_temp_ssa_var (sum_type, NULL);
1471 412 : gimple *pattern_stmt = gimple_build_assign (var, SAD_EXPR, sad_oprnd[0],
1472 : sad_oprnd[1], plus_oprnd1);
1473 :
1474 412 : return pattern_stmt;
1475 : }
1476 :
1477 : /* Function vect_recog_abd_pattern
1478 :
1479 : Try to find the following ABsolute Difference (ABD) or
1480 : widening ABD (WIDEN_ABD) pattern:
1481 :
1482 : TYPE1 x;
1483 : TYPE2 y;
1484 : TYPE3 x_cast = (TYPE3) x; // widening or no-op
1485 : TYPE3 y_cast = (TYPE3) y; // widening or no-op
1486 : TYPE3 diff = x_cast - y_cast;
1487 : TYPE4 diff_cast = (TYPE4) diff; // widening or no-op
1488 : TYPE5 abs = ABS(U)_EXPR <diff_cast>;
1489 :
1490 : WIDEN_ABD exists to optimize the case where TYPE4 is at least
1491 : twice as wide as TYPE3.
1492 :
1493 : Input:
1494 :
1495 : * STMT_VINFO: The stmt from which the pattern search begins
1496 :
1497 : Output:
1498 :
1499 : * TYPE_OUT: The type of the output of this pattern
1500 :
1501 : * Return value: A new stmt that will be used to replace the sequence of
1502 : stmts that constitute the pattern, principally:
1503 : out = IFN_ABD (x, y)
1504 : out = IFN_WIDEN_ABD (x, y)
1505 : */
1506 :
1507 : static gimple *
1508 32099600 : vect_recog_abd_pattern (vec_info *vinfo,
1509 : stmt_vec_info stmt_vinfo, tree *type_out)
1510 : {
1511 54491862 : gassign *last_stmt = dyn_cast <gassign *> (STMT_VINFO_STMT (stmt_vinfo));
1512 22392262 : if (!last_stmt)
1513 : return NULL;
1514 :
1515 22392262 : tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
1516 :
1517 67176786 : vect_unpromoted_value unprom[2];
1518 22392262 : gassign *diff_stmt = NULL;
1519 22392262 : tree abd_in_type;
1520 22392262 : if (!vect_recog_absolute_difference (vinfo, last_stmt, &abd_in_type,
1521 : unprom, &diff_stmt))
1522 : {
1523 : /* We cannot try further without having a non-widening MINUS. */
1524 22390744 : if (!diff_stmt)
1525 : return NULL;
1526 :
1527 281 : unprom[0].op = gimple_assign_rhs1 (diff_stmt);
1528 281 : unprom[1].op = gimple_assign_rhs2 (diff_stmt);
1529 281 : abd_in_type = signed_type_for (out_type);
1530 : }
1531 :
1532 1799 : tree abd_out_type = abd_in_type;
1533 :
1534 1799 : tree vectype_in = get_vectype_for_scalar_type (vinfo, abd_in_type);
1535 1799 : if (!vectype_in)
1536 : return NULL;
1537 :
1538 1780 : internal_fn ifn = IFN_ABD;
1539 1780 : tree vectype_out = vectype_in;
1540 :
1541 1780 : if (TYPE_PRECISION (out_type) >= TYPE_PRECISION (abd_in_type) * 2
1542 1780 : && stmt_vinfo->min_output_precision >= TYPE_PRECISION (abd_in_type) * 2)
1543 : {
1544 1409 : tree mid_type
1545 1409 : = build_nonstandard_integer_type (TYPE_PRECISION (abd_in_type) * 2,
1546 1409 : TYPE_UNSIGNED (abd_in_type));
1547 1409 : tree mid_vectype = get_vectype_for_scalar_type (vinfo, mid_type);
1548 :
1549 1409 : code_helper dummy_code;
1550 1409 : int dummy_int;
1551 1409 : auto_vec<tree> dummy_vec;
1552 1409 : if (mid_vectype
1553 1409 : && supportable_widening_operation (IFN_VEC_WIDEN_ABD,
1554 : mid_vectype, vectype_in, false,
1555 : &dummy_code, &dummy_code,
1556 : &dummy_int, &dummy_vec))
1557 : {
1558 0 : ifn = IFN_VEC_WIDEN_ABD;
1559 0 : abd_out_type = mid_type;
1560 0 : vectype_out = mid_vectype;
1561 : }
1562 1409 : }
1563 :
1564 1409 : if (ifn == IFN_ABD
1565 1780 : && !direct_internal_fn_supported_p (ifn, vectype_in,
1566 : OPTIMIZE_FOR_SPEED))
1567 : return NULL;
1568 :
1569 0 : vect_pattern_detected ("vect_recog_abd_pattern", last_stmt);
1570 :
1571 0 : tree abd_oprnds[2];
1572 0 : vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds,
1573 : abd_in_type, unprom, vectype_in);
1574 :
1575 0 : *type_out = get_vectype_for_scalar_type (vinfo, out_type);
1576 :
1577 0 : tree abd_result = vect_recog_temp_ssa_var (abd_out_type, NULL);
1578 0 : gcall *abd_stmt = gimple_build_call_internal (ifn, 2,
1579 : abd_oprnds[0], abd_oprnds[1]);
1580 0 : gimple_call_set_lhs (abd_stmt, abd_result);
1581 0 : gimple_set_location (abd_stmt, gimple_location (last_stmt));
1582 :
1583 0 : gimple *stmt = abd_stmt;
1584 0 : if (TYPE_PRECISION (abd_in_type) == TYPE_PRECISION (abd_out_type)
1585 0 : && TYPE_PRECISION (abd_out_type) < TYPE_PRECISION (out_type)
1586 0 : && !TYPE_UNSIGNED (abd_out_type))
1587 : {
1588 0 : tree unsign = unsigned_type_for (abd_out_type);
1589 0 : stmt = vect_convert_output (vinfo, stmt_vinfo, unsign, stmt, vectype_out);
1590 0 : vectype_out = get_vectype_for_scalar_type (vinfo, unsign);
1591 : }
1592 :
1593 0 : return vect_convert_output (vinfo, stmt_vinfo, out_type, stmt, vectype_out);
1594 : }
1595 :
1596 : /* Recognize an operation that performs ORIG_CODE on widened inputs,
1597 : so that it can be treated as though it had the form:
1598 :
1599 : A_TYPE a;
1600 : B_TYPE b;
1601 : HALF_TYPE a_cast = (HALF_TYPE) a; // possible no-op
1602 : HALF_TYPE b_cast = (HALF_TYPE) b; // possible no-op
1603 : | RES_TYPE a_extend = (RES_TYPE) a_cast; // promotion from HALF_TYPE
1604 : | RES_TYPE b_extend = (RES_TYPE) b_cast; // promotion from HALF_TYPE
1605 : | RES_TYPE res = a_extend ORIG_CODE b_extend;
1606 :
1607 : Try to replace the pattern with:
1608 :
1609 : A_TYPE a;
1610 : B_TYPE b;
1611 : HALF_TYPE a_cast = (HALF_TYPE) a; // possible no-op
1612 : HALF_TYPE b_cast = (HALF_TYPE) b; // possible no-op
1613 : | EXT_TYPE ext = a_cast WIDE_CODE b_cast;
1614 : | RES_TYPE res = (EXT_TYPE) ext; // possible no-op
1615 :
1616 : where EXT_TYPE is wider than HALF_TYPE but has the same signedness.
1617 :
1618 : SHIFT_P is true if ORIG_CODE and WIDE_CODE are shifts. NAME is the
1619 : name of the pattern being matched, for dump purposes. */
1620 :
1621 : static gimple *
1622 129229405 : vect_recog_widen_op_pattern (vec_info *vinfo,
1623 : stmt_vec_info last_stmt_info, tree *type_out,
1624 : tree_code orig_code, code_helper wide_code,
1625 : bool shift_p, const char *name)
1626 : {
1627 129229405 : gimple *last_stmt = last_stmt_info->stmt;
1628 :
1629 387688215 : vect_unpromoted_value unprom[2];
1630 129229405 : tree half_type;
1631 129229405 : if (!vect_widened_op_tree (vinfo, last_stmt_info, orig_code, orig_code,
1632 : shift_p, 2, unprom, &half_type))
1633 :
1634 : return NULL;
1635 :
1636 : /* Pattern detected. */
1637 330019 : vect_pattern_detected (name, last_stmt);
1638 :
1639 330019 : tree type = TREE_TYPE (gimple_get_lhs (last_stmt));
1640 330019 : tree itype = type;
1641 330019 : if (TYPE_PRECISION (type) != TYPE_PRECISION (half_type) * 2
1642 330019 : || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (half_type))
1643 228365 : itype = build_nonstandard_integer_type (TYPE_PRECISION (half_type) * 2,
1644 228365 : TYPE_UNSIGNED (half_type));
1645 :
1646 : /* Check target support */
1647 330019 : tree vectype = get_vectype_for_scalar_type (vinfo, half_type);
1648 330019 : tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
1649 330019 : tree ctype = itype;
1650 330019 : tree vecctype = vecitype;
1651 330019 : if (orig_code == MINUS_EXPR
1652 6402 : && TYPE_UNSIGNED (itype)
1653 334515 : && TYPE_PRECISION (type) > TYPE_PRECISION (itype))
1654 : {
1655 : /* Subtraction is special, even if half_type is unsigned and no matter
1656 : whether type is signed or unsigned, if type is wider than itype,
1657 : we need to sign-extend from the widening operation result to the
1658 : result type.
1659 : Consider half_type unsigned char, operand 1 0xfe, operand 2 0xff,
1660 : itype unsigned short and type either int or unsigned int.
1661 : Widened (unsigned short) 0xfe - (unsigned short) 0xff is
1662 : (unsigned short) 0xffff, but for type int we want the result -1
1663 : and for type unsigned int 0xffffffff rather than 0xffff. */
1664 730 : ctype = build_nonstandard_integer_type (TYPE_PRECISION (itype), 0);
1665 730 : vecctype = get_vectype_for_scalar_type (vinfo, ctype);
1666 : }
1667 :
1668 330019 : code_helper dummy_code;
1669 330019 : int dummy_int;
1670 330019 : auto_vec<tree> dummy_vec;
1671 330019 : if (!vectype
1672 330019 : || !vecitype
1673 255787 : || !vecctype
1674 585806 : || !supportable_widening_operation (wide_code, vecitype, vectype, true,
1675 : &dummy_code, &dummy_code,
1676 : &dummy_int, &dummy_vec))
1677 217202 : return NULL;
1678 :
1679 112817 : *type_out = get_vectype_for_scalar_type (vinfo, type);
1680 112817 : if (!*type_out)
1681 : return NULL;
1682 :
1683 112817 : tree oprnd[2];
1684 112817 : vect_convert_inputs (vinfo, last_stmt_info,
1685 : 2, oprnd, half_type, unprom, vectype);
1686 :
1687 112817 : tree var = vect_recog_temp_ssa_var (itype, NULL);
1688 112817 : gimple *pattern_stmt = vect_gimple_build (var, wide_code, oprnd[0], oprnd[1]);
1689 :
1690 112817 : if (vecctype != vecitype)
1691 0 : pattern_stmt = vect_convert_output (vinfo, last_stmt_info, ctype,
1692 : pattern_stmt, vecitype);
1693 :
1694 112817 : return vect_convert_output (vinfo, last_stmt_info,
1695 112817 : type, pattern_stmt, vecctype);
1696 330019 : }
1697 :
1698 : /* Try to detect multiplication on widened inputs, converting MULT_EXPR
1699 : to WIDEN_MULT_EXPR. See vect_recog_widen_op_pattern for details. */
1700 :
1701 : static gimple *
1702 32179167 : vect_recog_widen_mult_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
1703 : tree *type_out)
1704 : {
1705 32179167 : return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
1706 32179167 : MULT_EXPR, WIDEN_MULT_EXPR, false,
1707 32179167 : "vect_recog_widen_mult_pattern");
1708 : }
1709 :
1710 : /* Try to detect addition on widened inputs, converting PLUS_EXPR
1711 : to IFN_VEC_WIDEN_PLUS. See vect_recog_widen_op_pattern for details. */
1712 :
1713 : static gimple *
1714 32449711 : vect_recog_widen_plus_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
1715 : tree *type_out)
1716 : {
1717 32449711 : return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
1718 32449711 : PLUS_EXPR, IFN_VEC_WIDEN_PLUS,
1719 32449711 : false, "vect_recog_widen_plus_pattern");
1720 : }
1721 :
1722 : /* Try to detect subtraction on widened inputs, converting MINUS_EXPR
1723 : to IFN_VEC_WIDEN_MINUS. See vect_recog_widen_op_pattern for details. */
1724 : static gimple *
1725 32449711 : vect_recog_widen_minus_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
1726 : tree *type_out)
1727 : {
1728 32449711 : return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
1729 32449711 : MINUS_EXPR, IFN_VEC_WIDEN_MINUS,
1730 32449711 : false, "vect_recog_widen_minus_pattern");
1731 : }
1732 :
1733 : /* Try to detect abd on widened inputs, converting IFN_ABD
1734 : to IFN_VEC_WIDEN_ABD. */
1735 : static gimple *
1736 32449711 : vect_recog_widen_abd_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
1737 : tree *type_out)
1738 : {
1739 32449711 : gassign *last_stmt = dyn_cast <gassign *> (STMT_VINFO_STMT (stmt_vinfo));
1740 30550102 : if (!last_stmt || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (last_stmt)))
1741 : return NULL;
1742 :
1743 3220374 : tree last_rhs = gimple_assign_rhs1 (last_stmt);
1744 :
1745 3220374 : tree in_type = TREE_TYPE (last_rhs);
1746 3220374 : tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
1747 3220374 : if (!INTEGRAL_TYPE_P (in_type)
1748 2810950 : || !INTEGRAL_TYPE_P (out_type)
1749 2694149 : || TYPE_PRECISION (in_type) * 2 != TYPE_PRECISION (out_type)
1750 3869889 : || !TYPE_UNSIGNED (in_type))
1751 : return NULL;
1752 :
1753 230921 : vect_unpromoted_value unprom;
1754 230921 : tree op = vect_look_through_possible_promotion (vinfo, last_rhs, &unprom);
1755 230921 : if (!op || TYPE_PRECISION (TREE_TYPE (op)) != TYPE_PRECISION (in_type))
1756 : return NULL;
1757 :
1758 228341 : stmt_vec_info abd_pattern_vinfo = vect_get_internal_def (vinfo, op);
1759 228341 : if (!abd_pattern_vinfo)
1760 : return NULL;
1761 :
1762 32458891 : gcall *abd_stmt = dyn_cast <gcall *> (STMT_VINFO_STMT (abd_pattern_vinfo));
1763 9180 : if (!abd_stmt
1764 9180 : || !gimple_call_internal_p (abd_stmt)
1765 265 : || gimple_call_internal_fn (abd_stmt) != IFN_ABD)
1766 : return NULL;
1767 :
1768 0 : tree vectype_in = get_vectype_for_scalar_type (vinfo, in_type);
1769 0 : tree vectype_out = get_vectype_for_scalar_type (vinfo, out_type);
1770 :
1771 0 : code_helper dummy_code;
1772 0 : int dummy_int;
1773 0 : auto_vec<tree> dummy_vec;
1774 0 : if (!supportable_widening_operation (IFN_VEC_WIDEN_ABD, vectype_out,
1775 : vectype_in, false,
1776 : &dummy_code, &dummy_code,
1777 : &dummy_int, &dummy_vec))
1778 : return NULL;
1779 :
1780 0 : vect_pattern_detected ("vect_recog_widen_abd_pattern", last_stmt);
1781 :
1782 0 : *type_out = vectype_out;
1783 :
1784 0 : tree abd_oprnd0 = gimple_call_arg (abd_stmt, 0);
1785 0 : tree abd_oprnd1 = gimple_call_arg (abd_stmt, 1);
1786 0 : tree widen_abd_result = vect_recog_temp_ssa_var (out_type, NULL);
1787 0 : gcall *widen_abd_stmt = gimple_build_call_internal (IFN_VEC_WIDEN_ABD, 2,
1788 : abd_oprnd0, abd_oprnd1);
1789 0 : gimple_call_set_lhs (widen_abd_stmt, widen_abd_result);
1790 0 : gimple_set_location (widen_abd_stmt, gimple_location (last_stmt));
1791 0 : return widen_abd_stmt;
1792 0 : }
1793 :
1794 : /* Function vect_recog_ctz_ffs_pattern
1795 :
1796 : Try to find the following pattern:
1797 :
1798 : TYPE1 A;
1799 : TYPE1 B;
1800 :
1801 : B = __builtin_ctz{,l,ll} (A);
1802 :
1803 : or
1804 :
1805 : B = __builtin_ffs{,l,ll} (A);
1806 :
1807 : Input:
1808 :
1809 : * STMT_VINFO: The stmt from which the pattern search begins.
1810 : here it starts with B = __builtin_* (A);
1811 :
1812 : Output:
1813 :
1814 : * TYPE_OUT: The vector type of the output of this pattern.
1815 :
1816 : * Return value: A new stmt that will be used to replace the sequence of
1817 : stmts that constitute the pattern, using clz or popcount builtins. */
1818 :
1819 : static gimple *
1820 32150623 : vect_recog_ctz_ffs_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
1821 : tree *type_out)
1822 : {
1823 32150623 : gimple *call_stmt = stmt_vinfo->stmt;
1824 32150623 : gimple *pattern_stmt;
1825 32150623 : tree rhs_oprnd, rhs_type, lhs_oprnd, lhs_type, vec_type, vec_rhs_type;
1826 32150623 : tree new_var;
1827 32150623 : internal_fn ifn = IFN_LAST, ifnnew = IFN_LAST;
1828 32150623 : bool defined_at_zero = true, defined_at_zero_new = false;
1829 32150623 : int val = 0, val_new = 0, val_cmp = 0;
1830 32150623 : int prec;
1831 32150623 : int sub = 0, add = 0;
1832 32150623 : location_t loc;
1833 :
1834 32150623 : if (!is_gimple_call (call_stmt))
1835 : return NULL;
1836 :
1837 3775457 : if (gimple_call_num_args (call_stmt) != 1
1838 3775457 : && gimple_call_num_args (call_stmt) != 2)
1839 : return NULL;
1840 :
1841 2141518 : rhs_oprnd = gimple_call_arg (call_stmt, 0);
1842 2141518 : rhs_type = TREE_TYPE (rhs_oprnd);
1843 2141518 : lhs_oprnd = gimple_call_lhs (call_stmt);
1844 2141518 : if (!lhs_oprnd)
1845 : return NULL;
1846 1042112 : lhs_type = TREE_TYPE (lhs_oprnd);
1847 1042112 : if (!INTEGRAL_TYPE_P (lhs_type)
1848 338072 : || !INTEGRAL_TYPE_P (rhs_type)
1849 47306 : || !type_has_mode_precision_p (rhs_type)
1850 1087831 : || TREE_CODE (rhs_oprnd) != SSA_NAME)
1851 1009626 : return NULL;
1852 :
1853 32486 : switch (gimple_call_combined_fn (call_stmt))
1854 : {
1855 1963 : CASE_CFN_CTZ:
1856 1963 : ifn = IFN_CTZ;
1857 1963 : if (!gimple_call_internal_p (call_stmt)
1858 1963 : || gimple_call_num_args (call_stmt) != 2)
1859 : defined_at_zero = false;
1860 : else
1861 121 : val = tree_to_shwi (gimple_call_arg (call_stmt, 1));
1862 : break;
1863 : CASE_CFN_FFS:
1864 : ifn = IFN_FFS;
1865 : break;
1866 : default:
1867 : return NULL;
1868 : }
1869 :
1870 2198 : prec = TYPE_PRECISION (rhs_type);
1871 2198 : loc = gimple_location (call_stmt);
1872 :
1873 2198 : vec_type = get_vectype_for_scalar_type (vinfo, lhs_type);
1874 2198 : if (!vec_type)
1875 : return NULL;
1876 :
1877 2192 : vec_rhs_type = get_vectype_for_scalar_type (vinfo, rhs_type);
1878 2192 : if (!vec_rhs_type)
1879 : return NULL;
1880 :
1881 : /* Do it only if the backend doesn't have ctz<vector_mode>2 or
1882 : ffs<vector_mode>2 pattern but does have clz<vector_mode>2 or
1883 : popcount<vector_mode>2. */
1884 1929 : if (!vec_type
1885 1929 : || direct_internal_fn_supported_p (ifn, vec_rhs_type,
1886 : OPTIMIZE_FOR_SPEED))
1887 : return NULL;
1888 :
1889 1929 : if (ifn == IFN_FFS
1890 1929 : && direct_internal_fn_supported_p (IFN_CTZ, vec_rhs_type,
1891 : OPTIMIZE_FOR_SPEED))
1892 : {
1893 0 : ifnnew = IFN_CTZ;
1894 0 : defined_at_zero_new
1895 0 : = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (rhs_type),
1896 : val_new) == 2;
1897 : }
1898 1929 : else if (direct_internal_fn_supported_p (IFN_CLZ, vec_rhs_type,
1899 : OPTIMIZE_FOR_SPEED))
1900 : {
1901 166 : ifnnew = IFN_CLZ;
1902 166 : defined_at_zero_new
1903 166 : = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (rhs_type),
1904 : val_new) == 2;
1905 : }
1906 166 : if ((ifnnew == IFN_LAST
1907 166 : || (defined_at_zero && !defined_at_zero_new))
1908 1763 : && direct_internal_fn_supported_p (IFN_POPCOUNT, vec_rhs_type,
1909 : OPTIMIZE_FOR_SPEED))
1910 : {
1911 : ifnnew = IFN_POPCOUNT;
1912 : defined_at_zero_new = true;
1913 : val_new = prec;
1914 : }
1915 1803 : if (ifnnew == IFN_LAST)
1916 : return NULL;
1917 :
1918 292 : vect_pattern_detected ("vec_recog_ctz_ffs_pattern", call_stmt);
1919 :
1920 292 : val_cmp = val_new;
1921 292 : if ((ifnnew == IFN_CLZ
1922 292 : && defined_at_zero
1923 106 : && defined_at_zero_new
1924 106 : && val == prec
1925 54 : && val_new == prec)
1926 238 : || (ifnnew == IFN_POPCOUNT && ifn == IFN_CTZ))
1927 : {
1928 137 : if (vect_is_reduction (stmt_vinfo))
1929 : return NULL;
1930 :
1931 : /* .CTZ (X) = PREC - .CLZ ((X - 1) & ~X)
1932 : .CTZ (X) = .POPCOUNT ((X - 1) & ~X). */
1933 137 : if (ifnnew == IFN_CLZ)
1934 54 : sub = prec;
1935 137 : val_cmp = prec;
1936 :
1937 137 : if (!TYPE_UNSIGNED (rhs_type))
1938 : {
1939 12 : rhs_type = unsigned_type_for (rhs_type);
1940 12 : vec_rhs_type = get_vectype_for_scalar_type (vinfo, rhs_type);
1941 12 : new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
1942 12 : pattern_stmt = gimple_build_assign (new_var, NOP_EXPR, rhs_oprnd);
1943 12 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
1944 : vec_rhs_type);
1945 12 : rhs_oprnd = new_var;
1946 : }
1947 :
1948 137 : tree m1 = vect_recog_temp_ssa_var (rhs_type, NULL);
1949 137 : pattern_stmt = gimple_build_assign (m1, PLUS_EXPR, rhs_oprnd,
1950 : build_int_cst (rhs_type, -1));
1951 137 : gimple_set_location (pattern_stmt, loc);
1952 137 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
1953 :
1954 137 : new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
1955 137 : pattern_stmt = gimple_build_assign (new_var, BIT_NOT_EXPR, rhs_oprnd);
1956 137 : gimple_set_location (pattern_stmt, loc);
1957 137 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
1958 137 : rhs_oprnd = new_var;
1959 :
1960 137 : new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
1961 137 : pattern_stmt = gimple_build_assign (new_var, BIT_AND_EXPR,
1962 : m1, rhs_oprnd);
1963 137 : gimple_set_location (pattern_stmt, loc);
1964 137 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
1965 137 : rhs_oprnd = new_var;
1966 137 : }
1967 155 : else if (ifnnew == IFN_CLZ)
1968 : {
1969 112 : if (vect_is_reduction (stmt_vinfo))
1970 : return NULL;
1971 :
1972 : /* .CTZ (X) = (PREC - 1) - .CLZ (X & -X)
1973 : .FFS (X) = PREC - .CLZ (X & -X). */
1974 106 : sub = prec - (ifn == IFN_CTZ);
1975 106 : val_cmp = sub - val_new;
1976 :
1977 106 : tree neg = vect_recog_temp_ssa_var (rhs_type, NULL);
1978 106 : pattern_stmt = gimple_build_assign (neg, NEGATE_EXPR, rhs_oprnd);
1979 106 : gimple_set_location (pattern_stmt, loc);
1980 106 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
1981 :
1982 106 : new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
1983 106 : pattern_stmt = gimple_build_assign (new_var, BIT_AND_EXPR,
1984 : rhs_oprnd, neg);
1985 106 : gimple_set_location (pattern_stmt, loc);
1986 106 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
1987 106 : rhs_oprnd = new_var;
1988 : }
1989 43 : else if (ifnnew == IFN_POPCOUNT)
1990 : {
1991 43 : if (vect_is_reduction (stmt_vinfo))
1992 : return NULL;
1993 :
1994 : /* .CTZ (X) = PREC - .POPCOUNT (X | -X)
1995 : .FFS (X) = (PREC + 1) - .POPCOUNT (X | -X). */
1996 43 : sub = prec + (ifn == IFN_FFS);
1997 43 : val_cmp = sub;
1998 :
1999 43 : tree neg = vect_recog_temp_ssa_var (rhs_type, NULL);
2000 43 : pattern_stmt = gimple_build_assign (neg, NEGATE_EXPR, rhs_oprnd);
2001 43 : gimple_set_location (pattern_stmt, loc);
2002 43 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
2003 :
2004 43 : new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
2005 43 : pattern_stmt = gimple_build_assign (new_var, BIT_IOR_EXPR,
2006 : rhs_oprnd, neg);
2007 43 : gimple_set_location (pattern_stmt, loc);
2008 43 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
2009 43 : rhs_oprnd = new_var;
2010 : }
2011 0 : else if (ifnnew == IFN_CTZ)
2012 : {
2013 : /* .FFS (X) = .CTZ (X) + 1. */
2014 0 : add = 1;
2015 0 : val_cmp++;
2016 :
2017 0 : if (vect_is_reduction (stmt_vinfo)
2018 0 : && defined_at_zero
2019 0 : && (!defined_at_zero_new || val != val_cmp))
2020 : return NULL;
2021 : }
2022 :
2023 : /* Create B = .IFNNEW (A). */
2024 286 : new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2025 286 : if ((ifnnew == IFN_CLZ || ifnnew == IFN_CTZ) && defined_at_zero_new)
2026 160 : pattern_stmt
2027 160 : = gimple_build_call_internal (ifnnew, 2, rhs_oprnd,
2028 : build_int_cst (integer_type_node,
2029 160 : val_new));
2030 : else
2031 126 : pattern_stmt = gimple_build_call_internal (ifnnew, 1, rhs_oprnd);
2032 286 : gimple_call_set_lhs (pattern_stmt, new_var);
2033 286 : gimple_set_location (pattern_stmt, loc);
2034 286 : *type_out = vec_type;
2035 :
2036 286 : if (sub)
2037 : {
2038 203 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
2039 203 : tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2040 203 : pattern_stmt = gimple_build_assign (ret_var, MINUS_EXPR,
2041 203 : build_int_cst (lhs_type, sub),
2042 : new_var);
2043 203 : gimple_set_location (pattern_stmt, loc);
2044 203 : new_var = ret_var;
2045 : }
2046 83 : else if (add)
2047 : {
2048 0 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
2049 0 : tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2050 0 : pattern_stmt = gimple_build_assign (ret_var, PLUS_EXPR, new_var,
2051 0 : build_int_cst (lhs_type, add));
2052 0 : gimple_set_location (pattern_stmt, loc);
2053 0 : new_var = ret_var;
2054 : }
2055 :
2056 286 : if (defined_at_zero
2057 210 : && (!defined_at_zero_new || val != val_cmp))
2058 : {
2059 43 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
2060 43 : tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2061 43 : rhs_oprnd = gimple_call_arg (call_stmt, 0);
2062 43 : rhs_type = TREE_TYPE (rhs_oprnd);
2063 43 : tree cmp = vect_recog_temp_ssa_var (boolean_type_node, NULL);
2064 43 : pattern_stmt = gimple_build_assign (cmp, NE_EXPR, rhs_oprnd,
2065 : build_zero_cst (rhs_type));
2066 43 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
2067 : truth_type_for (vec_type), rhs_type);
2068 43 : pattern_stmt = gimple_build_assign (ret_var, COND_EXPR, cmp,
2069 : new_var,
2070 43 : build_int_cst (lhs_type, val));
2071 : }
2072 :
2073 286 : if (dump_enabled_p ())
2074 36 : dump_printf_loc (MSG_NOTE, vect_location,
2075 : "created pattern stmt: %G", pattern_stmt);
2076 :
2077 : return pattern_stmt;
2078 : }
2079 :
2080 : /* Function vect_recog_popcount_clz_ctz_ffs_pattern
2081 :
2082 : Try to find the following pattern:
2083 :
2084 : UTYPE1 A;
2085 : TYPE1 B;
2086 : UTYPE2 temp_in;
2087 : TYPE3 temp_out;
2088 : temp_in = (UTYPE2)A;
2089 :
2090 : temp_out = __builtin_popcount{,l,ll} (temp_in);
2091 : B = (TYPE1) temp_out;
2092 :
2093 : TYPE2 may or may not be equal to TYPE3.
2094 : i.e. TYPE2 is equal to TYPE3 for __builtin_popcount
2095 : i.e. TYPE2 is not equal to TYPE3 for __builtin_popcountll
2096 :
2097 : Input:
2098 :
2099 : * STMT_VINFO: The stmt from which the pattern search begins.
2100 : here it starts with B = (TYPE1) temp_out;
2101 :
2102 : Output:
2103 :
2104 : * TYPE_OUT: The vector type of the output of this pattern.
2105 :
2106 : * Return value: A new stmt that will be used to replace the sequence of
2107 : stmts that constitute the pattern. In this case it will be:
2108 : B = .POPCOUNT (A);
2109 :
2110 : Similarly for clz, ctz and ffs.
2111 : */
2112 :
2113 : static gimple *
2114 32150257 : vect_recog_popcount_clz_ctz_ffs_pattern (vec_info *vinfo,
2115 : stmt_vec_info stmt_vinfo,
2116 : tree *type_out)
2117 : {
2118 32150257 : gassign *last_stmt = dyn_cast <gassign *> (stmt_vinfo->stmt);
2119 22442637 : gimple *call_stmt, *pattern_stmt;
2120 22442637 : tree rhs_oprnd, rhs_origin, lhs_oprnd, lhs_type, vec_type, new_var;
2121 54592576 : internal_fn ifn = IFN_LAST;
2122 32149939 : int addend = 0;
2123 :
2124 : /* Find B = (TYPE1) temp_out. */
2125 22442637 : if (!last_stmt)
2126 : return NULL;
2127 22442637 : tree_code code = gimple_assign_rhs_code (last_stmt);
2128 22442637 : if (!CONVERT_EXPR_CODE_P (code))
2129 : return NULL;
2130 :
2131 3072434 : lhs_oprnd = gimple_assign_lhs (last_stmt);
2132 3072434 : lhs_type = TREE_TYPE (lhs_oprnd);
2133 3072434 : if (!INTEGRAL_TYPE_P (lhs_type))
2134 : return NULL;
2135 :
2136 2793348 : rhs_oprnd = gimple_assign_rhs1 (last_stmt);
2137 2793348 : if (TREE_CODE (rhs_oprnd) != SSA_NAME
2138 2793348 : || !has_single_use (rhs_oprnd))
2139 : return NULL;
2140 1416867 : call_stmt = SSA_NAME_DEF_STMT (rhs_oprnd);
2141 :
2142 : /* Find temp_out = __builtin_popcount{,l,ll} (temp_in); */
2143 1416867 : if (!is_gimple_call (call_stmt))
2144 : return NULL;
2145 59267 : switch (gimple_call_combined_fn (call_stmt))
2146 : {
2147 : int val;
2148 : CASE_CFN_POPCOUNT:
2149 : ifn = IFN_POPCOUNT;
2150 : break;
2151 2183 : CASE_CFN_CLZ:
2152 2183 : ifn = IFN_CLZ;
2153 : /* Punt if call result is unsigned and defined value at zero
2154 : is negative, as the negative value doesn't extend correctly. */
2155 2183 : if (TYPE_UNSIGNED (TREE_TYPE (rhs_oprnd))
2156 0 : && gimple_call_internal_p (call_stmt)
2157 2183 : && CLZ_DEFINED_VALUE_AT_ZERO
2158 : (SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs_oprnd)), val) == 2
2159 2183 : && val < 0)
2160 : return NULL;
2161 : break;
2162 740 : CASE_CFN_CTZ:
2163 740 : ifn = IFN_CTZ;
2164 : /* Punt if call result is unsigned and defined value at zero
2165 : is negative, as the negative value doesn't extend correctly. */
2166 740 : if (TYPE_UNSIGNED (TREE_TYPE (rhs_oprnd))
2167 0 : && gimple_call_internal_p (call_stmt)
2168 740 : && CTZ_DEFINED_VALUE_AT_ZERO
2169 : (SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs_oprnd)), val) == 2
2170 740 : && val < 0)
2171 : return NULL;
2172 : break;
2173 57 : CASE_CFN_FFS:
2174 57 : ifn = IFN_FFS;
2175 57 : break;
2176 : default:
2177 : return NULL;
2178 : }
2179 :
2180 3282 : if (gimple_call_num_args (call_stmt) != 1
2181 3282 : && gimple_call_num_args (call_stmt) != 2)
2182 : return NULL;
2183 :
2184 3282 : rhs_oprnd = gimple_call_arg (call_stmt, 0);
2185 3282 : vect_unpromoted_value unprom_diff;
2186 3282 : rhs_origin
2187 3282 : = vect_look_through_possible_promotion (vinfo, rhs_oprnd, &unprom_diff);
2188 :
2189 3282 : if (!rhs_origin)
2190 : return NULL;
2191 :
2192 : /* Input and output of .POPCOUNT should be same-precision integer. */
2193 3272 : if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (lhs_type))
2194 : return NULL;
2195 :
2196 : /* Also A should be unsigned or same precision as temp_in, otherwise
2197 : different builtins/internal functions have different behaviors. */
2198 1685 : if (TYPE_PRECISION (unprom_diff.type)
2199 1685 : != TYPE_PRECISION (TREE_TYPE (rhs_oprnd)))
2200 264 : switch (ifn)
2201 : {
2202 95 : case IFN_POPCOUNT:
2203 : /* For popcount require zero extension, which doesn't add any
2204 : further bits to the count. */
2205 95 : if (!TYPE_UNSIGNED (unprom_diff.type))
2206 : return NULL;
2207 : break;
2208 109 : case IFN_CLZ:
2209 : /* clzll (x) == clz (x) + 32 for unsigned x != 0, so ok
2210 : if it is undefined at zero or if it matches also for the
2211 : defined value there. */
2212 109 : if (!TYPE_UNSIGNED (unprom_diff.type))
2213 : return NULL;
2214 109 : if (!type_has_mode_precision_p (lhs_type)
2215 109 : || !type_has_mode_precision_p (TREE_TYPE (rhs_oprnd)))
2216 0 : return NULL;
2217 109 : addend = (TYPE_PRECISION (TREE_TYPE (rhs_oprnd))
2218 109 : - TYPE_PRECISION (lhs_type));
2219 109 : if (gimple_call_internal_p (call_stmt)
2220 109 : && gimple_call_num_args (call_stmt) == 2)
2221 : {
2222 0 : int val1, val2;
2223 0 : val1 = tree_to_shwi (gimple_call_arg (call_stmt, 1));
2224 0 : int d2
2225 0 : = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (lhs_type),
2226 : val2);
2227 0 : if (d2 != 2 || val1 != val2 + addend)
2228 : return NULL;
2229 : }
2230 : break;
2231 40 : case IFN_CTZ:
2232 : /* ctzll (x) == ctz (x) for unsigned or signed x != 0, so ok
2233 : if it is undefined at zero or if it matches also for the
2234 : defined value there. */
2235 40 : if (gimple_call_internal_p (call_stmt)
2236 40 : && gimple_call_num_args (call_stmt) == 2)
2237 : {
2238 0 : int val1, val2;
2239 0 : val1 = tree_to_shwi (gimple_call_arg (call_stmt, 1));
2240 0 : int d2
2241 0 : = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (lhs_type),
2242 : val2);
2243 0 : if (d2 != 2 || val1 != val2)
2244 : return NULL;
2245 : }
2246 : break;
2247 : case IFN_FFS:
2248 : /* ffsll (x) == ffs (x) for unsigned or signed x. */
2249 : break;
2250 0 : default:
2251 0 : gcc_unreachable ();
2252 : }
2253 :
2254 1685 : vec_type = get_vectype_for_scalar_type (vinfo, lhs_type);
2255 : /* Do it only if the backend has popcount<vector_mode>2 etc. pattern. */
2256 1685 : if (!vec_type)
2257 : return NULL;
2258 :
2259 1556 : bool supported
2260 1556 : = direct_internal_fn_supported_p (ifn, vec_type, OPTIMIZE_FOR_SPEED);
2261 : /* We cannot rely on the reduction check in vect_recog_ctz_ffs_pattern
2262 : because we do not set up the temporary pattern stmt finally for
2263 : the recursive analysis. */
2264 1556 : if (!supported && vect_is_reduction (stmt_vinfo))
2265 : return NULL;
2266 1540 : if (!supported)
2267 1363 : switch (ifn)
2268 : {
2269 : case IFN_POPCOUNT:
2270 : case IFN_CLZ:
2271 : return NULL;
2272 57 : case IFN_FFS:
2273 : /* vect_recog_ctz_ffs_pattern can implement ffs using ctz. */
2274 57 : if (direct_internal_fn_supported_p (IFN_CTZ, vec_type,
2275 : OPTIMIZE_FOR_SPEED))
2276 : break;
2277 : /* FALLTHRU */
2278 531 : case IFN_CTZ:
2279 : /* vect_recog_ctz_ffs_pattern can implement ffs or ctz using
2280 : clz or popcount. */
2281 531 : if (direct_internal_fn_supported_p (IFN_CLZ, vec_type,
2282 : OPTIMIZE_FOR_SPEED))
2283 : break;
2284 471 : if (direct_internal_fn_supported_p (IFN_POPCOUNT, vec_type,
2285 : OPTIMIZE_FOR_SPEED))
2286 : break;
2287 : return NULL;
2288 0 : default:
2289 0 : gcc_unreachable ();
2290 : }
2291 :
2292 318 : vect_pattern_detected ("vec_recog_popcount_clz_ctz_ffs_pattern",
2293 : call_stmt);
2294 :
2295 : /* Create B = .POPCOUNT (A). */
2296 318 : new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2297 318 : tree arg2 = NULL_TREE;
2298 318 : int val;
2299 318 : if (ifn == IFN_CLZ
2300 368 : && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (lhs_type),
2301 : val) == 2)
2302 48 : arg2 = build_int_cst (integer_type_node, val);
2303 270 : else if (ifn == IFN_CTZ
2304 363 : && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (lhs_type),
2305 : val) == 2)
2306 93 : arg2 = build_int_cst (integer_type_node, val);
2307 318 : if (arg2)
2308 141 : pattern_stmt = gimple_build_call_internal (ifn, 2, unprom_diff.op, arg2);
2309 : else
2310 177 : pattern_stmt = gimple_build_call_internal (ifn, 1, unprom_diff.op);
2311 318 : gimple_call_set_lhs (pattern_stmt, new_var);
2312 318 : gimple_set_location (pattern_stmt, gimple_location (last_stmt));
2313 318 : *type_out = vec_type;
2314 :
2315 318 : if (dump_enabled_p ())
2316 24 : dump_printf_loc (MSG_NOTE, vect_location,
2317 : "created pattern stmt: %G", pattern_stmt);
2318 :
2319 318 : if (addend)
2320 : {
2321 12 : gcc_assert (supported);
2322 12 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
2323 12 : tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
2324 12 : pattern_stmt = gimple_build_assign (ret_var, PLUS_EXPR, new_var,
2325 12 : build_int_cst (lhs_type, addend));
2326 : }
2327 306 : else if (!supported)
2328 : {
2329 141 : stmt_vec_info new_stmt_info = vinfo->add_stmt (pattern_stmt);
2330 141 : STMT_VINFO_VECTYPE (new_stmt_info) = vec_type;
2331 141 : pattern_stmt
2332 141 : = vect_recog_ctz_ffs_pattern (vinfo, new_stmt_info, type_out);
2333 141 : if (pattern_stmt == NULL)
2334 : return NULL;
2335 141 : if (gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (new_stmt_info))
2336 : {
2337 141 : gimple_seq *pseq = &STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo);
2338 141 : gimple_seq_add_seq_without_update (pseq, seq);
2339 : }
2340 : }
2341 : return pattern_stmt;
2342 : }
2343 :
2344 : /* Function vect_recog_pow_pattern
2345 :
2346 : Try to find the following pattern:
2347 :
2348 : x = POW (y, N);
2349 :
2350 : with POW being one of pow, powf, powi, powif and N being
2351 : either 2 or 0.5.
2352 :
2353 : Input:
2354 :
2355 : * STMT_VINFO: The stmt from which the pattern search begins.
2356 :
2357 : Output:
2358 :
2359 : * TYPE_OUT: The type of the output of this pattern.
2360 :
2361 : * Return value: A new stmt that will be used to replace the sequence of
2362 : stmts that constitute the pattern. In this case it will be:
2363 : x = x * x
2364 : or
2365 : x = sqrt (x)
2366 : */
2367 :
2368 : static gimple *
2369 32150274 : vect_recog_pow_pattern (vec_info *vinfo,
2370 : stmt_vec_info stmt_vinfo, tree *type_out)
2371 : {
2372 32150274 : gimple *last_stmt = stmt_vinfo->stmt;
2373 32150274 : tree base, exp;
2374 32150274 : gimple *stmt;
2375 32150274 : tree var;
2376 :
2377 32150274 : if (!is_gimple_call (last_stmt) || gimple_call_lhs (last_stmt) == NULL)
2378 : return NULL;
2379 :
2380 1525251 : switch (gimple_call_combined_fn (last_stmt))
2381 : {
2382 516 : CASE_CFN_POW:
2383 516 : CASE_CFN_POWI:
2384 516 : break;
2385 :
2386 : default:
2387 : return NULL;
2388 : }
2389 :
2390 516 : base = gimple_call_arg (last_stmt, 0);
2391 516 : exp = gimple_call_arg (last_stmt, 1);
2392 516 : if (TREE_CODE (exp) != REAL_CST
2393 461 : && TREE_CODE (exp) != INTEGER_CST)
2394 : {
2395 461 : if (flag_unsafe_math_optimizations
2396 37 : && TREE_CODE (base) == REAL_CST
2397 464 : && gimple_call_builtin_p (last_stmt, BUILT_IN_NORMAL))
2398 : {
2399 3 : combined_fn log_cfn;
2400 3 : built_in_function exp_bfn;
2401 3 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (last_stmt)))
2402 : {
2403 : case BUILT_IN_POW:
2404 : log_cfn = CFN_BUILT_IN_LOG;
2405 : exp_bfn = BUILT_IN_EXP;
2406 : break;
2407 0 : case BUILT_IN_POWF:
2408 0 : log_cfn = CFN_BUILT_IN_LOGF;
2409 0 : exp_bfn = BUILT_IN_EXPF;
2410 0 : break;
2411 0 : case BUILT_IN_POWL:
2412 0 : log_cfn = CFN_BUILT_IN_LOGL;
2413 0 : exp_bfn = BUILT_IN_EXPL;
2414 0 : break;
2415 : default:
2416 : return NULL;
2417 : }
2418 3 : tree logc = fold_const_call (log_cfn, TREE_TYPE (base), base);
2419 3 : tree exp_decl = builtin_decl_implicit (exp_bfn);
2420 : /* Optimize pow (C, x) as exp (log (C) * x). Normally match.pd
2421 : does that, but if C is a power of 2, we want to use
2422 : exp2 (log2 (C) * x) in the non-vectorized version, but for
2423 : vectorization we don't have vectorized exp2. */
2424 3 : if (logc
2425 3 : && TREE_CODE (logc) == REAL_CST
2426 3 : && exp_decl
2427 6 : && lookup_attribute ("omp declare simd",
2428 3 : DECL_ATTRIBUTES (exp_decl)))
2429 : {
2430 3 : cgraph_node *node = cgraph_node::get_create (exp_decl);
2431 3 : if (node->simd_clones == NULL)
2432 : {
2433 2 : if (targetm.simd_clone.compute_vecsize_and_simdlen == NULL
2434 2 : || node->definition)
2435 : return NULL;
2436 2 : expand_simd_clones (node);
2437 2 : if (node->simd_clones == NULL)
2438 : return NULL;
2439 : }
2440 3 : *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (base));
2441 3 : if (!*type_out)
2442 : return NULL;
2443 3 : tree def = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
2444 3 : gimple *g = gimple_build_assign (def, MULT_EXPR, exp, logc);
2445 3 : append_pattern_def_seq (vinfo, stmt_vinfo, g);
2446 3 : tree res = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
2447 3 : g = gimple_build_call (exp_decl, 1, def);
2448 3 : gimple_call_set_lhs (g, res);
2449 3 : return g;
2450 : }
2451 : }
2452 :
2453 458 : return NULL;
2454 : }
2455 :
2456 : /* We now have a pow or powi builtin function call with a constant
2457 : exponent. */
2458 :
2459 : /* Catch squaring. */
2460 55 : if ((tree_fits_shwi_p (exp)
2461 0 : && tree_to_shwi (exp) == 2)
2462 55 : || (TREE_CODE (exp) == REAL_CST
2463 55 : && real_equal (&TREE_REAL_CST (exp), &dconst2)))
2464 : {
2465 11 : if (!vect_supportable_direct_optab_p (vinfo, TREE_TYPE (base), MULT_EXPR,
2466 11 : TREE_TYPE (base), type_out))
2467 : return NULL;
2468 :
2469 9 : var = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
2470 9 : stmt = gimple_build_assign (var, MULT_EXPR, base, base);
2471 9 : return stmt;
2472 : }
2473 :
2474 : /* Catch square root. */
2475 44 : if (TREE_CODE (exp) == REAL_CST
2476 44 : && real_equal (&TREE_REAL_CST (exp), &dconsthalf))
2477 : {
2478 10 : *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (base));
2479 10 : if (*type_out
2480 10 : && direct_internal_fn_supported_p (IFN_SQRT, *type_out,
2481 : OPTIMIZE_FOR_SPEED))
2482 : {
2483 8 : gcall *stmt = gimple_build_call_internal (IFN_SQRT, 1, base);
2484 8 : var = vect_recog_temp_ssa_var (TREE_TYPE (base), stmt);
2485 8 : gimple_call_set_lhs (stmt, var);
2486 8 : gimple_call_set_nothrow (stmt, true);
2487 8 : return stmt;
2488 : }
2489 : }
2490 :
2491 : return NULL;
2492 : }
2493 :
2494 :
2495 : /* Function vect_recog_widen_sum_pattern
2496 :
2497 : Try to find the following pattern:
2498 :
2499 : type x_t;
2500 : TYPE x_T, sum = init;
2501 : loop:
2502 : sum_0 = phi <init, sum_1>
2503 : S1 x_t = *p;
2504 : S2 x_T = (TYPE) x_t;
2505 : S3 sum_1 = x_T + sum_0;
2506 :
2507 : where type 'TYPE' is at least double the size of type 'type', i.e - we're
2508 : summing elements of type 'type' into an accumulator of type 'TYPE'. This is
2509 : a special case of a reduction computation.
2510 :
2511 : Input:
2512 :
2513 : * STMT_VINFO: The stmt from which the pattern search begins. In the example,
2514 : when this function is called with S3, the pattern {S2,S3} will be detected.
2515 :
2516 : Output:
2517 :
2518 : * TYPE_OUT: The type of the output of this pattern.
2519 :
2520 : * Return value: A new stmt that will be used to replace the sequence of
2521 : stmts that constitute the pattern. In this case it will be:
2522 : WIDEN_SUM <x_t, sum_0>
2523 :
2524 : Note: The widening-sum idiom is a widening reduction pattern that is
2525 : vectorized without preserving all the intermediate results. It
2526 : produces only N/2 (widened) results (by summing up pairs of
2527 : intermediate results) rather than all N results. Therefore, we
2528 : cannot allow this pattern when we want to get all the results and in
2529 : the correct order (as is the case when this computation is in an
2530 : inner-loop nested in an outer-loop that us being vectorized). */
2531 :
2532 : static gimple *
2533 32150274 : vect_recog_widen_sum_pattern (vec_info *vinfo,
2534 : stmt_vec_info stmt_vinfo, tree *type_out)
2535 : {
2536 32150274 : gimple *last_stmt = stmt_vinfo->stmt;
2537 32150274 : tree oprnd0, oprnd1;
2538 32150274 : tree type;
2539 32150274 : gimple *pattern_stmt;
2540 32150274 : tree var;
2541 :
2542 : /* Look for the following pattern
2543 : DX = (TYPE) X;
2544 : sum_1 = DX + sum_0;
2545 : In which DX is at least double the size of X, and sum_1 has been
2546 : recognized as a reduction variable.
2547 : */
2548 :
2549 : /* Starting from LAST_STMT, follow the defs of its uses in search
2550 : of the above pattern. */
2551 :
2552 32150274 : if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
2553 : &oprnd0, &oprnd1)
2554 52571 : || TREE_CODE (oprnd0) != SSA_NAME
2555 32202576 : || !vinfo->lookup_def (oprnd0))
2556 32098037 : return NULL;
2557 :
2558 52237 : type = TREE_TYPE (gimple_get_lhs (last_stmt));
2559 :
2560 : /* So far so good. Since last_stmt was detected as a (summation) reduction,
2561 : we know that oprnd1 is the reduction variable (defined by a loop-header
2562 : phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
2563 : Left to check that oprnd0 is defined by a cast from type 'type' to type
2564 : 'TYPE'. */
2565 :
2566 52237 : vect_unpromoted_value unprom0;
2567 52237 : if (!vect_look_through_possible_promotion (vinfo, oprnd0, &unprom0)
2568 52237 : || TYPE_PRECISION (unprom0.type) * 2 > TYPE_PRECISION (type))
2569 : return NULL;
2570 :
2571 2310 : vect_pattern_detected ("vect_recog_widen_sum_pattern", last_stmt);
2572 :
2573 2310 : if (!vect_supportable_conv_optab_p (vinfo, type, WIDEN_SUM_EXPR,
2574 : unprom0.type, type_out))
2575 : return NULL;
2576 :
2577 0 : var = vect_recog_temp_ssa_var (type, NULL);
2578 0 : pattern_stmt = gimple_build_assign (var, WIDEN_SUM_EXPR, unprom0.op, oprnd1);
2579 :
2580 0 : return pattern_stmt;
2581 : }
2582 :
2583 : /* Function vect_recog_bitfield_ref_pattern
2584 :
2585 : Try to find the following pattern:
2586 :
2587 : bf_value = BIT_FIELD_REF (container, bitsize, bitpos);
2588 : result = (type_out) bf_value;
2589 :
2590 : or
2591 :
2592 : if (BIT_FIELD_REF (container, bitsize, bitpos) `cmp` <constant>)
2593 :
2594 : where type_out is a non-bitfield type, that is to say, it's precision matches
2595 : 2^(TYPE_SIZE(type_out) - (TYPE_UNSIGNED (type_out) ? 1 : 2)).
2596 :
2597 : Input:
2598 :
2599 : * STMT_VINFO: The stmt from which the pattern search begins.
2600 : here it starts with:
2601 : result = (type_out) bf_value;
2602 :
2603 : or
2604 :
2605 : if (BIT_FIELD_REF (container, bitsize, bitpos) `cmp` <constant>)
2606 :
2607 : Output:
2608 :
2609 : * TYPE_OUT: The vector type of the output of this pattern.
2610 :
2611 : * Return value: A new stmt that will be used to replace the sequence of
2612 : stmts that constitute the pattern. If the precision of type_out is bigger
2613 : than the precision type of _1 we perform the widening before the shifting,
2614 : since the new precision will be large enough to shift the value and moving
2615 : widening operations up the statement chain enables the generation of
2616 : widening loads. If we are widening and the operation after the pattern is
2617 : an addition then we mask first and shift later, to enable the generation of
2618 : shifting adds. In the case of narrowing we will always mask first, shift
2619 : last and then perform a narrowing operation. This will enable the
2620 : generation of narrowing shifts.
2621 :
2622 : Widening with mask first, shift later:
2623 : container = (type_out) container;
2624 : masked = container & (((1 << bitsize) - 1) << bitpos);
2625 : result = masked >> bitpos;
2626 :
2627 : Widening with shift first, mask last:
2628 : container = (type_out) container;
2629 : shifted = container >> bitpos;
2630 : result = shifted & ((1 << bitsize) - 1);
2631 :
2632 : Narrowing:
2633 : masked = container & (((1 << bitsize) - 1) << bitpos);
2634 : result = masked >> bitpos;
2635 : result = (type_out) result;
2636 :
2637 : If the bitfield is signed and it's wider than type_out, we need to
2638 : keep the result sign-extended:
2639 : container = (type) container;
2640 : masked = container << (prec - bitsize - bitpos);
2641 : result = (type_out) (masked >> (prec - bitsize));
2642 :
2643 : Here type is the signed variant of the wider of type_out and the type
2644 : of container.
2645 :
2646 : The shifting is always optional depending on whether bitpos != 0.
2647 :
2648 : When the original bitfield was inside a gcond then an new gcond is also
2649 : generated with the newly `result` as the operand to the comparison.
2650 :
2651 : */
2652 :
2653 : static gimple *
2654 32096132 : vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info,
2655 : tree *type_out)
2656 : {
2657 32096132 : gimple *bf_stmt = NULL;
2658 32096132 : tree lhs = NULL_TREE;
2659 32096132 : tree ret_type = NULL_TREE;
2660 32096132 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
2661 32096132 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
2662 : {
2663 5345397 : tree op = gimple_cond_lhs (cond_stmt);
2664 5345397 : if (TREE_CODE (op) != SSA_NAME)
2665 : return NULL;
2666 5345097 : bf_stmt = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
2667 5345097 : if (TREE_CODE (gimple_cond_rhs (cond_stmt)) != INTEGER_CST)
2668 : return NULL;
2669 : }
2670 26750735 : else if (is_gimple_assign (stmt)
2671 22388179 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
2672 29744333 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2673 : {
2674 2952081 : gimple *second_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
2675 2952081 : bf_stmt = dyn_cast <gassign *> (second_stmt);
2676 2952081 : lhs = gimple_assign_lhs (stmt);
2677 2952081 : ret_type = TREE_TYPE (lhs);
2678 : }
2679 :
2680 6414837 : if (!bf_stmt
2681 6414837 : || gimple_assign_rhs_code (bf_stmt) != BIT_FIELD_REF)
2682 : return NULL;
2683 :
2684 17014 : tree bf_ref = gimple_assign_rhs1 (bf_stmt);
2685 17014 : tree container = TREE_OPERAND (bf_ref, 0);
2686 17014 : ret_type = ret_type ? ret_type : TREE_TYPE (container);
2687 :
2688 17014 : if (!bit_field_offset (bf_ref).is_constant ()
2689 17014 : || !bit_field_size (bf_ref).is_constant ()
2690 17014 : || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (container))))
2691 : return NULL;
2692 :
2693 33650 : if (!INTEGRAL_TYPE_P (TREE_TYPE (bf_ref))
2694 17012 : || !INTEGRAL_TYPE_P (TREE_TYPE (container))
2695 19183 : || TYPE_MODE (TREE_TYPE (container)) == E_BLKmode)
2696 14845 : return NULL;
2697 :
2698 2169 : gimple *use_stmt, *pattern_stmt;
2699 2169 : use_operand_p use_p;
2700 2169 : bool shift_first = true;
2701 2169 : tree container_type = TREE_TYPE (container);
2702 2169 : tree vectype = get_vectype_for_scalar_type (vinfo, container_type);
2703 :
2704 : /* Calculate shift_n before the adjustments for widening loads, otherwise
2705 : the container may change and we have to consider offset change for
2706 : widening loads on big endianness. The shift_n calculated here can be
2707 : independent of widening. */
2708 2169 : unsigned HOST_WIDE_INT shift_n = bit_field_offset (bf_ref).to_constant ();
2709 2169 : unsigned HOST_WIDE_INT mask_width = bit_field_size (bf_ref).to_constant ();
2710 2169 : unsigned HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (container_type));
2711 2169 : if (BYTES_BIG_ENDIAN)
2712 : shift_n = prec - shift_n - mask_width;
2713 :
2714 2169 : bool ref_sext = (!TYPE_UNSIGNED (TREE_TYPE (bf_ref)) &&
2715 1394 : TYPE_PRECISION (ret_type) > mask_width);
2716 2169 : bool load_widen = (TYPE_PRECISION (TREE_TYPE (container)) <
2717 2169 : TYPE_PRECISION (ret_type));
2718 :
2719 : /* We move the conversion earlier if the loaded type is smaller than the
2720 : return type to enable the use of widening loads. And if we need a
2721 : sign extension, we need to convert the loaded value early to a signed
2722 : type as well. */
2723 2169 : if (ref_sext || load_widen)
2724 : {
2725 941 : tree type = load_widen ? ret_type : container_type;
2726 941 : if (ref_sext)
2727 902 : type = gimple_signed_type (type);
2728 941 : pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type),
2729 : NOP_EXPR, container);
2730 941 : container = gimple_get_lhs (pattern_stmt);
2731 941 : container_type = TREE_TYPE (container);
2732 941 : prec = tree_to_uhwi (TYPE_SIZE (container_type));
2733 941 : vectype = get_vectype_for_scalar_type (vinfo, container_type);
2734 941 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
2735 : }
2736 1228 : else if (!useless_type_conversion_p (TREE_TYPE (container), ret_type))
2737 : /* If we are doing the conversion last then also delay the shift as we may
2738 : be able to combine the shift and conversion in certain cases. */
2739 : shift_first = false;
2740 :
2741 : /* If the only use of the result of this BIT_FIELD_REF + CONVERT is a
2742 : PLUS_EXPR then do the shift last as some targets can combine the shift and
2743 : add into a single instruction. */
2744 1416 : if (lhs && !is_pattern_stmt_p (stmt_info)
2745 3585 : && single_imm_use (lhs, &use_p, &use_stmt))
2746 : {
2747 1049 : if (gimple_code (use_stmt) == GIMPLE_ASSIGN
2748 1049 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR)
2749 : shift_first = false;
2750 : }
2751 :
2752 : /* If we don't have to shift we only generate the mask, so just fix the
2753 : code-path to shift_first. */
2754 2169 : if (shift_n == 0)
2755 762 : shift_first = true;
2756 :
2757 2169 : tree result;
2758 2169 : if (shift_first && !ref_sext)
2759 : {
2760 515 : tree shifted = container;
2761 515 : if (shift_n)
2762 : {
2763 65 : pattern_stmt
2764 65 : = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
2765 : RSHIFT_EXPR, container,
2766 65 : build_int_cst (sizetype, shift_n));
2767 65 : shifted = gimple_assign_lhs (pattern_stmt);
2768 65 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
2769 : }
2770 :
2771 515 : tree mask = wide_int_to_tree (container_type,
2772 515 : wi::mask (mask_width, false, prec));
2773 :
2774 515 : pattern_stmt
2775 515 : = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
2776 : BIT_AND_EXPR, shifted, mask);
2777 515 : result = gimple_assign_lhs (pattern_stmt);
2778 : }
2779 : else
2780 : {
2781 1654 : tree temp = vect_recog_temp_ssa_var (container_type);
2782 1654 : if (!ref_sext)
2783 : {
2784 752 : tree mask = wide_int_to_tree (container_type,
2785 752 : wi::shifted_mask (shift_n,
2786 : mask_width,
2787 : false, prec));
2788 752 : pattern_stmt = gimple_build_assign (temp, BIT_AND_EXPR,
2789 : container, mask);
2790 : }
2791 : else
2792 : {
2793 902 : HOST_WIDE_INT shl = prec - shift_n - mask_width;
2794 902 : shift_n += shl;
2795 902 : pattern_stmt = gimple_build_assign (temp, LSHIFT_EXPR,
2796 : container,
2797 : build_int_cst (sizetype,
2798 902 : shl));
2799 : }
2800 :
2801 1654 : tree masked = gimple_assign_lhs (pattern_stmt);
2802 1654 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
2803 1654 : pattern_stmt
2804 1654 : = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
2805 : RSHIFT_EXPR, masked,
2806 1654 : build_int_cst (sizetype, shift_n));
2807 1654 : result = gimple_assign_lhs (pattern_stmt);
2808 : }
2809 :
2810 2169 : if (!useless_type_conversion_p (TREE_TYPE (result), ret_type))
2811 : {
2812 1438 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
2813 1438 : pattern_stmt
2814 1438 : = gimple_build_assign (vect_recog_temp_ssa_var (ret_type),
2815 : NOP_EXPR, result);
2816 : }
2817 :
2818 2169 : if (!lhs)
2819 : {
2820 753 : if (!vectype)
2821 : return NULL;
2822 :
2823 615 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
2824 615 : vectype = truth_type_for (vectype);
2825 :
2826 : /* FIXME: This part extracts the boolean value out of the bitfield in the
2827 : same way as vect_recog_gcond_pattern does. However because
2828 : patterns cannot match the same root twice, when we handle and
2829 : lower the bitfield in the gcond, vect_recog_gcond_pattern can't
2830 : apply anymore. We should really fix it so that we don't need to
2831 : duplicate transformations like these. */
2832 615 : tree new_lhs = vect_recog_temp_ssa_var (boolean_type_node, NULL);
2833 615 : gcond *cond_stmt = dyn_cast <gcond *> (stmt_info->stmt);
2834 615 : tree cond_cst = gimple_cond_rhs (cond_stmt);
2835 615 : gimple *new_stmt
2836 615 : = gimple_build_assign (new_lhs, gimple_cond_code (cond_stmt),
2837 : gimple_get_lhs (pattern_stmt),
2838 : fold_convert (container_type, cond_cst));
2839 615 : append_pattern_def_seq (vinfo, stmt_info, new_stmt, vectype, container_type);
2840 615 : pattern_stmt
2841 615 : = gimple_build_cond (NE_EXPR, new_lhs,
2842 615 : build_zero_cst (TREE_TYPE (new_lhs)),
2843 : NULL_TREE, NULL_TREE);
2844 : }
2845 :
2846 2031 : *type_out = STMT_VINFO_VECTYPE (stmt_info);
2847 2031 : vect_pattern_detected ("bitfield_ref pattern", stmt_info->stmt);
2848 :
2849 2031 : return pattern_stmt;
2850 : }
2851 :
2852 : /* Function vect_recog_bit_insert_pattern
2853 :
2854 : Try to find the following pattern:
2855 :
2856 : written = BIT_INSERT_EXPR (container, value, bitpos);
2857 :
2858 : Input:
2859 :
2860 : * STMT_VINFO: The stmt we want to replace.
2861 :
2862 : Output:
2863 :
2864 : * TYPE_OUT: The vector type of the output of this pattern.
2865 :
2866 : * Return value: A new stmt that will be used to replace the sequence of
2867 : stmts that constitute the pattern. In this case it will be:
2868 : value = (container_type) value; // Make sure
2869 : shifted = value << bitpos; // Shift value into place
2870 : masked = shifted & (mask << bitpos); // Mask off the non-relevant bits in
2871 : // the 'to-write value'.
2872 : cleared = container & ~(mask << bitpos); // Clearing the bits we want to
2873 : // write to from the value we want
2874 : // to write to.
2875 : written = cleared | masked; // Write bits.
2876 :
2877 :
2878 : where mask = ((1 << TYPE_PRECISION (value)) - 1), a mask to keep the number of
2879 : bits corresponding to the real size of the bitfield value we are writing to.
2880 : The shifting is always optional depending on whether bitpos != 0.
2881 :
2882 : */
2883 :
2884 : static gimple *
2885 32099160 : vect_recog_bit_insert_pattern (vec_info *vinfo, stmt_vec_info stmt_info,
2886 : tree *type_out)
2887 : {
2888 32099160 : gassign *bf_stmt = dyn_cast <gassign *> (stmt_info->stmt);
2889 29823149 : if (!bf_stmt || gimple_assign_rhs_code (bf_stmt) != BIT_INSERT_EXPR)
2890 : return NULL;
2891 :
2892 668 : tree container = gimple_assign_rhs1 (bf_stmt);
2893 668 : tree value = gimple_assign_rhs2 (bf_stmt);
2894 668 : tree shift = gimple_assign_rhs3 (bf_stmt);
2895 :
2896 668 : tree bf_type = TREE_TYPE (value);
2897 668 : tree container_type = TREE_TYPE (container);
2898 :
2899 668 : if (!INTEGRAL_TYPE_P (container_type)
2900 668 : || !tree_fits_uhwi_p (TYPE_SIZE (container_type)))
2901 : return NULL;
2902 :
2903 500 : gimple *pattern_stmt;
2904 :
2905 500 : vect_unpromoted_value unprom;
2906 500 : unprom.set_op (value, vect_internal_def);
2907 500 : value = vect_convert_input (vinfo, stmt_info, container_type, &unprom,
2908 : get_vectype_for_scalar_type (vinfo,
2909 : container_type));
2910 :
2911 500 : unsigned HOST_WIDE_INT mask_width = TYPE_PRECISION (bf_type);
2912 500 : unsigned HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (container_type));
2913 500 : unsigned HOST_WIDE_INT shift_n = tree_to_uhwi (shift);
2914 500 : if (BYTES_BIG_ENDIAN)
2915 : {
2916 : shift_n = prec - shift_n - mask_width;
2917 : shift = build_int_cst (TREE_TYPE (shift), shift_n);
2918 : }
2919 :
2920 500 : if (!useless_type_conversion_p (TREE_TYPE (value), container_type))
2921 : {
2922 0 : pattern_stmt =
2923 0 : gimple_build_assign (vect_recog_temp_ssa_var (container_type),
2924 : NOP_EXPR, value);
2925 0 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
2926 0 : value = gimple_get_lhs (pattern_stmt);
2927 : }
2928 :
2929 : /* Shift VALUE into place. */
2930 500 : tree shifted = value;
2931 500 : if (shift_n)
2932 : {
2933 249 : gimple_seq stmts = NULL;
2934 249 : shifted
2935 249 : = gimple_build (&stmts, LSHIFT_EXPR, container_type, value, shift);
2936 249 : if (!gimple_seq_empty_p (stmts))
2937 112 : append_pattern_def_seq (vinfo, stmt_info,
2938 : gimple_seq_first_stmt (stmts));
2939 : }
2940 :
2941 500 : tree mask_t
2942 500 : = wide_int_to_tree (container_type,
2943 500 : wi::shifted_mask (shift_n, mask_width, false, prec));
2944 :
2945 : /* Clear bits we don't want to write back from SHIFTED. */
2946 500 : gimple_seq stmts = NULL;
2947 500 : tree masked = gimple_build (&stmts, BIT_AND_EXPR, container_type, shifted,
2948 : mask_t);
2949 500 : if (!gimple_seq_empty_p (stmts))
2950 : {
2951 110 : pattern_stmt = gimple_seq_first_stmt (stmts);
2952 110 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
2953 : }
2954 :
2955 : /* Mask off the bits in the container that we are to write to. */
2956 500 : mask_t = wide_int_to_tree (container_type,
2957 500 : wi::shifted_mask (shift_n, mask_width, true, prec));
2958 500 : tree cleared = vect_recog_temp_ssa_var (container_type);
2959 500 : pattern_stmt = gimple_build_assign (cleared, BIT_AND_EXPR, container, mask_t);
2960 500 : append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
2961 :
2962 : /* Write MASKED into CLEARED. */
2963 500 : pattern_stmt
2964 500 : = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
2965 : BIT_IOR_EXPR, cleared, masked);
2966 :
2967 500 : *type_out = STMT_VINFO_VECTYPE (stmt_info);
2968 500 : vect_pattern_detected ("bit_insert pattern", stmt_info->stmt);
2969 :
2970 500 : return pattern_stmt;
2971 : }
2972 :
2973 :
2974 : /* Recognize cases in which an operation is performed in one type WTYPE
2975 : but could be done more efficiently in a narrower type NTYPE. For example,
2976 : if we have:
2977 :
2978 : ATYPE a; // narrower than NTYPE
2979 : BTYPE b; // narrower than NTYPE
2980 : WTYPE aw = (WTYPE) a;
2981 : WTYPE bw = (WTYPE) b;
2982 : WTYPE res = aw + bw; // only uses of aw and bw
2983 :
2984 : then it would be more efficient to do:
2985 :
2986 : NTYPE an = (NTYPE) a;
2987 : NTYPE bn = (NTYPE) b;
2988 : NTYPE resn = an + bn;
2989 : WTYPE res = (WTYPE) resn;
2990 :
2991 : Other situations include things like:
2992 :
2993 : ATYPE a; // NTYPE or narrower
2994 : WTYPE aw = (WTYPE) a;
2995 : WTYPE res = aw + b;
2996 :
2997 : when only "(NTYPE) res" is significant. In that case it's more efficient
2998 : to truncate "b" and do the operation on NTYPE instead:
2999 :
3000 : NTYPE an = (NTYPE) a;
3001 : NTYPE bn = (NTYPE) b; // truncation
3002 : NTYPE resn = an + bn;
3003 : WTYPE res = (WTYPE) resn;
3004 :
3005 : All users of "res" should then use "resn" instead, making the final
3006 : statement dead (not marked as relevant). The final statement is still
3007 : needed to maintain the type correctness of the IR.
3008 :
3009 : vect_determine_precisions has already determined the minimum
3010 : precision of the operation and the minimum precision required
3011 : by users of the result. */
3012 :
3013 : static gimple *
3014 32099600 : vect_recog_over_widening_pattern (vec_info *vinfo,
3015 : stmt_vec_info last_stmt_info, tree *type_out)
3016 : {
3017 32099600 : gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
3018 22392262 : if (!last_stmt)
3019 : return NULL;
3020 :
3021 : /* See whether we have found that this operation can be done on a
3022 : narrower type without changing its semantics. */
3023 22392262 : unsigned int new_precision = last_stmt_info->operation_precision;
3024 22392262 : if (!new_precision)
3025 : return NULL;
3026 :
3027 1666085 : tree lhs = gimple_assign_lhs (last_stmt);
3028 1666085 : tree type = TREE_TYPE (lhs);
3029 1666085 : tree_code code = gimple_assign_rhs_code (last_stmt);
3030 :
3031 : /* Punt for reductions where we don't handle the type conversions. */
3032 1666085 : if (vect_is_reduction (last_stmt_info))
3033 : return NULL;
3034 :
3035 : /* Keep the first operand of a COND_EXPR as-is: only the other two
3036 : operands are interesting. */
3037 1657739 : unsigned int first_op = (code == COND_EXPR ? 2 : 1);
3038 :
3039 : /* Check the operands. */
3040 1657739 : unsigned int nops = gimple_num_ops (last_stmt) - first_op;
3041 1657739 : auto_vec <vect_unpromoted_value, 3> unprom (nops);
3042 1657739 : unprom.quick_grow_cleared (nops);
3043 1657739 : unsigned int min_precision = 0;
3044 1657739 : bool single_use_p = false;
3045 4953051 : for (unsigned int i = 0; i < nops; ++i)
3046 : {
3047 3297001 : tree op = gimple_op (last_stmt, first_op + i);
3048 3297001 : if (TREE_CODE (op) == INTEGER_CST)
3049 1506593 : unprom[i].set_op (op, vect_constant_def);
3050 1790408 : else if (TREE_CODE (op) == SSA_NAME)
3051 : {
3052 1790408 : bool op_single_use_p = true;
3053 1790408 : if (!vect_look_through_possible_promotion (vinfo, op, &unprom[i],
3054 : &op_single_use_p))
3055 1689 : return NULL;
3056 : /* If:
3057 :
3058 : (1) N bits of the result are needed;
3059 : (2) all inputs are widened from M<N bits; and
3060 : (3) one operand OP is a single-use SSA name
3061 :
3062 : we can shift the M->N widening from OP to the output
3063 : without changing the number or type of extensions involved.
3064 : This then reduces the number of copies of STMT_INFO.
3065 :
3066 : If instead of (3) more than one operand is a single-use SSA name,
3067 : shifting the extension to the output is even more of a win.
3068 :
3069 : If instead:
3070 :
3071 : (1) N bits of the result are needed;
3072 : (2) one operand OP2 is widened from M2<N bits;
3073 : (3) another operand OP1 is widened from M1<M2 bits; and
3074 : (4) both OP1 and OP2 are single-use
3075 :
3076 : the choice is between:
3077 :
3078 : (a) truncating OP2 to M1, doing the operation on M1,
3079 : and then widening the result to N
3080 :
3081 : (b) widening OP1 to M2, doing the operation on M2, and then
3082 : widening the result to N
3083 :
3084 : Both shift the M2->N widening of the inputs to the output.
3085 : (a) additionally shifts the M1->M2 widening to the output;
3086 : it requires fewer copies of STMT_INFO but requires an extra
3087 : M2->M1 truncation.
3088 :
3089 : Which is better will depend on the complexity and cost of
3090 : STMT_INFO, which is hard to predict at this stage. However,
3091 : a clear tie-breaker in favor of (b) is the fact that the
3092 : truncation in (a) increases the length of the operation chain.
3093 :
3094 : If instead of (4) only one of OP1 or OP2 is single-use,
3095 : (b) is still a win over doing the operation in N bits:
3096 : it still shifts the M2->N widening on the single-use operand
3097 : to the output and reduces the number of STMT_INFO copies.
3098 :
3099 : If neither operand is single-use then operating on fewer than
3100 : N bits might lead to more extensions overall. Whether it does
3101 : or not depends on global information about the vectorization
3102 : region, and whether that's a good trade-off would again
3103 : depend on the complexity and cost of the statements involved,
3104 : as well as things like register pressure that are not normally
3105 : modelled at this stage. We therefore ignore these cases
3106 : and just optimize the clear single-use wins above.
3107 :
3108 : Thus we take the maximum precision of the unpromoted operands
3109 : and record whether any operand is single-use. */
3110 1788719 : if (unprom[i].dt == vect_internal_def)
3111 : {
3112 1033858 : min_precision = MAX (min_precision,
3113 : TYPE_PRECISION (unprom[i].type));
3114 1033858 : single_use_p |= op_single_use_p;
3115 : }
3116 : }
3117 : else
3118 : return NULL;
3119 : }
3120 :
3121 : /* Although the operation could be done in operation_precision, we have
3122 : to balance that against introducing extra truncations or extensions.
3123 : Calculate the minimum precision that can be handled efficiently.
3124 :
3125 : The loop above determined that the operation could be handled
3126 : efficiently in MIN_PRECISION if SINGLE_USE_P; this would shift an
3127 : extension from the inputs to the output without introducing more
3128 : instructions, and would reduce the number of instructions required
3129 : for STMT_INFO itself.
3130 :
3131 : vect_determine_precisions has also determined that the result only
3132 : needs min_output_precision bits. Truncating by a factor of N times
3133 : requires a tree of N - 1 instructions, so if TYPE is N times wider
3134 : than min_output_precision, doing the operation in TYPE and truncating
3135 : the result requires N + (N - 1) = 2N - 1 instructions per output vector.
3136 : In contrast:
3137 :
3138 : - truncating the input to a unary operation and doing the operation
3139 : in the new type requires at most N - 1 + 1 = N instructions per
3140 : output vector
3141 :
3142 : - doing the same for a binary operation requires at most
3143 : (N - 1) * 2 + 1 = 2N - 1 instructions per output vector
3144 :
3145 : Both unary and binary operations require fewer instructions than
3146 : this if the operands were extended from a suitable truncated form.
3147 : Thus there is usually nothing to lose by doing operations in
3148 : min_output_precision bits, but there can be something to gain. */
3149 1656050 : if (!single_use_p)
3150 1309722 : min_precision = last_stmt_info->min_output_precision;
3151 : else
3152 346328 : min_precision = MIN (min_precision, last_stmt_info->min_output_precision);
3153 :
3154 : /* Apply the minimum efficient precision we just calculated. */
3155 1656050 : if (new_precision < min_precision)
3156 : new_precision = min_precision;
3157 1656050 : new_precision = vect_element_precision (new_precision);
3158 1656050 : if (new_precision >= TYPE_PRECISION (type))
3159 : return NULL;
3160 :
3161 154287 : vect_pattern_detected ("vect_recog_over_widening_pattern", last_stmt);
3162 :
3163 154287 : *type_out = get_vectype_for_scalar_type (vinfo, type);
3164 154287 : if (!*type_out)
3165 : return NULL;
3166 :
3167 : /* We've found a viable pattern. Get the new type of the operation. */
3168 136654 : bool unsigned_p = (last_stmt_info->operation_sign == UNSIGNED);
3169 136654 : tree new_type = build_nonstandard_integer_type (new_precision, unsigned_p);
3170 :
3171 : /* If we're truncating an operation, we need to make sure that we
3172 : don't introduce new undefined overflow. The codes tested here are
3173 : a subset of those accepted by vect_truncatable_operation_p. */
3174 136654 : tree op_type = new_type;
3175 136654 : if (TYPE_OVERFLOW_UNDEFINED (new_type)
3176 176956 : && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR))
3177 27623 : op_type = build_nonstandard_integer_type (new_precision, true);
3178 :
3179 136654 : tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
3180 136654 : tree op_vectype = get_vectype_for_scalar_type (vinfo, op_type);
3181 136654 : if (!new_vectype || !op_vectype)
3182 : return NULL;
3183 :
3184 : /* Verify we can handle the new operation. For shifts and rotates
3185 : apply heuristic of whether we are likely facing vector-vector or
3186 : vector-scalar operation. Since we are eventually expecting that
3187 : a later pattern might eventually want to rewrite an unsupported
3188 : into a supported case error on that side in case the original
3189 : operation was not supported either or this is a binary operation
3190 : and the 2nd operand is constant. */
3191 136654 : if (code == RSHIFT_EXPR || code == LSHIFT_EXPR || code == RROTATE_EXPR)
3192 : {
3193 28916 : if (!target_has_vecop_for_code (code, op_vectype, optab_vector)
3194 27857 : && ((unprom[1].dt != vect_external_def
3195 27445 : && unprom[1].dt != vect_constant_def)
3196 18986 : || !target_has_vecop_for_code (code, op_vectype, optab_scalar))
3197 37816 : && !(!target_has_vecop_for_code (code, *type_out, optab_vector)
3198 7802 : && ((unprom[1].dt != vect_external_def
3199 7802 : || unprom[1].dt != vect_constant_def)
3200 : || !target_has_vecop_for_code (code, *type_out,
3201 : optab_scalar))))
3202 : return NULL;
3203 : }
3204 107738 : else if (!target_has_vecop_for_code (code, op_vectype, optab_vector)
3205 107738 : && (target_has_vecop_for_code (code, *type_out, optab_vector)
3206 29 : && !(nops == 2 && unprom[1].dt == vect_constant_def)))
3207 : return NULL;
3208 :
3209 135546 : if (dump_enabled_p ())
3210 4335 : dump_printf_loc (MSG_NOTE, vect_location, "demoting %T to %T\n",
3211 : type, new_type);
3212 :
3213 : /* Calculate the rhs operands for an operation on OP_TYPE. */
3214 135546 : tree ops[3] = {};
3215 135775 : for (unsigned int i = 1; i < first_op; ++i)
3216 229 : ops[i - 1] = gimple_op (last_stmt, i);
3217 135546 : vect_convert_inputs (vinfo, last_stmt_info, nops, &ops[first_op - 1],
3218 135546 : op_type, &unprom[0], op_vectype);
3219 :
3220 : /* Use the operation to produce a result of type OP_TYPE. */
3221 135546 : tree new_var = vect_recog_temp_ssa_var (op_type, NULL);
3222 135546 : gimple *pattern_stmt = gimple_build_assign (new_var, code,
3223 : ops[0], ops[1], ops[2]);
3224 135546 : gimple_set_location (pattern_stmt, gimple_location (last_stmt));
3225 :
3226 135546 : if (dump_enabled_p ())
3227 4335 : dump_printf_loc (MSG_NOTE, vect_location,
3228 : "created pattern stmt: %G", pattern_stmt);
3229 :
3230 : /* Convert back to the original signedness, if OP_TYPE is different
3231 : from NEW_TYPE. */
3232 135546 : if (op_type != new_type)
3233 27616 : pattern_stmt = vect_convert_output (vinfo, last_stmt_info, new_type,
3234 : pattern_stmt, op_vectype);
3235 :
3236 : /* Promote the result to the original type. */
3237 135546 : pattern_stmt = vect_convert_output (vinfo, last_stmt_info, type,
3238 : pattern_stmt, new_vectype);
3239 :
3240 135546 : return pattern_stmt;
3241 1657739 : }
3242 :
3243 : /* Recognize the following patterns:
3244 :
3245 : ATYPE a; // narrower than TYPE
3246 : BTYPE b; // narrower than TYPE
3247 :
3248 : 1) Multiply high with scaling
3249 : TYPE res = ((TYPE) a * (TYPE) b) >> c;
3250 : Here, c is bitsize (TYPE) / 2 - 1.
3251 :
3252 : 2) ... or also with rounding
3253 : TYPE res = (((TYPE) a * (TYPE) b) >> d + 1) >> 1;
3254 : Here, d is bitsize (TYPE) / 2 - 2.
3255 :
3256 : 3) Normal multiply high
3257 : TYPE res = ((TYPE) a * (TYPE) b) >> e;
3258 : Here, e is bitsize (TYPE) / 2.
3259 :
3260 : where only the bottom half of res is used. */
3261 :
3262 : static gimple *
3263 32227600 : vect_recog_mulhs_pattern (vec_info *vinfo,
3264 : stmt_vec_info last_stmt_info, tree *type_out)
3265 : {
3266 : /* Check for a right shift. */
3267 32227600 : gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
3268 22520104 : if (!last_stmt
3269 22520104 : || gimple_assign_rhs_code (last_stmt) != RSHIFT_EXPR)
3270 : return NULL;
3271 :
3272 : /* Check that the shift result is wider than the users of the
3273 : result need (i.e. that narrowing would be a natural choice). */
3274 367903 : tree lhs_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
3275 367903 : unsigned int target_precision
3276 367903 : = vect_element_precision (last_stmt_info->min_output_precision);
3277 367903 : if (!INTEGRAL_TYPE_P (lhs_type)
3278 367903 : || target_precision >= TYPE_PRECISION (lhs_type))
3279 : return NULL;
3280 :
3281 : /* Look through any change in sign on the outer shift input. */
3282 53145 : vect_unpromoted_value unprom_rshift_input;
3283 53145 : tree rshift_input = vect_look_through_possible_promotion
3284 53145 : (vinfo, gimple_assign_rhs1 (last_stmt), &unprom_rshift_input);
3285 53145 : if (!rshift_input
3286 53145 : || TYPE_PRECISION (TREE_TYPE (rshift_input))
3287 52547 : != TYPE_PRECISION (lhs_type))
3288 : return NULL;
3289 :
3290 : /* Get the definition of the shift input. */
3291 48252 : stmt_vec_info rshift_input_stmt_info
3292 48252 : = vect_get_internal_def (vinfo, rshift_input);
3293 48252 : if (!rshift_input_stmt_info)
3294 : return NULL;
3295 43621 : gassign *rshift_input_stmt
3296 32267784 : = dyn_cast <gassign *> (rshift_input_stmt_info->stmt);
3297 40328 : if (!rshift_input_stmt)
3298 : return NULL;
3299 :
3300 40328 : stmt_vec_info mulh_stmt_info;
3301 40328 : tree scale_term;
3302 40328 : bool rounding_p = false;
3303 :
3304 : /* Check for the presence of the rounding term. */
3305 47725 : if (gimple_assign_rhs_code (rshift_input_stmt) == PLUS_EXPR)
3306 : {
3307 : /* Check that the outer shift was by 1. */
3308 18912 : if (!integer_onep (gimple_assign_rhs2 (last_stmt)))
3309 9389 : return NULL;
3310 :
3311 : /* Check that the second operand of the PLUS_EXPR is 1. */
3312 1304 : if (!integer_onep (gimple_assign_rhs2 (rshift_input_stmt)))
3313 : return NULL;
3314 :
3315 : /* Look through any change in sign on the addition input. */
3316 110 : vect_unpromoted_value unprom_plus_input;
3317 110 : tree plus_input = vect_look_through_possible_promotion
3318 110 : (vinfo, gimple_assign_rhs1 (rshift_input_stmt), &unprom_plus_input);
3319 110 : if (!plus_input
3320 110 : || TYPE_PRECISION (TREE_TYPE (plus_input))
3321 110 : != TYPE_PRECISION (TREE_TYPE (rshift_input)))
3322 : return NULL;
3323 :
3324 : /* Get the definition of the multiply-high-scale part. */
3325 110 : stmt_vec_info plus_input_stmt_info
3326 110 : = vect_get_internal_def (vinfo, plus_input);
3327 110 : if (!plus_input_stmt_info)
3328 : return NULL;
3329 110 : gassign *plus_input_stmt
3330 9499 : = dyn_cast <gassign *> (plus_input_stmt_info->stmt);
3331 110 : if (!plus_input_stmt
3332 110 : || gimple_assign_rhs_code (plus_input_stmt) != RSHIFT_EXPR)
3333 : return NULL;
3334 :
3335 : /* Look through any change in sign on the scaling input. */
3336 67 : vect_unpromoted_value unprom_scale_input;
3337 67 : tree scale_input = vect_look_through_possible_promotion
3338 67 : (vinfo, gimple_assign_rhs1 (plus_input_stmt), &unprom_scale_input);
3339 67 : if (!scale_input
3340 67 : || TYPE_PRECISION (TREE_TYPE (scale_input))
3341 67 : != TYPE_PRECISION (TREE_TYPE (plus_input)))
3342 : return NULL;
3343 :
3344 : /* Get the definition of the multiply-high part. */
3345 67 : mulh_stmt_info = vect_get_internal_def (vinfo, scale_input);
3346 67 : if (!mulh_stmt_info)
3347 : return NULL;
3348 :
3349 : /* Get the scaling term. */
3350 67 : scale_term = gimple_assign_rhs2 (plus_input_stmt);
3351 67 : rounding_p = true;
3352 : }
3353 : else
3354 : {
3355 30872 : mulh_stmt_info = rshift_input_stmt_info;
3356 30872 : scale_term = gimple_assign_rhs2 (last_stmt);
3357 : }
3358 :
3359 : /* Check that the scaling factor is constant. */
3360 30939 : if (TREE_CODE (scale_term) != INTEGER_CST)
3361 : return NULL;
3362 :
3363 : /* Check whether the scaling input term can be seen as two widened
3364 : inputs multiplied together. */
3365 89982 : vect_unpromoted_value unprom_mult[2];
3366 29994 : tree new_type;
3367 29994 : unsigned int nops
3368 29994 : = vect_widened_op_tree (vinfo, mulh_stmt_info, MULT_EXPR, WIDEN_MULT_EXPR,
3369 : false, 2, unprom_mult, &new_type);
3370 29994 : if (nops != 2)
3371 : return NULL;
3372 :
3373 : /* Adjust output precision. */
3374 1215 : if (TYPE_PRECISION (new_type) < target_precision)
3375 0 : new_type = build_nonstandard_integer_type
3376 0 : (target_precision, TYPE_UNSIGNED (new_type));
3377 :
3378 1215 : unsigned mult_precision = TYPE_PRECISION (new_type);
3379 1215 : internal_fn ifn;
3380 : /* Check that the scaling factor is expected. Instead of
3381 : target_precision, we should use the one that we actually
3382 : use for internal function. */
3383 1215 : if (rounding_p)
3384 : {
3385 : /* Check pattern 2). */
3386 134 : if (wi::to_widest (scale_term) + mult_precision + 2
3387 201 : != TYPE_PRECISION (lhs_type))
3388 : return NULL;
3389 :
3390 : ifn = IFN_MULHRS;
3391 : }
3392 : else
3393 : {
3394 : /* Check for pattern 1). */
3395 2296 : if (wi::to_widest (scale_term) + mult_precision + 1
3396 3444 : == TYPE_PRECISION (lhs_type))
3397 : ifn = IFN_MULHS;
3398 : /* Check for pattern 3). */
3399 1114 : else if (wi::to_widest (scale_term) + mult_precision
3400 2228 : == TYPE_PRECISION (lhs_type))
3401 : ifn = IFN_MULH;
3402 : else
3403 : return NULL;
3404 : }
3405 :
3406 1152 : vect_pattern_detected ("vect_recog_mulhs_pattern", last_stmt);
3407 :
3408 : /* Check for target support. */
3409 1152 : tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
3410 1152 : if (!new_vectype
3411 2280 : || !direct_internal_fn_supported_p
3412 1128 : (ifn, new_vectype, OPTIMIZE_FOR_SPEED))
3413 1008 : return NULL;
3414 :
3415 : /* The IR requires a valid vector type for the cast result, even though
3416 : it's likely to be discarded. */
3417 144 : *type_out = get_vectype_for_scalar_type (vinfo, lhs_type);
3418 144 : if (!*type_out)
3419 : return NULL;
3420 :
3421 : /* Generate the IFN_MULHRS call. */
3422 144 : tree new_var = vect_recog_temp_ssa_var (new_type, NULL);
3423 144 : tree new_ops[2];
3424 144 : vect_convert_inputs (vinfo, last_stmt_info, 2, new_ops, new_type,
3425 : unprom_mult, new_vectype);
3426 144 : gcall *mulhrs_stmt
3427 144 : = gimple_build_call_internal (ifn, 2, new_ops[0], new_ops[1]);
3428 144 : gimple_call_set_lhs (mulhrs_stmt, new_var);
3429 144 : gimple_set_location (mulhrs_stmt, gimple_location (last_stmt));
3430 :
3431 144 : if (dump_enabled_p ())
3432 0 : dump_printf_loc (MSG_NOTE, vect_location,
3433 : "created pattern stmt: %G", (gimple *) mulhrs_stmt);
3434 :
3435 144 : return vect_convert_output (vinfo, last_stmt_info, lhs_type,
3436 144 : mulhrs_stmt, new_vectype);
3437 : }
3438 :
3439 : /* Recognize the patterns:
3440 :
3441 : ATYPE a; // narrower than TYPE
3442 : BTYPE b; // narrower than TYPE
3443 : (1) TYPE avg = ((TYPE) a + (TYPE) b) >> 1;
3444 : or (2) TYPE avg = ((TYPE) a + (TYPE) b + 1) >> 1;
3445 :
3446 : where only the bottom half of avg is used. Try to transform them into:
3447 :
3448 : (1) NTYPE avg' = .AVG_FLOOR ((NTYPE) a, (NTYPE) b);
3449 : or (2) NTYPE avg' = .AVG_CEIL ((NTYPE) a, (NTYPE) b);
3450 :
3451 : followed by:
3452 :
3453 : TYPE avg = (TYPE) avg';
3454 :
3455 : where NTYPE is no wider than half of TYPE. Since only the bottom half
3456 : of avg is used, all or part of the cast of avg' should become redundant.
3457 :
3458 : If there is no target support available, generate code to distribute rshift
3459 : over plus and add a carry. */
3460 :
3461 : static gimple *
3462 32225966 : vect_recog_average_pattern (vec_info *vinfo,
3463 : stmt_vec_info last_stmt_info, tree *type_out)
3464 : {
3465 : /* Check for a shift right by one bit. */
3466 32225966 : gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
3467 22518628 : if (!last_stmt
3468 22518628 : || gimple_assign_rhs_code (last_stmt) != RSHIFT_EXPR
3469 367809 : || !integer_onep (gimple_assign_rhs2 (last_stmt)))
3470 32168884 : return NULL;
3471 :
3472 : /* Check that the shift result is wider than the users of the
3473 : result need (i.e. that narrowing would be a natural choice). */
3474 57082 : tree lhs = gimple_assign_lhs (last_stmt);
3475 57082 : tree type = TREE_TYPE (lhs);
3476 57082 : unsigned int target_precision
3477 57082 : = vect_element_precision (last_stmt_info->min_output_precision);
3478 57082 : if (!INTEGRAL_TYPE_P (type) || target_precision >= TYPE_PRECISION (type))
3479 : return NULL;
3480 :
3481 : /* Look through any change in sign on the shift input. */
3482 2207 : tree rshift_rhs = gimple_assign_rhs1 (last_stmt);
3483 2207 : vect_unpromoted_value unprom_plus;
3484 2207 : rshift_rhs = vect_look_through_possible_promotion (vinfo, rshift_rhs,
3485 : &unprom_plus);
3486 2207 : if (!rshift_rhs
3487 2207 : || TYPE_PRECISION (TREE_TYPE (rshift_rhs)) != TYPE_PRECISION (type))
3488 : return NULL;
3489 :
3490 : /* Get the definition of the shift input. */
3491 2205 : stmt_vec_info plus_stmt_info = vect_get_internal_def (vinfo, rshift_rhs);
3492 2205 : if (!plus_stmt_info)
3493 : return NULL;
3494 :
3495 : /* Check whether the shift input can be seen as a tree of additions on
3496 : 2 or 3 widened inputs.
3497 :
3498 : Note that the pattern should be a win even if the result of one or
3499 : more additions is reused elsewhere: if the pattern matches, we'd be
3500 : replacing 2N RSHIFT_EXPRs and N VEC_PACK_*s with N IFN_AVG_*s. */
3501 8796 : internal_fn ifn = IFN_AVG_FLOOR;
3502 8796 : vect_unpromoted_value unprom[3];
3503 2199 : tree new_type;
3504 2199 : unsigned int nops = vect_widened_op_tree (vinfo, plus_stmt_info, PLUS_EXPR,
3505 2199 : IFN_VEC_WIDEN_PLUS, false, 3,
3506 : unprom, &new_type);
3507 2199 : if (nops == 0)
3508 : return NULL;
3509 907 : if (nops == 3)
3510 : {
3511 : /* Check that one operand is 1. */
3512 : unsigned int i;
3513 987 : for (i = 0; i < 3; ++i)
3514 933 : if (integer_onep (unprom[i].op))
3515 : break;
3516 311 : if (i == 3)
3517 : return NULL;
3518 : /* Throw away the 1 operand and keep the other two. */
3519 257 : if (i < 2)
3520 0 : unprom[i] = unprom[2];
3521 : ifn = IFN_AVG_CEIL;
3522 : }
3523 :
3524 853 : vect_pattern_detected ("vect_recog_average_pattern", last_stmt);
3525 :
3526 : /* We know that:
3527 :
3528 : (a) the operation can be viewed as:
3529 :
3530 : TYPE widened0 = (TYPE) UNPROM[0];
3531 : TYPE widened1 = (TYPE) UNPROM[1];
3532 : TYPE tmp1 = widened0 + widened1 {+ 1};
3533 : TYPE tmp2 = tmp1 >> 1; // LAST_STMT_INFO
3534 :
3535 : (b) the first two statements are equivalent to:
3536 :
3537 : TYPE widened0 = (TYPE) (NEW_TYPE) UNPROM[0];
3538 : TYPE widened1 = (TYPE) (NEW_TYPE) UNPROM[1];
3539 :
3540 : (c) vect_recog_over_widening_pattern has already tried to narrow TYPE
3541 : where sensible;
3542 :
3543 : (d) all the operations can be performed correctly at twice the width of
3544 : NEW_TYPE, due to the nature of the average operation; and
3545 :
3546 : (e) users of the result of the right shift need only TARGET_PRECISION
3547 : bits, where TARGET_PRECISION is no more than half of TYPE's
3548 : precision.
3549 :
3550 : Under these circumstances, the only situation in which NEW_TYPE
3551 : could be narrower than TARGET_PRECISION is if widened0, widened1
3552 : and an addition result are all used more than once. Thus we can
3553 : treat any widening of UNPROM[0] and UNPROM[1] to TARGET_PRECISION
3554 : as "free", whereas widening the result of the average instruction
3555 : from NEW_TYPE to TARGET_PRECISION would be a new operation. It's
3556 : therefore better not to go narrower than TARGET_PRECISION. */
3557 853 : if (TYPE_PRECISION (new_type) < target_precision)
3558 0 : new_type = build_nonstandard_integer_type (target_precision,
3559 0 : TYPE_UNSIGNED (new_type));
3560 :
3561 : /* Check for target support. */
3562 853 : tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
3563 853 : if (!new_vectype)
3564 : return NULL;
3565 :
3566 853 : bool fallback_p = false;
3567 :
3568 853 : if (direct_internal_fn_supported_p (ifn, new_vectype, OPTIMIZE_FOR_SPEED))
3569 : ;
3570 695 : else if (TYPE_UNSIGNED (new_type)
3571 256 : && optab_for_tree_code (RSHIFT_EXPR, new_vectype, optab_scalar)
3572 256 : && optab_for_tree_code (PLUS_EXPR, new_vectype, optab_default)
3573 256 : && optab_for_tree_code (BIT_IOR_EXPR, new_vectype, optab_default)
3574 951 : && optab_for_tree_code (BIT_AND_EXPR, new_vectype, optab_default))
3575 : fallback_p = true;
3576 : else
3577 439 : return NULL;
3578 :
3579 : /* The IR requires a valid vector type for the cast result, even though
3580 : it's likely to be discarded. */
3581 414 : *type_out = get_vectype_for_scalar_type (vinfo, type);
3582 414 : if (!*type_out)
3583 : return NULL;
3584 :
3585 410 : tree new_var = vect_recog_temp_ssa_var (new_type, NULL);
3586 410 : tree new_ops[2];
3587 410 : vect_convert_inputs (vinfo, last_stmt_info, 2, new_ops, new_type,
3588 : unprom, new_vectype);
3589 :
3590 410 : if (fallback_p)
3591 : {
3592 : /* As a fallback, generate code for following sequence:
3593 :
3594 : shifted_op0 = new_ops[0] >> 1;
3595 : shifted_op1 = new_ops[1] >> 1;
3596 : sum_of_shifted = shifted_op0 + shifted_op1;
3597 : unmasked_carry = new_ops[0] and/or new_ops[1];
3598 : carry = unmasked_carry & 1;
3599 : new_var = sum_of_shifted + carry;
3600 : */
3601 :
3602 252 : tree one_cst = build_one_cst (new_type);
3603 252 : gassign *g;
3604 :
3605 252 : tree shifted_op0 = vect_recog_temp_ssa_var (new_type, NULL);
3606 252 : g = gimple_build_assign (shifted_op0, RSHIFT_EXPR, new_ops[0], one_cst);
3607 252 : append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
3608 :
3609 252 : tree shifted_op1 = vect_recog_temp_ssa_var (new_type, NULL);
3610 252 : g = gimple_build_assign (shifted_op1, RSHIFT_EXPR, new_ops[1], one_cst);
3611 252 : append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
3612 :
3613 252 : tree sum_of_shifted = vect_recog_temp_ssa_var (new_type, NULL);
3614 252 : g = gimple_build_assign (sum_of_shifted, PLUS_EXPR,
3615 : shifted_op0, shifted_op1);
3616 252 : append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
3617 :
3618 252 : tree unmasked_carry = vect_recog_temp_ssa_var (new_type, NULL);
3619 252 : tree_code c = (ifn == IFN_AVG_CEIL) ? BIT_IOR_EXPR : BIT_AND_EXPR;
3620 252 : g = gimple_build_assign (unmasked_carry, c, new_ops[0], new_ops[1]);
3621 252 : append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
3622 :
3623 252 : tree carry = vect_recog_temp_ssa_var (new_type, NULL);
3624 252 : g = gimple_build_assign (carry, BIT_AND_EXPR, unmasked_carry, one_cst);
3625 252 : append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
3626 :
3627 252 : g = gimple_build_assign (new_var, PLUS_EXPR, sum_of_shifted, carry);
3628 252 : return vect_convert_output (vinfo, last_stmt_info, type, g, new_vectype);
3629 : }
3630 :
3631 : /* Generate the IFN_AVG* call. */
3632 158 : gcall *average_stmt = gimple_build_call_internal (ifn, 2, new_ops[0],
3633 : new_ops[1]);
3634 158 : gimple_call_set_lhs (average_stmt, new_var);
3635 158 : gimple_set_location (average_stmt, gimple_location (last_stmt));
3636 :
3637 158 : if (dump_enabled_p ())
3638 31 : dump_printf_loc (MSG_NOTE, vect_location,
3639 : "created pattern stmt: %G", (gimple *) average_stmt);
3640 :
3641 158 : return vect_convert_output (vinfo, last_stmt_info,
3642 158 : type, average_stmt, new_vectype);
3643 : }
3644 :
3645 : /* Recognize cases in which the input to a cast is wider than its
3646 : output, and the input is fed by a widening operation. Fold this
3647 : by removing the unnecessary intermediate widening. E.g.:
3648 :
3649 : unsigned char a;
3650 : unsigned int b = (unsigned int) a;
3651 : unsigned short c = (unsigned short) b;
3652 :
3653 : -->
3654 :
3655 : unsigned short c = (unsigned short) a;
3656 :
3657 : Although this is rare in input IR, it is an expected side-effect
3658 : of the over-widening pattern above.
3659 :
3660 : This is beneficial also for integer-to-float conversions, if the
3661 : widened integer has more bits than the float, and if the unwidened
3662 : input doesn't. */
3663 :
3664 : static gimple *
3665 32227600 : vect_recog_cast_forwprop_pattern (vec_info *vinfo,
3666 : stmt_vec_info last_stmt_info, tree *type_out)
3667 : {
3668 : /* Check for a cast, including an integer-to-float conversion. */
3669 54699127 : gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
3670 22519960 : if (!last_stmt)
3671 : return NULL;
3672 22519960 : tree_code code = gimple_assign_rhs_code (last_stmt);
3673 22519960 : if (!CONVERT_EXPR_CODE_P (code) && code != FLOAT_EXPR)
3674 : return NULL;
3675 :
3676 : /* Make sure that the rhs is a scalar with a natural bitsize. */
3677 3247082 : tree lhs = gimple_assign_lhs (last_stmt);
3678 3247082 : if (!lhs)
3679 : return NULL;
3680 3247082 : tree lhs_type = TREE_TYPE (lhs);
3681 3247082 : scalar_mode lhs_mode;
3682 3226403 : if (VECT_SCALAR_BOOLEAN_TYPE_P (lhs_type)
3683 6471749 : || !is_a <scalar_mode> (TYPE_MODE (lhs_type), &lhs_mode))
3684 26262 : return NULL;
3685 :
3686 : /* Check for a narrowing operation (from a vector point of view). */
3687 3220820 : tree rhs = gimple_assign_rhs1 (last_stmt);
3688 3220820 : tree rhs_type = TREE_TYPE (rhs);
3689 3220820 : if (!INTEGRAL_TYPE_P (rhs_type)
3690 2813726 : || VECT_SCALAR_BOOLEAN_TYPE_P (rhs_type)
3691 8689030 : || TYPE_PRECISION (rhs_type) <= GET_MODE_BITSIZE (lhs_mode))
3692 : return NULL;
3693 :
3694 : /* Try to find an unpromoted input. */
3695 372633 : vect_unpromoted_value unprom;
3696 372633 : if (!vect_look_through_possible_promotion (vinfo, rhs, &unprom)
3697 372633 : || TYPE_PRECISION (unprom.type) >= TYPE_PRECISION (rhs_type))
3698 : return NULL;
3699 :
3700 : /* If the bits above RHS_TYPE matter, make sure that they're the
3701 : same when extending from UNPROM as they are when extending from RHS. */
3702 48553 : if (!INTEGRAL_TYPE_P (lhs_type)
3703 48553 : && TYPE_SIGN (rhs_type) != TYPE_SIGN (unprom.type))
3704 : return NULL;
3705 :
3706 : /* We can get the same result by casting UNPROM directly, to avoid
3707 : the unnecessary widening and narrowing. */
3708 48433 : vect_pattern_detected ("vect_recog_cast_forwprop_pattern", last_stmt);
3709 :
3710 48433 : *type_out = get_vectype_for_scalar_type (vinfo, lhs_type);
3711 48433 : if (!*type_out)
3712 : return NULL;
3713 :
3714 48433 : tree new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
3715 48433 : gimple *pattern_stmt = gimple_build_assign (new_var, code, unprom.op);
3716 48433 : gimple_set_location (pattern_stmt, gimple_location (last_stmt));
3717 :
3718 48433 : return pattern_stmt;
3719 : }
3720 :
3721 : /* Try to detect a shift left of a widened input, converting LSHIFT_EXPR
3722 : to WIDEN_LSHIFT_EXPR. See vect_recog_widen_op_pattern for details. */
3723 :
3724 : static gimple *
3725 32150816 : vect_recog_widen_shift_pattern (vec_info *vinfo,
3726 : stmt_vec_info last_stmt_info, tree *type_out)
3727 : {
3728 32150816 : return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
3729 32150816 : LSHIFT_EXPR, WIDEN_LSHIFT_EXPR, true,
3730 32150816 : "vect_recog_widen_shift_pattern");
3731 : }
3732 :
3733 : /* Detect a rotate pattern wouldn't be otherwise vectorized:
3734 :
3735 : type a_t, b_t, c_t;
3736 :
3737 : S0 a_t = b_t r<< c_t;
3738 :
3739 : Input/Output:
3740 :
3741 : * STMT_VINFO: The stmt from which the pattern search begins,
3742 : i.e. the shift/rotate stmt. The original stmt (S0) is replaced
3743 : with a sequence:
3744 :
3745 : S1 d_t = -c_t;
3746 : S2 e_t = d_t & (B - 1);
3747 : S3 f_t = b_t << c_t;
3748 : S4 g_t = b_t >> e_t;
3749 : S0 a_t = f_t | g_t;
3750 :
3751 : where B is element bitsize of type.
3752 :
3753 : Output:
3754 :
3755 : * TYPE_OUT: The type of the output of this pattern.
3756 :
3757 : * Return value: A new stmt that will be used to replace the rotate
3758 : S0 stmt. */
3759 :
3760 : static gimple *
3761 32150816 : vect_recog_rotate_pattern (vec_info *vinfo,
3762 : stmt_vec_info stmt_vinfo, tree *type_out)
3763 : {
3764 32150816 : gimple *last_stmt = stmt_vinfo->stmt;
3765 32150816 : tree oprnd0, oprnd1, lhs, var, var1, var2, vectype, type, stype, def, def2;
3766 32150816 : gimple *pattern_stmt, *def_stmt;
3767 32150816 : enum tree_code rhs_code;
3768 32150816 : enum vect_def_type dt;
3769 32150816 : optab optab1, optab2;
3770 32150816 : edge ext_def = NULL;
3771 32150816 : bool bswap16_p = false;
3772 :
3773 32150816 : if (is_gimple_assign (last_stmt))
3774 : {
3775 22443126 : rhs_code = gimple_assign_rhs_code (last_stmt);
3776 22443126 : switch (rhs_code)
3777 : {
3778 8568 : case LROTATE_EXPR:
3779 8568 : case RROTATE_EXPR:
3780 8568 : break;
3781 : default:
3782 : return NULL;
3783 : }
3784 :
3785 8568 : lhs = gimple_assign_lhs (last_stmt);
3786 8568 : oprnd0 = gimple_assign_rhs1 (last_stmt);
3787 8568 : type = TREE_TYPE (oprnd0);
3788 8568 : oprnd1 = gimple_assign_rhs2 (last_stmt);
3789 : }
3790 9707690 : else if (gimple_call_builtin_p (last_stmt, BUILT_IN_BSWAP16))
3791 : {
3792 : /* __builtin_bswap16 (x) is another form of x r>> 8.
3793 : The vectorizer has bswap support, but only if the argument isn't
3794 : promoted. */
3795 206 : lhs = gimple_call_lhs (last_stmt);
3796 206 : oprnd0 = gimple_call_arg (last_stmt, 0);
3797 206 : type = TREE_TYPE (oprnd0);
3798 206 : if (!lhs
3799 206 : || TYPE_PRECISION (TREE_TYPE (lhs)) != 16
3800 206 : || TYPE_PRECISION (type) <= 16
3801 0 : || TREE_CODE (oprnd0) != SSA_NAME
3802 206 : || BITS_PER_UNIT != 8)
3803 206 : return NULL;
3804 :
3805 0 : stmt_vec_info def_stmt_info;
3806 0 : if (!vect_is_simple_use (oprnd0, vinfo, &dt, &def_stmt_info, &def_stmt))
3807 : return NULL;
3808 :
3809 0 : if (dt != vect_internal_def)
3810 : return NULL;
3811 :
3812 0 : if (gimple_assign_cast_p (def_stmt))
3813 : {
3814 0 : def = gimple_assign_rhs1 (def_stmt);
3815 0 : if (INTEGRAL_TYPE_P (TREE_TYPE (def))
3816 0 : && TYPE_PRECISION (TREE_TYPE (def)) == 16)
3817 : oprnd0 = def;
3818 : }
3819 :
3820 0 : type = TREE_TYPE (lhs);
3821 0 : vectype = get_vectype_for_scalar_type (vinfo, type);
3822 0 : if (vectype == NULL_TREE)
3823 : return NULL;
3824 :
3825 0 : if (tree char_vectype = get_same_sized_vectype (char_type_node, vectype))
3826 : {
3827 : /* The encoding uses one stepped pattern for each byte in the
3828 : 16-bit word. */
3829 0 : vec_perm_builder elts (TYPE_VECTOR_SUBPARTS (char_vectype), 2, 3);
3830 0 : for (unsigned i = 0; i < 3; ++i)
3831 0 : for (unsigned j = 0; j < 2; ++j)
3832 0 : elts.quick_push ((i + 1) * 2 - j - 1);
3833 :
3834 0 : vec_perm_indices indices (elts, 1,
3835 0 : TYPE_VECTOR_SUBPARTS (char_vectype));
3836 0 : machine_mode vmode = TYPE_MODE (char_vectype);
3837 0 : if (can_vec_perm_const_p (vmode, vmode, indices))
3838 : {
3839 : /* vectorizable_bswap can handle the __builtin_bswap16 if we
3840 : undo the argument promotion. */
3841 0 : if (!useless_type_conversion_p (type, TREE_TYPE (oprnd0)))
3842 : {
3843 0 : def = vect_recog_temp_ssa_var (type, NULL);
3844 0 : def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
3845 0 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
3846 0 : oprnd0 = def;
3847 : }
3848 :
3849 : /* Pattern detected. */
3850 0 : vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
3851 :
3852 0 : *type_out = vectype;
3853 :
3854 : /* Pattern supported. Create a stmt to be used to replace the
3855 : pattern, with the unpromoted argument. */
3856 0 : var = vect_recog_temp_ssa_var (type, NULL);
3857 0 : pattern_stmt = gimple_build_call (gimple_call_fndecl (last_stmt),
3858 : 1, oprnd0);
3859 0 : gimple_call_set_lhs (pattern_stmt, var);
3860 0 : gimple_call_set_fntype (as_a <gcall *> (pattern_stmt),
3861 : gimple_call_fntype (last_stmt));
3862 0 : return pattern_stmt;
3863 : }
3864 0 : }
3865 :
3866 0 : oprnd1 = build_int_cst (integer_type_node, 8);
3867 0 : rhs_code = LROTATE_EXPR;
3868 0 : bswap16_p = true;
3869 : }
3870 : else
3871 : return NULL;
3872 :
3873 8568 : if (TREE_CODE (oprnd0) != SSA_NAME
3874 8448 : || !INTEGRAL_TYPE_P (type)
3875 16695 : || TYPE_PRECISION (TREE_TYPE (lhs)) != TYPE_PRECISION (type))
3876 : return NULL;
3877 :
3878 8127 : stmt_vec_info def_stmt_info;
3879 8127 : if (!vect_is_simple_use (oprnd1, vinfo, &dt, &def_stmt_info, &def_stmt))
3880 : return NULL;
3881 :
3882 8127 : if (dt != vect_internal_def
3883 7060 : && dt != vect_constant_def
3884 25 : && dt != vect_external_def)
3885 : return NULL;
3886 :
3887 8121 : vectype = get_vectype_for_scalar_type (vinfo, type);
3888 8121 : if (vectype == NULL_TREE)
3889 : return NULL;
3890 :
3891 : /* If vector/vector or vector/scalar rotate is supported by the target,
3892 : don't do anything here. */
3893 7754 : optab1 = optab_for_tree_code (rhs_code, vectype, optab_vector);
3894 7754 : if (optab1
3895 7754 : && can_implement_p (optab1, TYPE_MODE (vectype)))
3896 : {
3897 564 : use_rotate:
3898 564 : if (bswap16_p)
3899 : {
3900 0 : if (!useless_type_conversion_p (type, TREE_TYPE (oprnd0)))
3901 : {
3902 0 : def = vect_recog_temp_ssa_var (type, NULL);
3903 0 : def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
3904 0 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
3905 0 : oprnd0 = def;
3906 : }
3907 :
3908 : /* Pattern detected. */
3909 0 : vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
3910 :
3911 0 : *type_out = vectype;
3912 :
3913 : /* Pattern supported. Create a stmt to be used to replace the
3914 : pattern. */
3915 0 : var = vect_recog_temp_ssa_var (type, NULL);
3916 0 : pattern_stmt = gimple_build_assign (var, LROTATE_EXPR, oprnd0,
3917 : oprnd1);
3918 0 : return pattern_stmt;
3919 : }
3920 : return NULL;
3921 : }
3922 :
3923 7730 : if (is_a <bb_vec_info> (vinfo) || dt != vect_internal_def)
3924 : {
3925 7646 : optab2 = optab_for_tree_code (rhs_code, vectype, optab_scalar);
3926 7646 : if (optab2
3927 7646 : && can_implement_p (optab2, TYPE_MODE (vectype)))
3928 540 : goto use_rotate;
3929 : }
3930 :
3931 : /* We may not use a reduction operand twice. */
3932 7190 : if (vect_is_reduction (stmt_vinfo))
3933 : return NULL;
3934 :
3935 7169 : tree utype = unsigned_type_for (type);
3936 7169 : tree uvectype = get_vectype_for_scalar_type (vinfo, utype);
3937 7169 : if (!uvectype)
3938 : return NULL;
3939 :
3940 : /* If vector/vector or vector/scalar shifts aren't supported by the target,
3941 : don't do anything here either. */
3942 7169 : optab1 = optab_for_tree_code (LSHIFT_EXPR, uvectype, optab_vector);
3943 7169 : optab2 = optab_for_tree_code (RSHIFT_EXPR, uvectype, optab_vector);
3944 7169 : if (!optab1
3945 7169 : || !can_implement_p (optab1, TYPE_MODE (uvectype))
3946 746 : || !optab2
3947 7915 : || !can_implement_p (optab2, TYPE_MODE (uvectype)))
3948 : {
3949 6423 : if (! is_a <bb_vec_info> (vinfo) && dt == vect_internal_def)
3950 : return NULL;
3951 6360 : optab1 = optab_for_tree_code (LSHIFT_EXPR, uvectype, optab_scalar);
3952 6360 : optab2 = optab_for_tree_code (RSHIFT_EXPR, uvectype, optab_scalar);
3953 6360 : if (!optab1
3954 6360 : || !can_implement_p (optab1, TYPE_MODE (uvectype))
3955 4705 : || !optab2
3956 11065 : || !can_implement_p (optab2, TYPE_MODE (uvectype)))
3957 1655 : return NULL;
3958 : }
3959 :
3960 5451 : *type_out = vectype;
3961 :
3962 5451 : if (!useless_type_conversion_p (utype, TREE_TYPE (oprnd0)))
3963 : {
3964 52 : def = vect_recog_temp_ssa_var (utype, NULL);
3965 52 : def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
3966 52 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
3967 52 : oprnd0 = def;
3968 : }
3969 :
3970 5451 : if (dt == vect_external_def && TREE_CODE (oprnd1) == SSA_NAME)
3971 15 : ext_def = vect_get_external_def_edge (vinfo, oprnd1);
3972 :
3973 5451 : def = NULL_TREE;
3974 5451 : scalar_int_mode mode = SCALAR_INT_TYPE_MODE (utype);
3975 5451 : if (dt != vect_internal_def || TYPE_MODE (TREE_TYPE (oprnd1)) == mode)
3976 : def = oprnd1;
3977 124 : else if (def_stmt && gimple_assign_cast_p (def_stmt))
3978 : {
3979 96 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3980 96 : if (TYPE_MODE (TREE_TYPE (rhs1)) == mode
3981 96 : && TYPE_PRECISION (TREE_TYPE (rhs1))
3982 0 : == TYPE_PRECISION (type))
3983 : def = rhs1;
3984 : }
3985 :
3986 5327 : if (def == NULL_TREE)
3987 : {
3988 124 : def = vect_recog_temp_ssa_var (utype, NULL);
3989 124 : def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1);
3990 124 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
3991 : }
3992 5451 : stype = TREE_TYPE (def);
3993 :
3994 5451 : if (TREE_CODE (def) == INTEGER_CST)
3995 : {
3996 4873 : if (!tree_fits_uhwi_p (def)
3997 4873 : || tree_to_uhwi (def) >= GET_MODE_PRECISION (mode)
3998 9746 : || integer_zerop (def))
3999 0 : return NULL;
4000 4873 : def2 = build_int_cst (stype,
4001 4873 : GET_MODE_PRECISION (mode) - tree_to_uhwi (def));
4002 : }
4003 : else
4004 : {
4005 578 : tree vecstype = get_vectype_for_scalar_type (vinfo, stype);
4006 :
4007 578 : if (vecstype == NULL_TREE)
4008 : return NULL;
4009 578 : def2 = vect_recog_temp_ssa_var (stype, NULL);
4010 578 : def_stmt = gimple_build_assign (def2, NEGATE_EXPR, def);
4011 578 : if (ext_def)
4012 : {
4013 15 : basic_block new_bb
4014 15 : = gsi_insert_on_edge_immediate (ext_def, def_stmt);
4015 15 : gcc_assert (!new_bb);
4016 : }
4017 : else
4018 563 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
4019 :
4020 578 : def2 = vect_recog_temp_ssa_var (stype, NULL);
4021 578 : tree mask = build_int_cst (stype, GET_MODE_PRECISION (mode) - 1);
4022 578 : def_stmt = gimple_build_assign (def2, BIT_AND_EXPR,
4023 : gimple_assign_lhs (def_stmt), mask);
4024 578 : if (ext_def)
4025 : {
4026 15 : basic_block new_bb
4027 15 : = gsi_insert_on_edge_immediate (ext_def, def_stmt);
4028 15 : gcc_assert (!new_bb);
4029 : }
4030 : else
4031 563 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
4032 : }
4033 :
4034 5451 : var1 = vect_recog_temp_ssa_var (utype, NULL);
4035 10591 : def_stmt = gimple_build_assign (var1, rhs_code == LROTATE_EXPR
4036 : ? LSHIFT_EXPR : RSHIFT_EXPR,
4037 : oprnd0, def);
4038 5451 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
4039 :
4040 5451 : var2 = vect_recog_temp_ssa_var (utype, NULL);
4041 10591 : def_stmt = gimple_build_assign (var2, rhs_code == LROTATE_EXPR
4042 : ? RSHIFT_EXPR : LSHIFT_EXPR,
4043 : oprnd0, def2);
4044 5451 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
4045 :
4046 : /* Pattern detected. */
4047 5451 : vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
4048 :
4049 : /* Pattern supported. Create a stmt to be used to replace the pattern. */
4050 5451 : var = vect_recog_temp_ssa_var (utype, NULL);
4051 5451 : pattern_stmt = gimple_build_assign (var, BIT_IOR_EXPR, var1, var2);
4052 :
4053 5451 : if (!useless_type_conversion_p (type, utype))
4054 : {
4055 52 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, uvectype);
4056 52 : tree result = vect_recog_temp_ssa_var (type, NULL);
4057 52 : pattern_stmt = gimple_build_assign (result, NOP_EXPR, var);
4058 : }
4059 : return pattern_stmt;
4060 : }
4061 :
4062 : /* Detect a vector by vector shift pattern that wouldn't be otherwise
4063 : vectorized:
4064 :
4065 : type a_t;
4066 : TYPE b_T, res_T;
4067 :
4068 : S1 a_t = ;
4069 : S2 b_T = ;
4070 : S3 res_T = b_T op a_t;
4071 :
4072 : where type 'TYPE' is a type with different size than 'type',
4073 : and op is <<, >> or rotate.
4074 :
4075 : Also detect cases:
4076 :
4077 : type a_t;
4078 : TYPE b_T, c_T, res_T;
4079 :
4080 : S0 c_T = ;
4081 : S1 a_t = (type) c_T;
4082 : S2 b_T = ;
4083 : S3 res_T = b_T op a_t;
4084 :
4085 : Input/Output:
4086 :
4087 : * STMT_VINFO: The stmt from which the pattern search begins,
4088 : i.e. the shift/rotate stmt. The original stmt (S3) is replaced
4089 : with a shift/rotate which has same type on both operands, in the
4090 : second case just b_T op c_T, in the first case with added cast
4091 : from a_t to c_T in STMT_VINFO_PATTERN_DEF_SEQ.
4092 :
4093 : Output:
4094 :
4095 : * TYPE_OUT: The type of the output of this pattern.
4096 :
4097 : * Return value: A new stmt that will be used to replace the shift/rotate
4098 : S3 stmt. */
4099 :
4100 : static gimple *
4101 32157621 : vect_recog_vector_vector_shift_pattern (vec_info *vinfo,
4102 : stmt_vec_info stmt_vinfo,
4103 : tree *type_out)
4104 : {
4105 32157621 : gimple *last_stmt = stmt_vinfo->stmt;
4106 32157621 : tree oprnd0, oprnd1, lhs, var;
4107 32157621 : gimple *pattern_stmt;
4108 32157621 : enum tree_code rhs_code;
4109 :
4110 32157621 : if (!is_gimple_assign (last_stmt))
4111 : return NULL;
4112 :
4113 22449931 : rhs_code = gimple_assign_rhs_code (last_stmt);
4114 22449931 : switch (rhs_code)
4115 : {
4116 517876 : case LSHIFT_EXPR:
4117 517876 : case RSHIFT_EXPR:
4118 517876 : case LROTATE_EXPR:
4119 517876 : case RROTATE_EXPR:
4120 517876 : break;
4121 : default:
4122 : return NULL;
4123 : }
4124 :
4125 517876 : lhs = gimple_assign_lhs (last_stmt);
4126 517876 : oprnd0 = gimple_assign_rhs1 (last_stmt);
4127 517876 : oprnd1 = gimple_assign_rhs2 (last_stmt);
4128 517876 : if (TREE_CODE (oprnd1) != SSA_NAME
4129 110908 : || TYPE_MODE (TREE_TYPE (oprnd0)) == TYPE_MODE (TREE_TYPE (oprnd1))
4130 49490 : || !INTEGRAL_TYPE_P (TREE_TYPE (oprnd0))
4131 48985 : || !type_has_mode_precision_p (TREE_TYPE (oprnd1))
4132 566861 : || TYPE_PRECISION (TREE_TYPE (lhs))
4133 48985 : != TYPE_PRECISION (TREE_TYPE (oprnd0)))
4134 468891 : return NULL;
4135 :
4136 48985 : stmt_vec_info def_vinfo = vinfo->lookup_def (oprnd1);
4137 48985 : if (!def_vinfo || STMT_VINFO_DEF_TYPE (def_vinfo) == vect_external_def)
4138 : return NULL;
4139 :
4140 46135 : def_vinfo = vect_stmt_to_vectorize (def_vinfo);
4141 1138 : gcc_assert (def_vinfo);
4142 :
4143 46135 : *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (oprnd0));
4144 46135 : if (*type_out == NULL_TREE)
4145 : return NULL;
4146 :
4147 33360 : tree def = NULL_TREE;
4148 33360 : gassign *def_stmt = dyn_cast <gassign *> (def_vinfo->stmt);
4149 19598 : if (def_stmt && gimple_assign_cast_p (def_stmt))
4150 : {
4151 5508 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
4152 5508 : if (TYPE_MODE (TREE_TYPE (rhs1)) == TYPE_MODE (TREE_TYPE (oprnd0))
4153 5508 : && TYPE_PRECISION (TREE_TYPE (rhs1))
4154 1271 : == TYPE_PRECISION (TREE_TYPE (oprnd0)))
4155 : {
4156 1271 : if (TYPE_PRECISION (TREE_TYPE (oprnd1))
4157 1271 : >= TYPE_PRECISION (TREE_TYPE (rhs1)))
4158 : def = rhs1;
4159 : else
4160 : {
4161 1184 : tree mask
4162 1184 : = build_low_bits_mask (TREE_TYPE (rhs1),
4163 1184 : TYPE_PRECISION (TREE_TYPE (oprnd1)));
4164 1184 : def = vect_recog_temp_ssa_var (TREE_TYPE (rhs1), NULL);
4165 1184 : def_stmt = gimple_build_assign (def, BIT_AND_EXPR, rhs1, mask);
4166 1184 : tree vecstype = get_vectype_for_scalar_type (vinfo,
4167 1184 : TREE_TYPE (rhs1));
4168 1184 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
4169 : }
4170 : }
4171 : }
4172 :
4173 1271 : if (def == NULL_TREE)
4174 : {
4175 32089 : def = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL);
4176 32089 : def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1);
4177 32089 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
4178 : }
4179 :
4180 : /* Pattern detected. */
4181 33360 : vect_pattern_detected ("vect_recog_vector_vector_shift_pattern", last_stmt);
4182 :
4183 : /* Pattern supported. Create a stmt to be used to replace the pattern. */
4184 33360 : var = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL);
4185 33360 : pattern_stmt = gimple_build_assign (var, rhs_code, oprnd0, def);
4186 :
4187 33360 : return pattern_stmt;
4188 : }
4189 :
4190 : /* Verify that the target has optabs of VECTYPE to perform all the steps
4191 : needed by the multiplication-by-immediate synthesis algorithm described by
4192 : ALG and VAR. If SYNTH_SHIFT_P is true ensure that vector addition is
4193 : present. Return true iff the target supports all the steps. */
4194 :
4195 : static bool
4196 301630 : target_supports_mult_synth_alg (struct algorithm *alg, mult_variant var,
4197 : tree vectype, bool synth_shift_p)
4198 : {
4199 301630 : if (alg->op[0] != alg_zero && alg->op[0] != alg_m)
4200 : return false;
4201 :
4202 301630 : bool supports_vminus = target_has_vecop_for_code (MINUS_EXPR, vectype);
4203 301630 : bool supports_vplus = target_has_vecop_for_code (PLUS_EXPR, vectype);
4204 :
4205 301630 : if (var == negate_variant
4206 301630 : && !target_has_vecop_for_code (NEGATE_EXPR, vectype))
4207 : return false;
4208 :
4209 : /* If we must synthesize shifts with additions make sure that vector
4210 : addition is available. */
4211 301026 : if ((var == add_variant || synth_shift_p) && !supports_vplus)
4212 : return false;
4213 :
4214 144283 : for (int i = 1; i < alg->ops; i++)
4215 : {
4216 107960 : switch (alg->op[i])
4217 : {
4218 : case alg_shift:
4219 : break;
4220 26607 : case alg_add_t_m2:
4221 26607 : case alg_add_t2_m:
4222 26607 : case alg_add_factor:
4223 26607 : if (!supports_vplus)
4224 : return false;
4225 : break;
4226 16469 : case alg_sub_t_m2:
4227 16469 : case alg_sub_t2_m:
4228 16469 : case alg_sub_factor:
4229 16469 : if (!supports_vminus)
4230 : return false;
4231 : break;
4232 : case alg_unknown:
4233 : case alg_m:
4234 : case alg_zero:
4235 : case alg_impossible:
4236 : return false;
4237 0 : default:
4238 0 : gcc_unreachable ();
4239 : }
4240 : }
4241 :
4242 : return true;
4243 : }
4244 :
4245 : /* Synthesize a left shift of OP by AMNT bits using a series of additions and
4246 : putting the final result in DEST. Append all statements but the last into
4247 : VINFO. Return the last statement. */
4248 :
4249 : static gimple *
4250 0 : synth_lshift_by_additions (vec_info *vinfo,
4251 : tree dest, tree op, HOST_WIDE_INT amnt,
4252 : stmt_vec_info stmt_info, tree vectype)
4253 : {
4254 0 : HOST_WIDE_INT i;
4255 0 : tree itype = TREE_TYPE (op);
4256 0 : tree prev_res = op;
4257 0 : gcc_assert (amnt >= 0);
4258 0 : for (i = 0; i < amnt; i++)
4259 : {
4260 0 : tree tmp_var = (i < amnt - 1) ? vect_recog_temp_ssa_var (itype, NULL)
4261 : : dest;
4262 0 : gimple *stmt
4263 0 : = gimple_build_assign (tmp_var, PLUS_EXPR, prev_res, prev_res);
4264 0 : prev_res = tmp_var;
4265 0 : if (i < amnt - 1)
4266 0 : append_pattern_def_seq (vinfo, stmt_info, stmt, vectype);
4267 : else
4268 0 : return stmt;
4269 : }
4270 0 : gcc_unreachable ();
4271 : return NULL;
4272 : }
4273 :
4274 : /* Helper for vect_synth_mult_by_constant. Apply a binary operation
4275 : CODE to operands OP1 and OP2, creating a new temporary SSA var in
4276 : the process if necessary. Append the resulting assignment statements
4277 : to the sequence in STMT_VINFO. Return the SSA variable that holds the
4278 : result of the binary operation. If SYNTH_SHIFT_P is true synthesize
4279 : left shifts using additions. */
4280 :
4281 : static tree
4282 42974 : apply_binop_and_append_stmt (vec_info *vinfo,
4283 : tree_code code, tree op1, tree op2,
4284 : stmt_vec_info stmt_vinfo, tree vectype,
4285 : bool synth_shift_p)
4286 : {
4287 42974 : if (integer_zerop (op2)
4288 42974 : && (code == LSHIFT_EXPR
4289 37202 : || code == PLUS_EXPR))
4290 : {
4291 37202 : gcc_assert (TREE_CODE (op1) == SSA_NAME);
4292 : return op1;
4293 : }
4294 :
4295 5772 : gimple *stmt;
4296 5772 : tree itype = TREE_TYPE (op1);
4297 5772 : tree tmp_var = vect_recog_temp_ssa_var (itype, NULL);
4298 :
4299 5772 : if (code == LSHIFT_EXPR
4300 5772 : && synth_shift_p)
4301 : {
4302 0 : stmt = synth_lshift_by_additions (vinfo, tmp_var, op1,
4303 0 : TREE_INT_CST_LOW (op2), stmt_vinfo,
4304 : vectype);
4305 0 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4306 0 : return tmp_var;
4307 : }
4308 :
4309 5772 : stmt = gimple_build_assign (tmp_var, code, op1, op2);
4310 5772 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4311 5772 : return tmp_var;
4312 : }
4313 :
4314 : /* Synthesize a multiplication of OP by an INTEGER_CST VAL using shifts
4315 : and simple arithmetic operations to be vectorized. Record the statements
4316 : produced in STMT_VINFO and return the last statement in the sequence or
4317 : NULL if it's not possible to synthesize such a multiplication.
4318 : This function mirrors the behavior of expand_mult_const in expmed.cc but
4319 : works on tree-ssa form. */
4320 :
4321 : static gimple *
4322 304413 : vect_synth_mult_by_constant (vec_info *vinfo, tree op, tree val,
4323 : stmt_vec_info stmt_vinfo)
4324 : {
4325 304413 : tree itype = TREE_TYPE (op);
4326 304413 : machine_mode mode = TYPE_MODE (itype);
4327 304413 : struct algorithm alg;
4328 304413 : mult_variant variant;
4329 304413 : if (!tree_fits_shwi_p (val))
4330 : return NULL;
4331 :
4332 : /* Multiplication synthesis by shifts, adds and subs can introduce
4333 : signed overflow where the original operation didn't. Perform the
4334 : operations on an unsigned type and cast back to avoid this.
4335 : In the future we may want to relax this for synthesis algorithms
4336 : that we can prove do not cause unexpected overflow. */
4337 301645 : bool cast_to_unsigned_p = !TYPE_OVERFLOW_WRAPS (itype);
4338 :
4339 59290 : tree multtype = cast_to_unsigned_p ? unsigned_type_for (itype) : itype;
4340 301645 : tree vectype = get_vectype_for_scalar_type (vinfo, multtype);
4341 301645 : if (!vectype)
4342 : return NULL;
4343 :
4344 : /* Targets that don't support vector shifts but support vector additions
4345 : can synthesize shifts that way. */
4346 301645 : bool synth_shift_p = !vect_supportable_shift (vinfo, LSHIFT_EXPR, multtype);
4347 :
4348 301645 : HOST_WIDE_INT hwval = tree_to_shwi (val);
4349 : /* Use MAX_COST here as we don't want to limit the sequence on rtx costs.
4350 : The vectorizer's benefit analysis will decide whether it's beneficial
4351 : to do this. */
4352 603290 : bool possible = choose_mult_variant (VECTOR_MODE_P (TYPE_MODE (vectype))
4353 301645 : ? TYPE_MODE (vectype) : mode,
4354 : hwval, &alg, &variant, MAX_COST);
4355 301645 : if (!possible)
4356 : return NULL;
4357 :
4358 301645 : if (vect_is_reduction (stmt_vinfo))
4359 : {
4360 26 : int op_uses = alg.op[0] != alg_zero;
4361 45 : for (int i = 1; i < alg.ops; i++)
4362 32 : switch (alg.op[i])
4363 : {
4364 4 : case alg_add_t_m2:
4365 4 : case alg_sub_t_m2:
4366 4 : if (synth_shift_p && alg.log[i])
4367 : return NULL;
4368 : else
4369 4 : op_uses++;
4370 4 : break;
4371 0 : case alg_add_t2_m:
4372 0 : case alg_sub_t2_m:
4373 0 : op_uses++;
4374 : /* Fallthru. */
4375 28 : case alg_shift:
4376 28 : if (synth_shift_p && alg.log[i])
4377 : return NULL;
4378 : break;
4379 : case alg_add_factor:
4380 : case alg_sub_factor:
4381 : return NULL;
4382 : default:
4383 : break;
4384 : }
4385 13 : if (variant == add_variant)
4386 0 : op_uses++;
4387 : /* When we'll synthesize more than a single use of the reduction
4388 : operand the reduction constraints are violated. Avoid this
4389 : situation. */
4390 13 : if (op_uses > 1)
4391 : return NULL;
4392 : }
4393 :
4394 301630 : if (!target_supports_mult_synth_alg (&alg, variant, vectype, synth_shift_p))
4395 : return NULL;
4396 :
4397 36323 : tree accumulator;
4398 :
4399 : /* Clear out the sequence of statements so we can populate it below. */
4400 36323 : gimple *stmt = NULL;
4401 :
4402 36323 : if (cast_to_unsigned_p)
4403 : {
4404 12162 : tree tmp_op = vect_recog_temp_ssa_var (multtype, NULL);
4405 12162 : stmt = gimple_build_assign (tmp_op, CONVERT_EXPR, op);
4406 12162 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4407 12162 : op = tmp_op;
4408 : }
4409 :
4410 36323 : if (alg.op[0] == alg_zero)
4411 205 : accumulator = build_int_cst (multtype, 0);
4412 : else
4413 : accumulator = op;
4414 :
4415 36323 : bool needs_fixup = (variant == negate_variant)
4416 36323 : || (variant == add_variant);
4417 :
4418 144112 : for (int i = 1; i < alg.ops; i++)
4419 : {
4420 107789 : tree shft_log = build_int_cst (multtype, alg.log[i]);
4421 107789 : tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
4422 107789 : tree tmp_var = NULL_TREE;
4423 :
4424 107789 : switch (alg.op[i])
4425 : {
4426 64815 : case alg_shift:
4427 64815 : if (synth_shift_p)
4428 0 : stmt
4429 0 : = synth_lshift_by_additions (vinfo, accum_tmp, accumulator,
4430 0 : alg.log[i], stmt_vinfo, vectype);
4431 : else
4432 64815 : stmt = gimple_build_assign (accum_tmp, LSHIFT_EXPR, accumulator,
4433 : shft_log);
4434 : break;
4435 21756 : case alg_add_t_m2:
4436 21756 : tmp_var
4437 21756 : = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, op, shft_log,
4438 : stmt_vinfo, vectype, synth_shift_p);
4439 21756 : stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator,
4440 : tmp_var);
4441 21756 : break;
4442 15649 : case alg_sub_t_m2:
4443 15649 : tmp_var = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, op,
4444 : shft_log, stmt_vinfo,
4445 : vectype, synth_shift_p);
4446 : /* In some algorithms the first step involves zeroing the
4447 : accumulator. If subtracting from such an accumulator
4448 : just emit the negation directly. */
4449 15649 : if (integer_zerop (accumulator))
4450 205 : stmt = gimple_build_assign (accum_tmp, NEGATE_EXPR, tmp_var);
4451 : else
4452 15444 : stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, accumulator,
4453 : tmp_var);
4454 : break;
4455 0 : case alg_add_t2_m:
4456 0 : tmp_var
4457 0 : = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
4458 : shft_log, stmt_vinfo, vectype,
4459 : synth_shift_p);
4460 0 : stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, tmp_var, op);
4461 0 : break;
4462 0 : case alg_sub_t2_m:
4463 0 : tmp_var
4464 0 : = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
4465 : shft_log, stmt_vinfo, vectype,
4466 : synth_shift_p);
4467 0 : stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, tmp_var, op);
4468 0 : break;
4469 4787 : case alg_add_factor:
4470 4787 : tmp_var
4471 4787 : = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
4472 : shft_log, stmt_vinfo, vectype,
4473 : synth_shift_p);
4474 4787 : stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator,
4475 : tmp_var);
4476 4787 : break;
4477 782 : case alg_sub_factor:
4478 782 : tmp_var
4479 782 : = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
4480 : shft_log, stmt_vinfo, vectype,
4481 : synth_shift_p);
4482 782 : stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, tmp_var,
4483 : accumulator);
4484 782 : break;
4485 0 : default:
4486 0 : gcc_unreachable ();
4487 : }
4488 : /* We don't want to append the last stmt in the sequence to stmt_vinfo
4489 : but rather return it directly. */
4490 :
4491 107789 : if ((i < alg.ops - 1) || needs_fixup || cast_to_unsigned_p)
4492 83929 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4493 107789 : accumulator = accum_tmp;
4494 : }
4495 36323 : if (variant == negate_variant)
4496 : {
4497 441 : tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
4498 441 : stmt = gimple_build_assign (accum_tmp, NEGATE_EXPR, accumulator);
4499 441 : accumulator = accum_tmp;
4500 441 : if (cast_to_unsigned_p)
4501 150 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4502 : }
4503 35882 : else if (variant == add_variant)
4504 : {
4505 101 : tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
4506 101 : stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator, op);
4507 101 : accumulator = accum_tmp;
4508 101 : if (cast_to_unsigned_p)
4509 91 : append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
4510 : }
4511 : /* Move back to a signed if needed. */
4512 36022 : if (cast_to_unsigned_p)
4513 : {
4514 12162 : tree accum_tmp = vect_recog_temp_ssa_var (itype, NULL);
4515 12162 : stmt = gimple_build_assign (accum_tmp, CONVERT_EXPR, accumulator);
4516 : }
4517 :
4518 : return stmt;
4519 : }
4520 :
4521 : /* Detect multiplication by constant and convert it into a sequence of
4522 : shifts and additions, subtractions, negations. We reuse the
4523 : choose_mult_variant algorithms from expmed.cc
4524 :
4525 : Input/Output:
4526 :
4527 : STMT_VINFO: The stmt from which the pattern search begins,
4528 : i.e. the mult stmt.
4529 :
4530 : Output:
4531 :
4532 : * TYPE_OUT: The type of the output of this pattern.
4533 :
4534 : * Return value: A new stmt that will be used to replace
4535 : the multiplication. */
4536 :
4537 : static gimple *
4538 32356546 : vect_recog_mult_pattern (vec_info *vinfo,
4539 : stmt_vec_info stmt_vinfo, tree *type_out)
4540 : {
4541 32356546 : gimple *last_stmt = stmt_vinfo->stmt;
4542 32356546 : tree oprnd0, oprnd1, vectype, itype;
4543 32356546 : gimple *pattern_stmt;
4544 :
4545 32356546 : if (!is_gimple_assign (last_stmt))
4546 : return NULL;
4547 :
4548 22648856 : if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR)
4549 : return NULL;
4550 :
4551 1537197 : oprnd0 = gimple_assign_rhs1 (last_stmt);
4552 1537197 : oprnd1 = gimple_assign_rhs2 (last_stmt);
4553 1537197 : itype = TREE_TYPE (oprnd0);
4554 :
4555 1537197 : if (TREE_CODE (oprnd0) != SSA_NAME
4556 1537124 : || TREE_CODE (oprnd1) != INTEGER_CST
4557 917399 : || !INTEGRAL_TYPE_P (itype)
4558 2454596 : || !type_has_mode_precision_p (itype))
4559 619853 : return NULL;
4560 :
4561 917344 : vectype = get_vectype_for_scalar_type (vinfo, itype);
4562 917344 : if (vectype == NULL_TREE)
4563 : return NULL;
4564 :
4565 : /* If the target can handle vectorized multiplication natively,
4566 : don't attempt to optimize this. */
4567 744741 : optab mul_optab = optab_for_tree_code (MULT_EXPR, vectype, optab_default);
4568 744741 : if (mul_optab != unknown_optab
4569 744741 : && can_implement_p (mul_optab, TYPE_MODE (vectype)))
4570 : return NULL;
4571 :
4572 304413 : pattern_stmt = vect_synth_mult_by_constant (vinfo,
4573 : oprnd0, oprnd1, stmt_vinfo);
4574 304413 : if (!pattern_stmt)
4575 : return NULL;
4576 :
4577 : /* Pattern detected. */
4578 36323 : vect_pattern_detected ("vect_recog_mult_pattern", last_stmt);
4579 :
4580 36323 : *type_out = vectype;
4581 :
4582 36323 : return pattern_stmt;
4583 : }
4584 :
4585 : extern bool gimple_unsigned_integer_sat_add (tree, tree*, tree (*)(tree));
4586 : extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree));
4587 : extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree));
4588 :
4589 : extern bool gimple_unsigned_integer_narrow_clip (tree, tree*, tree (*)(tree));
4590 :
4591 : extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree));
4592 : extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree));
4593 : extern bool gimple_signed_integer_sat_trunc (tree, tree*, tree (*)(tree));
4594 :
4595 : static gimple *
4596 301 : vect_recog_build_binary_gimple_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
4597 : internal_fn fn, tree *type_out,
4598 : tree lhs, tree op_0, tree op_1)
4599 : {
4600 301 : tree itype = TREE_TYPE (op_0);
4601 301 : tree otype = TREE_TYPE (lhs);
4602 301 : tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
4603 301 : tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
4604 :
4605 301 : if (v_itype != NULL_TREE && v_otype != NULL_TREE
4606 301 : && direct_internal_fn_supported_p (fn, v_itype, OPTIMIZE_FOR_BOTH))
4607 : {
4608 97 : gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1);
4609 97 : tree in_ssa = vect_recog_temp_ssa_var (itype, NULL);
4610 :
4611 97 : gimple_call_set_lhs (call, in_ssa);
4612 97 : gimple_call_set_nothrow (call, /* nothrow_p */ false);
4613 97 : gimple_set_location (call, gimple_location (STMT_VINFO_STMT (stmt_info)));
4614 :
4615 97 : *type_out = v_otype;
4616 :
4617 97 : if (types_compatible_p (itype, otype))
4618 : return call;
4619 : else
4620 : {
4621 0 : append_pattern_def_seq (vinfo, stmt_info, call, v_itype);
4622 0 : tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
4623 :
4624 0 : return gimple_build_assign (out_ssa, NOP_EXPR, in_ssa);
4625 : }
4626 : }
4627 :
4628 : return NULL;
4629 : }
4630 :
4631 : /*
4632 : * Try to detect saturation add pattern (SAT_ADD), aka below gimple:
4633 : * _7 = _4 + _6;
4634 : * _8 = _4 > _7;
4635 : * _9 = (long unsigned int) _8;
4636 : * _10 = -_9;
4637 : * _12 = _7 | _10;
4638 : *
4639 : * And then simplified to
4640 : * _12 = .SAT_ADD (_4, _6);
4641 : */
4642 :
4643 : static gimple *
4644 32427424 : vect_recog_sat_add_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
4645 : tree *type_out)
4646 : {
4647 32427424 : gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
4648 :
4649 32427424 : if (!is_gimple_assign (last_stmt))
4650 : return NULL;
4651 :
4652 22719734 : tree ops[2];
4653 22719734 : tree lhs = gimple_assign_lhs (last_stmt);
4654 :
4655 22719734 : if (gimple_unsigned_integer_sat_add (lhs, ops, NULL)
4656 22719734 : || gimple_signed_integer_sat_add (lhs, ops, NULL))
4657 : {
4658 63 : if (TREE_CODE (ops[1]) == INTEGER_CST)
4659 13 : ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
4660 :
4661 63 : gimple *stmt = vect_recog_build_binary_gimple_stmt (vinfo, stmt_vinfo,
4662 : IFN_SAT_ADD, type_out,
4663 : lhs, ops[0], ops[1]);
4664 63 : if (stmt)
4665 : {
4666 44 : vect_pattern_detected ("vect_recog_sat_add_pattern", last_stmt);
4667 44 : return stmt;
4668 : }
4669 : }
4670 :
4671 : return NULL;
4672 : }
4673 :
4674 : /*
4675 : * Try to transform the truncation for .SAT_SUB pattern, mostly occurs in
4676 : * the benchmark zip. Aka:
4677 : *
4678 : * unsigned int _1;
4679 : * unsigned int _2;
4680 : * unsigned short int _4;
4681 : * _9 = (unsigned short int).SAT_SUB (_1, _2);
4682 : *
4683 : * if _1 is known to be in the range of unsigned short int. For example
4684 : * there is a def _1 = (unsigned short int)_4. Then we can transform the
4685 : * truncation to:
4686 : *
4687 : * _3 = (unsigned short int) MIN (65535, _2); // aka _3 = .SAT_TRUNC (_2);
4688 : * _9 = .SAT_SUB (_4, _3);
4689 : *
4690 : * Then, we can better vectorized code and avoid the unnecessary narrowing
4691 : * stmt during vectorization with below stmt(s).
4692 : *
4693 : * _3 = .SAT_TRUNC(_2); // SI => HI
4694 : * _9 = .SAT_SUB (_4, _3);
4695 : */
4696 : static void
4697 238 : vect_recog_sat_sub_pattern_transform (vec_info *vinfo,
4698 : stmt_vec_info stmt_vinfo,
4699 : tree lhs, tree *ops)
4700 : {
4701 238 : tree otype = TREE_TYPE (lhs);
4702 238 : tree itype = TREE_TYPE (ops[0]);
4703 238 : unsigned itype_prec = TYPE_PRECISION (itype);
4704 238 : unsigned otype_prec = TYPE_PRECISION (otype);
4705 :
4706 238 : if (types_compatible_p (otype, itype) || otype_prec >= itype_prec)
4707 238 : return;
4708 :
4709 0 : tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
4710 0 : tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
4711 0 : tree_pair v_pair = tree_pair (v_otype, v_itype);
4712 :
4713 0 : if (v_otype == NULL_TREE || v_itype == NULL_TREE
4714 0 : || !direct_internal_fn_supported_p (IFN_SAT_TRUNC, v_pair,
4715 : OPTIMIZE_FOR_BOTH))
4716 0 : return;
4717 :
4718 : /* 1. Find the _4 and update ops[0] as above example. */
4719 0 : vect_unpromoted_value unprom;
4720 0 : tree tmp = vect_look_through_possible_promotion (vinfo, ops[0], &unprom);
4721 :
4722 0 : if (tmp == NULL_TREE || TYPE_PRECISION (unprom.type) != otype_prec)
4723 : return;
4724 :
4725 0 : ops[0] = tmp;
4726 :
4727 : /* 2. Generate _3 = .SAT_TRUNC (_2) and update ops[1] as above example. */
4728 0 : tree trunc_lhs_ssa = vect_recog_temp_ssa_var (otype, NULL);
4729 0 : gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[1]);
4730 :
4731 0 : gimple_call_set_lhs (call, trunc_lhs_ssa);
4732 0 : gimple_call_set_nothrow (call, /* nothrow_p */ false);
4733 0 : append_pattern_def_seq (vinfo, stmt_vinfo, call, v_otype);
4734 :
4735 0 : ops[1] = trunc_lhs_ssa;
4736 : }
4737 :
4738 : /*
4739 : * Try to detect saturation sub pattern (SAT_ADD), aka below gimple:
4740 : * Unsigned:
4741 : * _7 = _1 >= _2;
4742 : * _8 = _1 - _2;
4743 : * _10 = (long unsigned int) _7;
4744 : * _9 = _8 * _10;
4745 : *
4746 : * And then simplified to
4747 : * _9 = .SAT_SUB (_1, _2);
4748 : *
4749 : * Signed:
4750 : * x.0_4 = (unsigned char) x_16;
4751 : * y.1_5 = (unsigned char) y_18;
4752 : * _6 = x.0_4 - y.1_5;
4753 : * minus_19 = (int8_t) _6;
4754 : * _7 = x_16 ^ y_18;
4755 : * _8 = x_16 ^ minus_19;
4756 : * _44 = _7 < 0;
4757 : * _23 = x_16 < 0;
4758 : * _24 = (signed char) _23;
4759 : * _58 = (unsigned char) _24;
4760 : * _59 = -_58;
4761 : * _25 = (signed char) _59;
4762 : * _26 = _25 ^ 127;
4763 : * _42 = _8 < 0;
4764 : * _41 = _42 & _44;
4765 : * iftmp.2_11 = _41 ? _26 : minus_19;
4766 : *
4767 : * And then simplified to
4768 : * iftmp.2_11 = .SAT_SUB (x_16, y_18);
4769 : */
4770 :
4771 : static gimple *
4772 32427380 : vect_recog_sat_sub_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
4773 : tree *type_out)
4774 : {
4775 32427380 : gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
4776 :
4777 32427380 : if (!is_gimple_assign (last_stmt))
4778 : return NULL;
4779 :
4780 22719690 : tree ops[2];
4781 22719690 : tree lhs = gimple_assign_lhs (last_stmt);
4782 :
4783 22719690 : if (gimple_unsigned_integer_sat_sub (lhs, ops, NULL)
4784 22719690 : || gimple_signed_integer_sat_sub (lhs, ops, NULL))
4785 : {
4786 238 : vect_recog_sat_sub_pattern_transform (vinfo, stmt_vinfo, lhs, ops);
4787 238 : gimple *stmt = vect_recog_build_binary_gimple_stmt (vinfo, stmt_vinfo,
4788 : IFN_SAT_SUB, type_out,
4789 : lhs, ops[0], ops[1]);
4790 238 : if (stmt)
4791 : {
4792 53 : vect_pattern_detected ("vect_recog_sat_sub_pattern", last_stmt);
4793 53 : return stmt;
4794 : }
4795 : }
4796 :
4797 : return NULL;
4798 : }
4799 :
4800 : /*
4801 : * Try to detect saturation truncation pattern (SAT_TRUNC), aka below gimple:
4802 : * overflow_5 = x_4(D) > 4294967295;
4803 : * _1 = (unsigned int) x_4(D);
4804 : * _2 = (unsigned int) overflow_5;
4805 : * _3 = -_2;
4806 : * _6 = _1 | _3;
4807 : *
4808 : * And then simplified to
4809 : * _6 = .SAT_TRUNC (x_4(D));
4810 : */
4811 :
4812 : static gimple *
4813 32427327 : vect_recog_sat_trunc_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
4814 : tree *type_out)
4815 : {
4816 32427327 : gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
4817 :
4818 32427327 : if (!is_gimple_assign (last_stmt))
4819 : return NULL;
4820 :
4821 22719637 : tree ops[1];
4822 22719637 : tree lhs = gimple_assign_lhs (last_stmt);
4823 22719637 : tree otype = TREE_TYPE (lhs);
4824 :
4825 22719637 : if ((gimple_unsigned_integer_narrow_clip (lhs, ops, NULL))
4826 22719637 : && type_has_mode_precision_p (otype))
4827 : {
4828 16 : tree itype = TREE_TYPE (ops[0]);
4829 16 : tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
4830 16 : tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
4831 16 : internal_fn fn = IFN_SAT_TRUNC;
4832 :
4833 16 : if (v_itype != NULL_TREE && v_otype != NULL_TREE
4834 32 : && direct_internal_fn_supported_p (fn, tree_pair (v_otype, v_itype),
4835 : OPTIMIZE_FOR_BOTH))
4836 : {
4837 0 : tree temp = vect_recog_temp_ssa_var (itype, NULL);
4838 0 : gimple * max_stmt = gimple_build_assign (temp, build2 (MAX_EXPR, itype, build_zero_cst(itype), ops[0]));
4839 0 : append_pattern_def_seq (vinfo, stmt_vinfo, max_stmt, v_itype);
4840 :
4841 0 : gcall *call = gimple_build_call_internal (fn, 1, temp);
4842 0 : tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
4843 :
4844 0 : gimple_call_set_lhs (call, out_ssa);
4845 0 : gimple_call_set_nothrow (call, /* nothrow_p */ false);
4846 0 : gimple_set_location (call, gimple_location (last_stmt));
4847 :
4848 0 : *type_out = v_otype;
4849 :
4850 0 : return call;
4851 : }
4852 :
4853 : }
4854 :
4855 22719637 : if ((gimple_unsigned_integer_sat_trunc (lhs, ops, NULL)
4856 22719314 : || gimple_signed_integer_sat_trunc (lhs, ops, NULL))
4857 22719637 : && type_has_mode_precision_p (otype))
4858 : {
4859 311 : tree itype = TREE_TYPE (ops[0]);
4860 311 : tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
4861 311 : tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
4862 311 : internal_fn fn = IFN_SAT_TRUNC;
4863 :
4864 305 : if (v_itype != NULL_TREE && v_otype != NULL_TREE
4865 616 : && direct_internal_fn_supported_p (fn, tree_pair (v_otype, v_itype),
4866 : OPTIMIZE_FOR_BOTH))
4867 : {
4868 0 : gcall *call = gimple_build_call_internal (fn, 1, ops[0]);
4869 0 : tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
4870 :
4871 0 : gimple_call_set_lhs (call, out_ssa);
4872 0 : gimple_call_set_nothrow (call, /* nothrow_p */ false);
4873 0 : gimple_set_location (call, gimple_location (last_stmt));
4874 :
4875 0 : *type_out = v_otype;
4876 :
4877 0 : return call;
4878 : }
4879 : }
4880 :
4881 : return NULL;
4882 : }
4883 :
4884 :
4885 : /* Function add_code_for_floorceilround_divmod
4886 : A helper function to add compensation code for implementing FLOOR_MOD_EXPR,
4887 : FLOOR_DIV_EXPR, CEIL_MOD_EXPR, CEIL_DIV_EXPR, ROUND_MOD_EXPR and
4888 : ROUND_DIV_EXPR
4889 : The quotient and remainder are needed for implemented these operators.
4890 : FLOOR cases
4891 : r = x %[fl] y; r = x/[fl] y;
4892 : is
4893 : r = x % y; if (r && (x ^ y) < 0) r += y;
4894 : r = x % y; d = x/y; if (r && (x ^ y) < 0) d--; Respectively
4895 : Produce following sequence
4896 : v0 = x^y
4897 : v1 = -r
4898 : v2 = r | -r
4899 : v3 = v0 & v2
4900 : v4 = v3 < 0
4901 : if (floor_mod)
4902 : v5 = v4 ? y : 0
4903 : v6 = r + v5
4904 : if (floor_div)
4905 : v5 = v4 ? 1 : 0
4906 : v6 = d - 1
4907 : Similar sequences of vector instructions are produces for following cases
4908 : CEIL cases
4909 : r = x %[cl] y; r = x/[cl] y;
4910 : is
4911 : r = x % y; if (r && (x ^ y) >= 0) r -= y;
4912 : r = x % y; if (r) r -= y; (unsigned)
4913 : r = x % y; d = x/y; if (r && (x ^ y) >= 0) d++;
4914 : r = x % y; d = x/y; if (r) d++; (unsigned)
4915 : ROUND cases
4916 : r = x %[rd] y; r = x/[rd] y;
4917 : is
4918 : r = x % y; if (r > ((y-1)/2)) if ((x ^ y) >= 0) r -= y; else r += y;
4919 : r = x % y; if (r > ((y-1)/2)) r -= y; (unsigned)
4920 : r = x % y; d = x/y; if (r > ((y-1)/2)) if ((x ^ y) >= 0) d++; else d--;
4921 : r = x % y; d = x/y; if (r > ((y-1)/2)) d++; (unsigned)
4922 : Inputs:
4923 : VECTYPE: Vector type of the operands
4924 : STMT_VINFO: Statement where pattern begins
4925 : RHS_CODE: Should either be FLOOR_MOD_EXPR or FLOOR_DIV_EXPR
4926 : Q: The quotient of division
4927 : R: Remainder of division
4928 : OPRDN0/OPRND1: Actual operands involved
4929 : ITYPE: tree type of oprnd0
4930 : Output:
4931 : NULL if vectorization not possible
4932 : Gimple statement based on rhs_code
4933 : */
4934 : static gimple *
4935 431 : add_code_for_floorceilround_divmod (tree vectype, vec_info *vinfo,
4936 : stmt_vec_info stmt_vinfo,
4937 : enum tree_code rhs_code, tree q, tree r,
4938 : tree oprnd0, tree oprnd1, tree itype)
4939 : {
4940 431 : gimple *def_stmt;
4941 431 : tree mask_vectype = truth_type_for (vectype);
4942 431 : if (!mask_vectype)
4943 : return NULL;
4944 431 : tree bool_cond;
4945 431 : bool unsigned_p = TYPE_UNSIGNED (itype);
4946 :
4947 431 : switch (rhs_code)
4948 : {
4949 395 : case FLOOR_MOD_EXPR:
4950 395 : case FLOOR_DIV_EXPR:
4951 395 : case CEIL_MOD_EXPR:
4952 395 : case CEIL_DIV_EXPR:
4953 395 : {
4954 395 : if (!target_has_vecop_for_code (NEGATE_EXPR, vectype)
4955 363 : || !target_has_vecop_for_code (BIT_XOR_EXPR, vectype)
4956 363 : || !target_has_vecop_for_code (BIT_IOR_EXPR, vectype)
4957 363 : || !target_has_vecop_for_code (PLUS_EXPR, vectype)
4958 363 : || !target_has_vecop_for_code (MINUS_EXPR, vectype)
4959 363 : || !expand_vec_cmp_expr_p (vectype, mask_vectype, LT_EXPR)
4960 631 : || !expand_vec_cond_expr_p (vectype, mask_vectype))
4961 159 : return NULL;
4962 236 : if (unsigned_p)
4963 : {
4964 18 : gcc_assert (rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR);
4965 :
4966 18 : if (!expand_vec_cmp_expr_p (vectype, mask_vectype, GT_EXPR))
4967 : return NULL;
4968 18 : bool is_mod = rhs_code == CEIL_MOD_EXPR;
4969 : // r > 0
4970 18 : bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
4971 18 : def_stmt = gimple_build_assign (bool_cond, GT_EXPR, r,
4972 : build_int_cst (itype, 0));
4973 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
4974 : itype);
4975 :
4976 : // (r > 0) ? y : 0 (mod)
4977 : // (r > 0) ? 1 : 0 (ceil)
4978 18 : tree extr_cond = vect_recog_temp_ssa_var (itype, NULL);
4979 18 : def_stmt
4980 27 : = gimple_build_assign (extr_cond, COND_EXPR, bool_cond,
4981 9 : is_mod ? oprnd1 : build_int_cst (itype, 1),
4982 : build_int_cst (itype, 0));
4983 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
4984 :
4985 : // r -= (r > 0) ? y : 0 (mod)
4986 : // d += (x^y < 0 && r) ? -1 : 0 (ceil)
4987 18 : tree result = vect_recog_temp_ssa_var (itype, NULL);
4988 27 : return gimple_build_assign (result, is_mod ? MINUS_EXPR : PLUS_EXPR,
4989 18 : is_mod ? r : q, extr_cond);
4990 : }
4991 : else
4992 : {
4993 218 : bool ceil_p
4994 218 : = (rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR);
4995 218 : if (ceil_p && !target_has_vecop_for_code (BIT_NOT_EXPR, vectype))
4996 : return NULL;
4997 : // x ^ y
4998 218 : tree xort = vect_recog_temp_ssa_var (itype, NULL);
4999 218 : def_stmt = gimple_build_assign (xort, BIT_XOR_EXPR, oprnd0, oprnd1);
5000 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5001 :
5002 218 : tree cond_reg = xort;
5003 : // ~(x ^ y) (ceil)
5004 218 : if (ceil_p)
5005 : {
5006 18 : cond_reg = vect_recog_temp_ssa_var (itype, NULL);
5007 18 : def_stmt = gimple_build_assign (cond_reg, BIT_NOT_EXPR, xort);
5008 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5009 : }
5010 :
5011 : // -r
5012 218 : tree negate_r = vect_recog_temp_ssa_var (itype, NULL);
5013 218 : def_stmt = gimple_build_assign (negate_r, NEGATE_EXPR, r);
5014 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5015 :
5016 : // r | -r , sign bit is set if r!=0
5017 218 : tree r_or_negr = vect_recog_temp_ssa_var (itype, NULL);
5018 218 : def_stmt
5019 218 : = gimple_build_assign (r_or_negr, BIT_IOR_EXPR, r, negate_r);
5020 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5021 :
5022 : // (x ^ y) & (r | -r)
5023 : // ~(x ^ y) & (r | -r) (ceil)
5024 218 : tree r_or_negr_and_xor = vect_recog_temp_ssa_var (itype, NULL);
5025 218 : def_stmt = gimple_build_assign (r_or_negr_and_xor, BIT_AND_EXPR,
5026 : r_or_negr, cond_reg);
5027 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5028 :
5029 : // (x ^ y) & (r | -r) < 0 which is equivalent to (x^y < 0 && r!=0)
5030 218 : bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5031 218 : def_stmt
5032 218 : = gimple_build_assign (bool_cond, LT_EXPR, r_or_negr_and_xor,
5033 : build_int_cst (itype, 0));
5034 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
5035 : itype);
5036 :
5037 : // (x^y < 0 && r) ? y : 0 (mod)
5038 : // (x^y < 0 && r) ? -1 : 0 (div)
5039 218 : bool is_mod
5040 218 : = (rhs_code == FLOOR_MOD_EXPR || rhs_code == CEIL_MOD_EXPR);
5041 218 : tree extr_cond = vect_recog_temp_ssa_var (itype, NULL);
5042 258 : def_stmt = gimple_build_assign (extr_cond, COND_EXPR, bool_cond,
5043 : is_mod ? oprnd1
5044 40 : : build_int_cst (itype, -1),
5045 : build_int_cst (itype, 0));
5046 218 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5047 :
5048 : // r += (x ^ y < 0 && r) ? y : 0 (floor mod)
5049 : // d += (x^y < 0 && r) ? -1 : 0 (floor div)
5050 : // r -= (x ^ y < 0 && r) ? y : 0 (ceil mod)
5051 : // d -= (x^y < 0 && r) ? -1 : 0 (ceil div)
5052 218 : tree result = vect_recog_temp_ssa_var (itype, NULL);
5053 436 : return gimple_build_assign (result,
5054 218 : (rhs_code == FLOOR_MOD_EXPR
5055 218 : || rhs_code == FLOOR_DIV_EXPR)
5056 : ? PLUS_EXPR
5057 : : MINUS_EXPR,
5058 218 : is_mod ? r : q, extr_cond);
5059 : }
5060 : }
5061 36 : case ROUND_MOD_EXPR:
5062 36 : case ROUND_DIV_EXPR:
5063 36 : {
5064 36 : if (!target_has_vecop_for_code (BIT_AND_EXPR, vectype)
5065 36 : || !target_has_vecop_for_code (PLUS_EXPR, vectype)
5066 36 : || !expand_vec_cmp_expr_p (vectype, mask_vectype, LT_EXPR)
5067 36 : || !expand_vec_cmp_expr_p (vectype, mask_vectype, GT_EXPR)
5068 72 : || !expand_vec_cond_expr_p (vectype, mask_vectype))
5069 0 : return NULL;
5070 :
5071 36 : bool is_mod = rhs_code == ROUND_MOD_EXPR;
5072 36 : HOST_WIDE_INT d = TREE_INT_CST_LOW (oprnd1);
5073 36 : unsigned HOST_WIDE_INT abs_d
5074 : = (d >= 0 ? (unsigned HOST_WIDE_INT) d : -(unsigned HOST_WIDE_INT) d);
5075 36 : unsigned HOST_WIDE_INT mid_d = (abs_d - 1) >> 1;
5076 36 : if (!unsigned_p)
5077 : {
5078 : // check availability of abs expression for vector
5079 18 : if (!target_has_vecop_for_code (ABS_EXPR, vectype))
5080 : return NULL;
5081 : // abs (r)
5082 18 : tree abs_r = vect_recog_temp_ssa_var (itype, NULL);
5083 18 : def_stmt = gimple_build_assign (abs_r, ABS_EXPR, r);
5084 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5085 :
5086 : // abs (r) > (abs (y-1) >> 1)
5087 18 : tree round_p = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5088 18 : def_stmt = gimple_build_assign (round_p, GT_EXPR, abs_r,
5089 18 : build_int_cst (itype, mid_d));
5090 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
5091 : itype);
5092 :
5093 : // x ^ y
5094 18 : tree cond_reg = vect_recog_temp_ssa_var (itype, NULL);
5095 18 : def_stmt
5096 18 : = gimple_build_assign (cond_reg, BIT_XOR_EXPR, oprnd0, oprnd1);
5097 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5098 :
5099 : // x ^ y < 0
5100 18 : bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5101 18 : def_stmt = gimple_build_assign (bool_cond, LT_EXPR, cond_reg,
5102 : build_int_cst (itype, 0));
5103 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
5104 : itype);
5105 :
5106 : // x ^ y < 0 ? y : -y (mod)
5107 : // x ^ y < 0 ? -1 : 1 (div)
5108 18 : tree val1 = vect_recog_temp_ssa_var (itype, NULL);
5109 18 : def_stmt
5110 36 : = gimple_build_assign (val1, COND_EXPR, bool_cond,
5111 27 : build_int_cst (itype, is_mod ? d : -1),
5112 18 : build_int_cst (itype, is_mod ? -d : 1));
5113 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5114 18 : int precision = TYPE_PRECISION (itype);
5115 18 : wide_int wmask = wi::mask (precision, false, precision);
5116 :
5117 : // abs (r) > (abs (y-1) >> 1) ? 0xffffffff : 0
5118 18 : tree val2 = vect_recog_temp_ssa_var (itype, NULL);
5119 36 : def_stmt = gimple_build_assign (val2, COND_EXPR, round_p,
5120 18 : wide_int_to_tree (itype, wmask),
5121 : build_int_cst (itype, 0));
5122 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5123 :
5124 18 : tree fval = vect_recog_temp_ssa_var (itype, NULL);
5125 18 : def_stmt = gimple_build_assign (fval, BIT_AND_EXPR, val1, val2);
5126 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5127 :
5128 18 : tree result = vect_recog_temp_ssa_var (itype, NULL);
5129 27 : return gimple_build_assign (result, PLUS_EXPR, is_mod ? r : q,
5130 : fval);
5131 18 : }
5132 : else
5133 : {
5134 : // r > (y-1 >> 1)
5135 18 : tree round_p = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5136 18 : def_stmt = gimple_build_assign (round_p, GT_EXPR, r,
5137 18 : build_int_cst (itype, mid_d));
5138 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
5139 : itype);
5140 :
5141 : // (r > (y-1)>>1) ? -d : 1
5142 18 : tree val2 = vect_recog_temp_ssa_var (itype, NULL);
5143 18 : def_stmt
5144 36 : = gimple_build_assign (val2, COND_EXPR, round_p,
5145 18 : build_int_cst (itype, is_mod ? -d : 1),
5146 : build_int_cst (itype, 0));
5147 18 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5148 :
5149 18 : tree result = vect_recog_temp_ssa_var (itype, NULL);
5150 27 : return gimple_build_assign (result, PLUS_EXPR, is_mod ? r : q,
5151 18 : val2);
5152 : }
5153 : }
5154 : default:
5155 : return NULL;
5156 : }
5157 : }
5158 :
5159 : /* Detect a signed division by a constant that wouldn't be
5160 : otherwise vectorized:
5161 :
5162 : type a_t, b_t;
5163 :
5164 : S1 a_t = b_t / N;
5165 :
5166 : where type 'type' is an integral type and N is a constant.
5167 :
5168 : Similarly handle modulo by a constant:
5169 :
5170 : S4 a_t = b_t % N;
5171 :
5172 : Input/Output:
5173 :
5174 : * STMT_VINFO: The stmt from which the pattern search begins,
5175 : i.e. the division stmt. S1 is replaced by if N is a power
5176 : of two constant and type is signed:
5177 : S3 y_t = b_t < 0 ? N - 1 : 0;
5178 : S2 x_t = b_t + y_t;
5179 : S1' a_t = x_t >> log2 (N);
5180 :
5181 : S4 is replaced if N is a power of two constant and
5182 : type is signed by (where *_T temporaries have unsigned type):
5183 : S9 y_T = b_t < 0 ? -1U : 0U;
5184 : S8 z_T = y_T >> (sizeof (type_t) * CHAR_BIT - log2 (N));
5185 : S7 z_t = (type) z_T;
5186 : S6 w_t = b_t + z_t;
5187 : S5 x_t = w_t & (N - 1);
5188 : S4' a_t = x_t - z_t;
5189 :
5190 : Output:
5191 :
5192 : * TYPE_OUT: The type of the output of this pattern.
5193 :
5194 : * Return value: A new stmt that will be used to replace the division
5195 : S1 or modulo S4 stmt. */
5196 :
5197 : static gimple *
5198 32157534 : vect_recog_divmod_pattern (vec_info *vinfo,
5199 : stmt_vec_info stmt_vinfo, tree *type_out)
5200 : {
5201 32157534 : gimple *last_stmt = stmt_vinfo->stmt;
5202 32157534 : tree oprnd0, oprnd1, vectype, itype, cond;
5203 32157534 : gimple *pattern_stmt = NULL;
5204 32157534 : gimple *def_stmt = NULL;
5205 32157534 : enum tree_code rhs_code;
5206 32157534 : optab optab;
5207 32157534 : tree q, cst;
5208 32157534 : int prec;
5209 :
5210 32157534 : if (!is_gimple_assign (last_stmt)
5211 : /* The pattern will disrupt the reduction chain with multiple uses. */
5212 32157534 : || vect_is_reduction (stmt_vinfo))
5213 : return NULL;
5214 :
5215 22340790 : rhs_code = gimple_assign_rhs_code (last_stmt);
5216 22340790 : switch (rhs_code)
5217 : {
5218 297435 : case TRUNC_DIV_EXPR:
5219 297435 : case EXACT_DIV_EXPR:
5220 297435 : case TRUNC_MOD_EXPR:
5221 297435 : case FLOOR_MOD_EXPR:
5222 297435 : case FLOOR_DIV_EXPR:
5223 297435 : case CEIL_MOD_EXPR:
5224 297435 : case CEIL_DIV_EXPR:
5225 297435 : case ROUND_MOD_EXPR:
5226 297435 : case ROUND_DIV_EXPR:
5227 297435 : break;
5228 : default:
5229 : return NULL;
5230 : }
5231 :
5232 297435 : oprnd0 = gimple_assign_rhs1 (last_stmt);
5233 297435 : oprnd1 = gimple_assign_rhs2 (last_stmt);
5234 297435 : itype = TREE_TYPE (oprnd0);
5235 297435 : if (TREE_CODE (oprnd0) != SSA_NAME
5236 279842 : || TREE_CODE (oprnd1) != INTEGER_CST
5237 169657 : || TREE_CODE (itype) != INTEGER_TYPE
5238 467092 : || !type_has_mode_precision_p (itype))
5239 127778 : return NULL;
5240 :
5241 169657 : scalar_int_mode itype_mode = SCALAR_INT_TYPE_MODE (itype);
5242 169657 : vectype = get_vectype_for_scalar_type (vinfo, itype);
5243 169657 : if (vectype == NULL_TREE)
5244 : return NULL;
5245 :
5246 135965 : if (optimize_bb_for_size_p (gimple_bb (last_stmt)))
5247 : {
5248 : /* If the target can handle vectorized division or modulo natively,
5249 : don't attempt to optimize this, since native division is likely
5250 : to give smaller code. */
5251 2602 : optab = optab_for_tree_code (rhs_code, vectype, optab_default);
5252 2602 : if (optab != unknown_optab
5253 2602 : && can_implement_p (optab, TYPE_MODE (vectype)))
5254 : return NULL;
5255 : }
5256 :
5257 135965 : prec = TYPE_PRECISION (itype);
5258 :
5259 271930 : bool is_flclrd_moddiv_p
5260 135965 : = rhs_code == FLOOR_MOD_EXPR || rhs_code == FLOOR_DIV_EXPR
5261 : || rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR
5262 135342 : || rhs_code == ROUND_MOD_EXPR || rhs_code == ROUND_DIV_EXPR;
5263 135965 : if (integer_pow2p (oprnd1))
5264 : {
5265 82667 : if ((TYPE_UNSIGNED (itype)
5266 57 : && (rhs_code == FLOOR_MOD_EXPR || rhs_code == FLOOR_DIV_EXPR))
5267 82721 : || tree_int_cst_sgn (oprnd1) != 1)
5268 3 : return NULL;
5269 :
5270 : /* Pattern detected. */
5271 82664 : vect_pattern_detected ("vect_recog_divmod_pattern", last_stmt);
5272 :
5273 82664 : *type_out = vectype;
5274 :
5275 : /* Check if the target supports this internal function. */
5276 82664 : internal_fn ifn = IFN_DIV_POW2;
5277 82664 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
5278 : {
5279 0 : tree shift = build_int_cst (itype, tree_log2 (oprnd1));
5280 :
5281 0 : tree var_div = vect_recog_temp_ssa_var (itype, NULL);
5282 0 : gimple *div_stmt = gimple_build_call_internal (ifn, 2, oprnd0, shift);
5283 0 : gimple_call_set_lhs (div_stmt, var_div);
5284 0 : if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
5285 : {
5286 0 : append_pattern_def_seq (vinfo, stmt_vinfo, div_stmt);
5287 0 : tree t1 = vect_recog_temp_ssa_var (itype, NULL);
5288 0 : def_stmt
5289 0 : = gimple_build_assign (t1, LSHIFT_EXPR, var_div, shift);
5290 0 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5291 0 : pattern_stmt
5292 0 : = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
5293 : MINUS_EXPR, oprnd0, t1);
5294 0 : if (is_flclrd_moddiv_p)
5295 : {
5296 0 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
5297 0 : pattern_stmt
5298 0 : = add_code_for_floorceilround_divmod (vectype, vinfo,
5299 : stmt_vinfo, rhs_code,
5300 : var_div, t1, oprnd0,
5301 : oprnd1, itype);
5302 0 : if (pattern_stmt == NULL)
5303 : return NULL;
5304 : }
5305 : }
5306 : else
5307 : pattern_stmt = div_stmt;
5308 0 : gimple_set_location (pattern_stmt, gimple_location (last_stmt));
5309 :
5310 0 : return pattern_stmt;
5311 : }
5312 :
5313 82664 : cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5314 82664 : def_stmt = gimple_build_assign (cond, LT_EXPR, oprnd0,
5315 : build_int_cst (itype, 0));
5316 82664 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt,
5317 : truth_type_for (vectype), itype);
5318 82664 : tree div_result = NULL_TREE;
5319 82664 : if (rhs_code == TRUNC_DIV_EXPR
5320 82664 : || rhs_code == EXACT_DIV_EXPR
5321 : || rhs_code == FLOOR_DIV_EXPR
5322 2735 : || rhs_code == CEIL_DIV_EXPR
5323 2576 : || rhs_code == ROUND_DIV_EXPR)
5324 : {
5325 80100 : tree var = vect_recog_temp_ssa_var (itype, NULL);
5326 80100 : tree shift;
5327 80100 : def_stmt
5328 80100 : = gimple_build_assign (var, COND_EXPR, cond,
5329 : fold_build2 (MINUS_EXPR, itype, oprnd1,
5330 : build_int_cst (itype, 1)),
5331 : build_int_cst (itype, 0));
5332 80100 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5333 80100 : var = vect_recog_temp_ssa_var (itype, NULL);
5334 80100 : def_stmt
5335 80100 : = gimple_build_assign (var, PLUS_EXPR, oprnd0,
5336 : gimple_assign_lhs (def_stmt));
5337 80100 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5338 :
5339 80100 : shift = build_int_cst (itype, tree_log2 (oprnd1));
5340 80100 : div_result = vect_recog_temp_ssa_var (itype, NULL);
5341 80100 : pattern_stmt
5342 80100 : = gimple_build_assign (div_result, RSHIFT_EXPR, var, shift);
5343 : }
5344 82664 : if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
5345 : {
5346 2735 : if (rhs_code == FLOOR_DIV_EXPR
5347 : || rhs_code == CEIL_DIV_EXPR
5348 2735 : || rhs_code == ROUND_DIV_EXPR)
5349 171 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
5350 :
5351 2735 : tree signmask;
5352 2735 : if (compare_tree_int (oprnd1, 2) == 0)
5353 : {
5354 1309 : signmask = vect_recog_temp_ssa_var (itype, NULL);
5355 1309 : def_stmt = gimple_build_assign (signmask, COND_EXPR, cond,
5356 : build_int_cst (itype, 1),
5357 : build_int_cst (itype, 0));
5358 1309 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5359 : }
5360 : else
5361 : {
5362 1426 : tree utype
5363 1426 : = build_nonstandard_integer_type (prec, 1);
5364 1426 : tree vecutype = get_vectype_for_scalar_type (vinfo, utype);
5365 1426 : tree shift
5366 1426 : = build_int_cst (utype, GET_MODE_BITSIZE (itype_mode)
5367 1426 : - tree_log2 (oprnd1));
5368 1426 : tree var = vect_recog_temp_ssa_var (utype, NULL);
5369 :
5370 1426 : def_stmt = gimple_build_assign (var, COND_EXPR, cond,
5371 : build_int_cst (utype, -1),
5372 : build_int_cst (utype, 0));
5373 1426 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecutype);
5374 1426 : var = vect_recog_temp_ssa_var (utype, NULL);
5375 1426 : def_stmt = gimple_build_assign (var, RSHIFT_EXPR,
5376 : gimple_assign_lhs (def_stmt),
5377 : shift);
5378 1426 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecutype);
5379 1426 : signmask = vect_recog_temp_ssa_var (itype, NULL);
5380 1426 : def_stmt
5381 1426 : = gimple_build_assign (signmask, NOP_EXPR, var);
5382 1426 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5383 : }
5384 2735 : def_stmt
5385 2735 : = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
5386 : PLUS_EXPR, oprnd0, signmask);
5387 2735 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5388 2735 : def_stmt
5389 2735 : = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
5390 : BIT_AND_EXPR, gimple_assign_lhs (def_stmt),
5391 : fold_build2 (MINUS_EXPR, itype, oprnd1,
5392 : build_int_cst (itype, 1)));
5393 2735 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5394 :
5395 2735 : tree r = vect_recog_temp_ssa_var (itype, NULL);
5396 2735 : pattern_stmt
5397 2735 : = gimple_build_assign (r, MINUS_EXPR, gimple_assign_lhs (def_stmt),
5398 : signmask);
5399 2735 : if (is_flclrd_moddiv_p)
5400 : {
5401 285 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
5402 285 : pattern_stmt
5403 285 : = add_code_for_floorceilround_divmod (vectype, vinfo,
5404 : stmt_vinfo, rhs_code,
5405 : div_result, r, oprnd0,
5406 : oprnd1, itype);
5407 285 : if (pattern_stmt == NULL)
5408 : return NULL;
5409 : }
5410 : }
5411 :
5412 82505 : return pattern_stmt;
5413 : }
5414 :
5415 53298 : if ((cst = uniform_integer_cst_p (oprnd1))
5416 53298 : && TYPE_UNSIGNED (itype)
5417 : && rhs_code == TRUNC_DIV_EXPR
5418 29974 : && vectype
5419 71351 : && targetm.vectorize.preferred_div_as_shifts_over_mult (vectype))
5420 : {
5421 : /* We can use the relationship:
5422 :
5423 : x // N == ((x+N+2) // (N+1) + x) // (N+1) for 0 <= x < N(N+3)
5424 :
5425 : to optimize cases where N+1 is a power of 2, and where // (N+1)
5426 : is therefore a shift right. When operating in modes that are
5427 : multiples of a byte in size, there are two cases:
5428 :
5429 : (1) N(N+3) is not representable, in which case the question
5430 : becomes whether the replacement expression overflows.
5431 : It is enough to test that x+N+2 does not overflow,
5432 : i.e. that x < MAX-(N+1).
5433 :
5434 : (2) N(N+3) is representable, in which case it is the (only)
5435 : bound that we need to check.
5436 :
5437 : ??? For now we just handle the case where // (N+1) is a shift
5438 : right by half the precision, since some architectures can
5439 : optimize the associated addition and shift combinations
5440 : into single instructions. */
5441 :
5442 12244 : auto wcst = wi::to_wide (cst);
5443 12244 : int pow = wi::exact_log2 (wcst + 1);
5444 12244 : if (pow == prec / 2)
5445 : {
5446 : /* Check that no overflow will occur. If we don't have range
5447 : information we can't perform the optimization. */
5448 :
5449 472 : int_range_max r;
5450 944 : if (get_range_query (cfun)->range_of_expr (r, oprnd0, last_stmt)
5451 472 : && !r.undefined_p ())
5452 : {
5453 472 : wide_int max = r.upper_bound ();
5454 472 : wide_int one = wi::shwi (1, prec);
5455 472 : wide_int adder = wi::add (one, wi::lshift (one, pow));
5456 472 : wi::overflow_type ovf;
5457 472 : wi::add (max, adder, UNSIGNED, &ovf);
5458 472 : if (ovf == wi::OVF_NONE)
5459 : {
5460 305 : *type_out = vectype;
5461 305 : tree tadder = wide_int_to_tree (itype, adder);
5462 305 : tree rshift = wide_int_to_tree (itype, pow);
5463 :
5464 305 : tree new_lhs1 = vect_recog_temp_ssa_var (itype, NULL);
5465 305 : gassign *patt1
5466 305 : = gimple_build_assign (new_lhs1, PLUS_EXPR, oprnd0, tadder);
5467 305 : append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
5468 :
5469 305 : tree new_lhs2 = vect_recog_temp_ssa_var (itype, NULL);
5470 305 : patt1 = gimple_build_assign (new_lhs2, RSHIFT_EXPR, new_lhs1,
5471 : rshift);
5472 305 : append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
5473 :
5474 305 : tree new_lhs3 = vect_recog_temp_ssa_var (itype, NULL);
5475 305 : patt1 = gimple_build_assign (new_lhs3, PLUS_EXPR, new_lhs2,
5476 : oprnd0);
5477 305 : append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
5478 :
5479 305 : tree new_lhs4 = vect_recog_temp_ssa_var (itype, NULL);
5480 305 : pattern_stmt = gimple_build_assign (new_lhs4, RSHIFT_EXPR,
5481 : new_lhs3, rshift);
5482 :
5483 305 : return pattern_stmt;
5484 : }
5485 472 : }
5486 472 : }
5487 : }
5488 :
5489 52993 : if (prec > HOST_BITS_PER_WIDE_INT
5490 52993 : || integer_zerop (oprnd1))
5491 287 : return NULL;
5492 :
5493 52706 : if (!can_mult_highpart_p (TYPE_MODE (vectype), TYPE_UNSIGNED (itype)))
5494 : return NULL;
5495 :
5496 14311 : if (TYPE_UNSIGNED (itype))
5497 : {
5498 8888 : unsigned HOST_WIDE_INT mh, ml;
5499 8888 : int pre_shift, post_shift;
5500 8888 : unsigned HOST_WIDE_INT d = (TREE_INT_CST_LOW (oprnd1)
5501 8888 : & GET_MODE_MASK (itype_mode));
5502 8888 : tree t1, t2, t3, t4;
5503 :
5504 8888 : if (d >= (HOST_WIDE_INT_1U << (prec - 1)))
5505 : /* FIXME: Can transform this into oprnd0 >= oprnd1 ? 1 : 0. */
5506 23 : return NULL;
5507 :
5508 : /* Find a suitable multiplier and right shift count instead of
5509 : directly dividing by D. */
5510 8865 : mh = choose_multiplier (d, prec, prec, &ml, &post_shift);
5511 :
5512 : /* If the suggested multiplier is more than PREC bits, we can do better
5513 : for even divisors, using an initial right shift. */
5514 8865 : if (mh != 0 && (d & 1) == 0)
5515 : {
5516 248 : pre_shift = ctz_or_zero (d);
5517 248 : mh = choose_multiplier (d >> pre_shift, prec, prec - pre_shift,
5518 : &ml, &post_shift);
5519 248 : gcc_assert (!mh);
5520 : }
5521 : else
5522 : pre_shift = 0;
5523 :
5524 652 : if (mh != 0)
5525 : {
5526 652 : if (post_shift - 1 >= prec)
5527 : return NULL;
5528 :
5529 : /* t1 = oprnd0 h* ml;
5530 : t2 = oprnd0 - t1;
5531 : t3 = t2 >> 1;
5532 : t4 = t1 + t3;
5533 : q = t4 >> (post_shift - 1); */
5534 652 : t1 = vect_recog_temp_ssa_var (itype, NULL);
5535 652 : def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0,
5536 652 : build_int_cst (itype, ml));
5537 652 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5538 :
5539 652 : t2 = vect_recog_temp_ssa_var (itype, NULL);
5540 652 : def_stmt
5541 652 : = gimple_build_assign (t2, MINUS_EXPR, oprnd0, t1);
5542 652 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5543 :
5544 652 : t3 = vect_recog_temp_ssa_var (itype, NULL);
5545 652 : def_stmt
5546 652 : = gimple_build_assign (t3, RSHIFT_EXPR, t2, integer_one_node);
5547 652 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5548 :
5549 652 : t4 = vect_recog_temp_ssa_var (itype, NULL);
5550 652 : def_stmt
5551 652 : = gimple_build_assign (t4, PLUS_EXPR, t1, t3);
5552 :
5553 652 : if (post_shift != 1)
5554 : {
5555 652 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5556 :
5557 652 : q = vect_recog_temp_ssa_var (itype, NULL);
5558 652 : pattern_stmt
5559 652 : = gimple_build_assign (q, RSHIFT_EXPR, t4,
5560 652 : build_int_cst (itype, post_shift - 1));
5561 : }
5562 : else
5563 : {
5564 : q = t4;
5565 : pattern_stmt = def_stmt;
5566 : }
5567 : }
5568 : else
5569 : {
5570 8213 : if (pre_shift >= prec || post_shift >= prec)
5571 : return NULL;
5572 :
5573 : /* t1 = oprnd0 >> pre_shift;
5574 : t2 = t1 h* ml;
5575 : q = t2 >> post_shift; */
5576 8213 : if (pre_shift)
5577 : {
5578 248 : t1 = vect_recog_temp_ssa_var (itype, NULL);
5579 248 : def_stmt
5580 248 : = gimple_build_assign (t1, RSHIFT_EXPR, oprnd0,
5581 248 : build_int_cst (NULL, pre_shift));
5582 248 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5583 : }
5584 : else
5585 : t1 = oprnd0;
5586 :
5587 8213 : t2 = vect_recog_temp_ssa_var (itype, NULL);
5588 8213 : def_stmt = gimple_build_assign (t2, MULT_HIGHPART_EXPR, t1,
5589 8213 : build_int_cst (itype, ml));
5590 :
5591 8213 : if (post_shift)
5592 : {
5593 8203 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5594 :
5595 8203 : q = vect_recog_temp_ssa_var (itype, NULL);
5596 8203 : def_stmt
5597 8203 : = gimple_build_assign (q, RSHIFT_EXPR, t2,
5598 8203 : build_int_cst (itype, post_shift));
5599 : }
5600 : else
5601 : q = t2;
5602 :
5603 : pattern_stmt = def_stmt;
5604 : }
5605 : }
5606 : else
5607 : {
5608 5423 : unsigned HOST_WIDE_INT ml;
5609 5423 : int post_shift;
5610 5423 : HOST_WIDE_INT d = TREE_INT_CST_LOW (oprnd1);
5611 5423 : unsigned HOST_WIDE_INT abs_d;
5612 5423 : bool add = false;
5613 5423 : tree t1, t2, t3, t4;
5614 :
5615 : /* Give up for -1. */
5616 5423 : if (d == -1)
5617 0 : return NULL;
5618 :
5619 : /* Since d might be INT_MIN, we have to cast to
5620 : unsigned HOST_WIDE_INT before negating to avoid
5621 : undefined signed overflow. */
5622 5423 : abs_d = (d >= 0
5623 5423 : ? (unsigned HOST_WIDE_INT) d
5624 : : - (unsigned HOST_WIDE_INT) d);
5625 :
5626 : /* n rem d = n rem -d */
5627 5423 : if (rhs_code == TRUNC_MOD_EXPR && d < 0)
5628 : {
5629 0 : d = abs_d;
5630 0 : oprnd1 = build_int_cst (itype, abs_d);
5631 : }
5632 5423 : if (HOST_BITS_PER_WIDE_INT >= prec
5633 5423 : && abs_d == HOST_WIDE_INT_1U << (prec - 1))
5634 : /* This case is not handled correctly below. */
5635 : return NULL;
5636 :
5637 5423 : choose_multiplier (abs_d, prec, prec - 1, &ml, &post_shift);
5638 5423 : if (ml >= HOST_WIDE_INT_1U << (prec - 1))
5639 : {
5640 1623 : add = true;
5641 1623 : ml |= HOST_WIDE_INT_M1U << (prec - 1);
5642 : }
5643 5423 : if (post_shift >= prec)
5644 : return NULL;
5645 :
5646 : /* t1 = oprnd0 h* ml; */
5647 5423 : t1 = vect_recog_temp_ssa_var (itype, NULL);
5648 5423 : def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0,
5649 5423 : build_int_cst (itype, ml));
5650 :
5651 5423 : if (add)
5652 : {
5653 : /* t2 = t1 + oprnd0; */
5654 1623 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5655 1623 : t2 = vect_recog_temp_ssa_var (itype, NULL);
5656 1623 : def_stmt = gimple_build_assign (t2, PLUS_EXPR, t1, oprnd0);
5657 : }
5658 : else
5659 : t2 = t1;
5660 :
5661 5423 : if (post_shift)
5662 : {
5663 : /* t3 = t2 >> post_shift; */
5664 4614 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5665 4614 : t3 = vect_recog_temp_ssa_var (itype, NULL);
5666 4614 : def_stmt = gimple_build_assign (t3, RSHIFT_EXPR, t2,
5667 4614 : build_int_cst (itype, post_shift));
5668 : }
5669 : else
5670 : t3 = t2;
5671 :
5672 5423 : int msb = 1;
5673 5423 : int_range_max r;
5674 10846 : get_range_query (cfun)->range_of_expr (r, oprnd0);
5675 5423 : if (!r.varying_p () && !r.undefined_p ())
5676 : {
5677 3026 : if (!wi::neg_p (r.lower_bound (), TYPE_SIGN (itype)))
5678 : msb = 0;
5679 758 : else if (wi::neg_p (r.upper_bound (), TYPE_SIGN (itype)))
5680 : msb = -1;
5681 : }
5682 :
5683 2268 : if (msb == 0 && d >= 0)
5684 : {
5685 : /* q = t3; */
5686 : q = t3;
5687 : pattern_stmt = def_stmt;
5688 : }
5689 : else
5690 : {
5691 : /* t4 = oprnd0 >> (prec - 1);
5692 : or if we know from VRP that oprnd0 >= 0
5693 : t4 = 0;
5694 : or if we know from VRP that oprnd0 < 0
5695 : t4 = -1; */
5696 3215 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5697 3215 : t4 = vect_recog_temp_ssa_var (itype, NULL);
5698 3215 : if (msb != 1)
5699 68 : def_stmt = gimple_build_assign (t4, INTEGER_CST,
5700 68 : build_int_cst (itype, msb));
5701 : else
5702 3147 : def_stmt = gimple_build_assign (t4, RSHIFT_EXPR, oprnd0,
5703 3147 : build_int_cst (itype, prec - 1));
5704 3215 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5705 :
5706 : /* q = t3 - t4; or q = t4 - t3; */
5707 3215 : q = vect_recog_temp_ssa_var (itype, NULL);
5708 6246 : pattern_stmt = gimple_build_assign (q, MINUS_EXPR, d < 0 ? t4 : t3,
5709 : d < 0 ? t3 : t4);
5710 : }
5711 5423 : }
5712 :
5713 14288 : if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
5714 : {
5715 6796 : tree r, t1;
5716 :
5717 : /* We divided. Now finish by:
5718 : t1 = q * oprnd1;
5719 : r = oprnd0 - t1; */
5720 6796 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
5721 :
5722 6796 : t1 = vect_recog_temp_ssa_var (itype, NULL);
5723 6796 : def_stmt = gimple_build_assign (t1, MULT_EXPR, q, oprnd1);
5724 6796 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
5725 :
5726 6796 : r = vect_recog_temp_ssa_var (itype, NULL);
5727 6796 : pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, t1);
5728 :
5729 6796 : if (is_flclrd_moddiv_p)
5730 : {
5731 146 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
5732 146 : pattern_stmt
5733 146 : = add_code_for_floorceilround_divmod (vectype, vinfo, stmt_vinfo,
5734 : rhs_code, q, r, oprnd0, oprnd1,
5735 : itype);
5736 146 : if (pattern_stmt == NULL)
5737 : return NULL;
5738 : }
5739 : }
5740 :
5741 : /* Pattern detected. */
5742 14288 : vect_pattern_detected ("vect_recog_divmod_pattern", last_stmt);
5743 :
5744 14288 : *type_out = vectype;
5745 14288 : return pattern_stmt;
5746 : }
5747 :
5748 : /* Detects pattern with a modulo operation (S1) where both arguments
5749 : are variables of integral type.
5750 : The statement is replaced by division, multiplication, and subtraction.
5751 : The last statement (S4) is returned.
5752 :
5753 : Example:
5754 : S1 c_t = a_t % b_t;
5755 :
5756 : is replaced by
5757 : S2 x_t = a_t / b_t;
5758 : S3 y_t = x_t * b_t;
5759 : S4 z_t = a_t - y_t; */
5760 :
5761 : static gimple *
5762 32356546 : vect_recog_mod_var_pattern (vec_info *vinfo,
5763 : stmt_vec_info stmt_vinfo, tree *type_out)
5764 : {
5765 32356546 : gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
5766 32356546 : tree oprnd0, oprnd1, vectype, itype;
5767 32356546 : gimple *pattern_stmt, *def_stmt;
5768 32356546 : enum tree_code rhs_code;
5769 :
5770 32356546 : if (!is_gimple_assign (last_stmt) || vect_is_reduction (stmt_vinfo))
5771 : return NULL;
5772 :
5773 22539802 : rhs_code = gimple_assign_rhs_code (last_stmt);
5774 22539802 : if (rhs_code != TRUNC_MOD_EXPR)
5775 : return NULL;
5776 :
5777 77811 : oprnd0 = gimple_assign_rhs1 (last_stmt);
5778 77811 : oprnd1 = gimple_assign_rhs2 (last_stmt);
5779 77811 : itype = TREE_TYPE (oprnd0);
5780 77811 : if (TREE_CODE (oprnd0) != SSA_NAME
5781 69500 : || TREE_CODE (oprnd1) != SSA_NAME
5782 52673 : || TREE_CODE (itype) != INTEGER_TYPE)
5783 : return NULL;
5784 :
5785 52570 : vectype = get_vectype_for_scalar_type (vinfo, itype);
5786 :
5787 52570 : if (!vectype
5788 43892 : || target_has_vecop_for_code (TRUNC_MOD_EXPR, vectype)
5789 43892 : || !target_has_vecop_for_code (TRUNC_DIV_EXPR, vectype)
5790 0 : || !target_has_vecop_for_code (MULT_EXPR, vectype)
5791 52570 : || !target_has_vecop_for_code (MINUS_EXPR, vectype))
5792 52570 : return NULL;
5793 :
5794 0 : tree q, tmp, r;
5795 0 : q = vect_recog_temp_ssa_var (itype, NULL);
5796 0 : def_stmt = gimple_build_assign (q, TRUNC_DIV_EXPR, oprnd0, oprnd1);
5797 0 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vectype);
5798 :
5799 0 : tmp = vect_recog_temp_ssa_var (itype, NULL);
5800 0 : def_stmt = gimple_build_assign (tmp, MULT_EXPR, q, oprnd1);
5801 0 : append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vectype);
5802 :
5803 0 : r = vect_recog_temp_ssa_var (itype, NULL);
5804 0 : pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, tmp);
5805 :
5806 : /* Pattern detected. */
5807 0 : *type_out = vectype;
5808 0 : vect_pattern_detected ("vect_recog_mod_var_pattern", last_stmt);
5809 :
5810 0 : return pattern_stmt;
5811 : }
5812 :
5813 :
5814 : /* Return the proper type for converting bool VAR into
5815 : an integer value or NULL_TREE if no such type exists.
5816 : The type is chosen so that the converted value has the
5817 : same number of elements as VAR's vector type. */
5818 :
5819 : static tree
5820 4820767 : integer_type_for_mask (tree var, vec_info *vinfo, vect_def_type *dt = nullptr)
5821 : {
5822 4820767 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (var)))
5823 : return NULL_TREE;
5824 :
5825 2219097 : stmt_vec_info def_stmt_info = vinfo->lookup_def (var);
5826 2219097 : if (dt)
5827 : {
5828 406080 : if (!def_stmt_info)
5829 3396 : *dt = vect_external_def;
5830 : else
5831 402684 : *dt = STMT_VINFO_DEF_TYPE (def_stmt_info);
5832 : }
5833 406080 : if (!def_stmt_info
5834 2124471 : || STMT_VINFO_DEF_TYPE (def_stmt_info) == vect_external_def
5835 3937488 : || !vect_use_mask_type_p (def_stmt_info))
5836 825303 : return NULL_TREE;
5837 :
5838 1393794 : return build_nonstandard_integer_type (def_stmt_info->mask_precision, 1);
5839 : }
5840 :
5841 : /* Function vect_recog_gcond_pattern
5842 :
5843 : Try to find pattern like following:
5844 :
5845 : if (a op b)
5846 :
5847 : where operator 'op' is not != and convert it to an adjusted boolean pattern
5848 :
5849 : mask = a op b
5850 : if (mask != 0)
5851 :
5852 : and set the mask type on MASK.
5853 :
5854 : Input:
5855 :
5856 : * STMT_VINFO: The stmt at the end from which the pattern
5857 : search begins, i.e. cast of a bool to
5858 : an integer type.
5859 :
5860 : Output:
5861 :
5862 : * TYPE_OUT: The type of the output of this pattern.
5863 :
5864 : * Return value: A new stmt that will be used to replace the pattern. */
5865 :
5866 : static gimple *
5867 32427327 : vect_recog_gcond_pattern (vec_info *vinfo,
5868 : stmt_vec_info stmt_vinfo, tree *type_out)
5869 : {
5870 : /* Currently we only support this for loop vectorization and when multiple
5871 : exits. */
5872 32427327 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5873 4565523 : if (!loop_vinfo || !LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
5874 : return NULL;
5875 :
5876 1691028 : gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
5877 1691028 : gcond* cond = NULL;
5878 32447173 : if (!(cond = dyn_cast <gcond *> (last_stmt)))
5879 : return NULL;
5880 :
5881 395537 : auto lhs = gimple_cond_lhs (cond);
5882 395537 : auto rhs = gimple_cond_rhs (cond);
5883 395537 : auto code = gimple_cond_code (cond);
5884 :
5885 395537 : tree scalar_type = TREE_TYPE (lhs);
5886 395537 : if (VECTOR_TYPE_P (scalar_type))
5887 : return NULL;
5888 :
5889 : /* If the input is a boolean then try to figure out the precision that the
5890 : vector type should use. We cannot use the scalar precision as this would
5891 : later mismatch. This is similar to what recog_bool does. */
5892 395537 : if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
5893 : {
5894 10356 : if (tree stype = integer_type_for_mask (lhs, vinfo))
5895 395537 : scalar_type = stype;
5896 : }
5897 :
5898 395537 : tree vectype = get_mask_type_for_scalar_type (vinfo, scalar_type);
5899 395537 : if (vectype == NULL_TREE)
5900 : return NULL;
5901 :
5902 375691 : tree new_lhs = vect_recog_temp_ssa_var (boolean_type_node, NULL);
5903 375691 : gimple *new_stmt = gimple_build_assign (new_lhs, code, lhs, rhs);
5904 375691 : append_pattern_def_seq (vinfo, stmt_vinfo, new_stmt, vectype, scalar_type);
5905 :
5906 375691 : gimple *pattern_stmt
5907 375691 : = gimple_build_cond (NE_EXPR, new_lhs,
5908 375691 : build_int_cst (TREE_TYPE (new_lhs), 0),
5909 : NULL_TREE, NULL_TREE);
5910 375691 : *type_out = vectype;
5911 375691 : vect_pattern_detected ("vect_recog_gcond_pattern", last_stmt);
5912 375691 : return pattern_stmt;
5913 : }
5914 :
5915 :
5916 : /* A helper for vect_recog_mask_conversion_pattern. Build
5917 : conversion of MASK to a type suitable for masking VECTYPE.
5918 : Built statement gets required vectype and is appended to
5919 : a pattern sequence of STMT_VINFO.
5920 :
5921 : Return converted mask. */
5922 :
5923 : static tree
5924 144232 : build_mask_conversion (vec_info *vinfo,
5925 : tree mask, tree vectype, stmt_vec_info stmt_vinfo)
5926 : {
5927 144232 : gimple *stmt;
5928 144232 : tree masktype, tmp;
5929 :
5930 144232 : masktype = truth_type_for (vectype);
5931 144232 : tmp = vect_recog_temp_ssa_var (TREE_TYPE (masktype), NULL);
5932 144232 : stmt = gimple_build_assign (tmp, CONVERT_EXPR, mask);
5933 144232 : append_pattern_def_seq (vinfo, stmt_vinfo,
5934 144232 : stmt, masktype, TREE_TYPE (vectype));
5935 :
5936 144232 : return tmp;
5937 : }
5938 :
5939 :
5940 : /* Return MASK if MASK is suitable for masking an operation on vectors
5941 : of type VECTYPE, otherwise convert it into such a form and return
5942 : the result. Associate any conversion statements with STMT_INFO's
5943 : pattern. */
5944 :
5945 : static tree
5946 78527 : vect_convert_mask_for_vectype (tree mask, tree vectype,
5947 : stmt_vec_info stmt_info, vec_info *vinfo)
5948 : {
5949 78527 : tree mask_type = integer_type_for_mask (mask, vinfo);
5950 78527 : if (mask_type)
5951 : {
5952 78527 : tree mask_vectype = get_mask_type_for_scalar_type (vinfo, mask_type);
5953 78527 : if (mask_vectype
5954 157054 : && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
5955 95599 : TYPE_VECTOR_SUBPARTS (mask_vectype)))
5956 61455 : mask = build_mask_conversion (vinfo, mask, vectype, stmt_info);
5957 : }
5958 78527 : return mask;
5959 : }
5960 :
5961 :
5962 : /* Function vect_recog_bool_pattern
5963 :
5964 : Try to find pattern like following:
5965 :
5966 : bool a_b, b_b, c_b, d_b, e_b;
5967 : TYPE f_T;
5968 : loop:
5969 : S1 a_b = x1 CMP1 y1;
5970 : S2 b_b = x2 CMP2 y2;
5971 : S3 c_b = a_b & b_b;
5972 : S4 d_b = x3 CMP3 y3;
5973 : S5 e_b = c_b | d_b;
5974 : S6 f_T = (TYPE) e_b;
5975 :
5976 : where type 'TYPE' is an integral type. Or a similar pattern
5977 : ending in
5978 :
5979 : S6 f_Y = e_b ? r_Y : s_Y;
5980 :
5981 : as results from if-conversion of a complex condition.
5982 :
5983 : Input:
5984 :
5985 : * STMT_VINFO: The stmt at the end from which the pattern
5986 : search begins, i.e. cast of a bool to
5987 : an integer type.
5988 :
5989 : Output:
5990 :
5991 : * TYPE_OUT: The type of the output of this pattern.
5992 :
5993 : * Return value: A new stmt that will be used to replace the pattern.
5994 :
5995 : Assuming size of TYPE is the same as size of all comparisons
5996 : (otherwise some casts would be added where needed), the above
5997 : sequence we create related pattern stmts:
5998 : S1' a_T = x1 CMP1 y1 ? 1 : 0;
5999 : S3' c_T = x2 CMP2 y2 ? a_T : 0;
6000 : S4' d_T = x3 CMP3 y3 ? 1 : 0;
6001 : S5' e_T = c_T | d_T;
6002 : S6' f_T = e_T;
6003 :
6004 : Instead of the above S3' we could emit:
6005 : S2' b_T = x2 CMP2 y2 ? 1 : 0;
6006 : S3' c_T = a_T | b_T;
6007 : but the above is more efficient. */
6008 :
6009 : static gimple *
6010 32427327 : vect_recog_bool_pattern (vec_info *vinfo,
6011 : stmt_vec_info stmt_vinfo, tree *type_out)
6012 : {
6013 32427327 : gimple *last_stmt = stmt_vinfo->stmt;
6014 32427327 : enum tree_code rhs_code;
6015 32427327 : tree var, lhs, rhs, vectype;
6016 32427327 : gimple *pattern_stmt;
6017 :
6018 32427327 : if (!is_gimple_assign (last_stmt))
6019 : return NULL;
6020 :
6021 23095328 : var = gimple_assign_rhs1 (last_stmt);
6022 23095328 : lhs = gimple_assign_lhs (last_stmt);
6023 23095328 : rhs_code = gimple_assign_rhs_code (last_stmt);
6024 :
6025 23095328 : if (rhs_code == VIEW_CONVERT_EXPR)
6026 194915 : var = TREE_OPERAND (var, 0);
6027 :
6028 23095328 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (var)))
6029 : return NULL;
6030 :
6031 782674 : hash_set<gimple *> bool_stmts;
6032 :
6033 782674 : if (CONVERT_EXPR_CODE_P (rhs_code)
6034 : || rhs_code == VIEW_CONVERT_EXPR
6035 : || rhs_code == FLOAT_EXPR)
6036 : {
6037 175368 : if (! (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6038 2171 : || SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs)))
6039 173731 : || VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
6040 : return NULL;
6041 81573 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
6042 :
6043 81573 : tree type = integer_type_for_mask (var, vinfo);
6044 81573 : tree cst0, cst1, tmp;
6045 :
6046 81573 : if (!type)
6047 : return NULL;
6048 :
6049 : /* We may directly use cond with narrowed type to avoid multiple cond
6050 : exprs with following result packing and perform single cond with
6051 : packed mask instead. In case of widening we better make cond first
6052 : and then extract results. */
6053 42905 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (lhs)))
6054 29864 : type = TREE_TYPE (lhs);
6055 :
6056 42905 : cst0 = build_int_cst (type, 0);
6057 42905 : cst1 = build_int_cst (type, 1);
6058 42905 : tmp = vect_recog_temp_ssa_var (type, NULL);
6059 42905 : pattern_stmt = gimple_build_assign (tmp, COND_EXPR, var, cst1, cst0);
6060 :
6061 42905 : if (!useless_type_conversion_p (type, TREE_TYPE (lhs)))
6062 : {
6063 13041 : tree new_vectype = get_vectype_for_scalar_type (vinfo, type);
6064 13041 : append_pattern_def_seq (vinfo, stmt_vinfo,
6065 : pattern_stmt, new_vectype);
6066 :
6067 13041 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6068 13041 : pattern_stmt
6069 25768 : = gimple_build_assign (lhs, (rhs_code == FLOAT_EXPR
6070 : ? FLOAT_EXPR : CONVERT_EXPR), tmp);
6071 : }
6072 :
6073 42905 : *type_out = vectype;
6074 42905 : vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
6075 :
6076 42905 : return pattern_stmt;
6077 : }
6078 : else if (rhs_code == COND_EXPR
6079 217047 : && TREE_CODE (var) == SSA_NAME)
6080 : {
6081 217047 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
6082 217047 : if (vectype == NULL_TREE)
6083 : return NULL;
6084 :
6085 : /* Build a scalar type for the boolean result that when
6086 : vectorized matches the vector type of the result in
6087 : size and number of elements. */
6088 201785 : unsigned prec
6089 201785 : = vector_element_size (tree_to_poly_uint64 (TYPE_SIZE (vectype)),
6090 : TYPE_VECTOR_SUBPARTS (vectype));
6091 :
6092 201785 : tree type
6093 403570 : = build_nonstandard_integer_type (prec,
6094 201785 : TYPE_UNSIGNED (TREE_TYPE (var)));
6095 201785 : if (get_vectype_for_scalar_type (vinfo, type) == NULL_TREE)
6096 : return NULL;
6097 :
6098 201785 : enum vect_def_type dt;
6099 201785 : if (integer_type_for_mask (var, vinfo))
6100 : return NULL;
6101 36640 : else if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
6102 36640 : && vect_is_simple_use (var, vinfo, &dt)
6103 36640 : && (dt == vect_external_def
6104 36633 : || dt == vect_constant_def))
6105 : {
6106 : /* If the condition is already a boolean then manually convert it to a
6107 : mask of the given integer type but don't set a vectype. */
6108 1387 : tree lhs_ivar = vect_recog_temp_ssa_var (type, NULL);
6109 1387 : pattern_stmt = gimple_build_assign (lhs_ivar, COND_EXPR, var,
6110 : build_all_ones_cst (type),
6111 : build_zero_cst (type));
6112 1387 : append_inv_pattern_def_seq (vinfo, pattern_stmt);
6113 1387 : var = lhs_ivar;
6114 : }
6115 :
6116 36640 : tree lhs_var = vect_recog_temp_ssa_var (boolean_type_node, NULL);
6117 36640 : pattern_stmt = gimple_build_assign (lhs_var, NE_EXPR, var,
6118 36640 : build_zero_cst (TREE_TYPE (var)));
6119 :
6120 36640 : tree new_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (var));
6121 36640 : if (!new_vectype)
6122 : return NULL;
6123 :
6124 36640 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, new_vectype,
6125 36640 : TREE_TYPE (var));
6126 :
6127 36640 : lhs_var = vect_convert_mask_for_vectype (lhs_var, vectype, stmt_vinfo,
6128 : vinfo);
6129 :
6130 36640 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6131 36640 : pattern_stmt
6132 36640 : = gimple_build_assign (lhs, COND_EXPR, lhs_var,
6133 : gimple_assign_rhs2 (last_stmt),
6134 : gimple_assign_rhs3 (last_stmt));
6135 36640 : *type_out = vectype;
6136 36640 : vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
6137 :
6138 36640 : return pattern_stmt;
6139 : }
6140 477568 : else if (rhs_code == BIT_NOT_EXPR
6141 477568 : && !vect_use_mask_type_p (stmt_vinfo))
6142 : {
6143 : /* When we have a bool data inversion rewrite that to an XOR to
6144 : cope with the fact that we'll use a wider vector element type. */
6145 9387 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6146 9387 : pattern_stmt
6147 9387 : = gimple_build_assign (lhs, BIT_XOR_EXPR, var,
6148 9387 : build_all_ones_cst (TREE_TYPE (var)));
6149 9387 : *type_out = NULL_TREE;
6150 9387 : vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
6151 :
6152 9387 : return pattern_stmt;
6153 : }
6154 468181 : else if ((rhs_code == BIT_XOR_EXPR
6155 : || rhs_code == BIT_AND_EXPR
6156 468181 : || rhs_code == BIT_IOR_EXPR)
6157 374223 : && TREE_CODE (var) == SSA_NAME)
6158 : {
6159 374223 : tree rhs2 = gimple_assign_rhs2 (last_stmt);
6160 374223 : if (TREE_CODE (rhs2) != SSA_NAME)
6161 : return NULL;
6162 374223 : tree lhs_type = integer_type_for_mask (lhs, vinfo);
6163 374223 : if (!lhs_type)
6164 : return NULL;
6165 203040 : vectype = get_mask_type_for_scalar_type (vinfo, lhs_type);
6166 203040 : if (!vectype)
6167 : return NULL;
6168 203040 : vect_def_type dt1, dt2;
6169 203040 : tree rhs1_type = integer_type_for_mask (var, vinfo, &dt1);
6170 203040 : tree rhs2_type = integer_type_for_mask (rhs2, vinfo, &dt2);
6171 203040 : if ((rhs1_type || dt1 == vect_external_def)
6172 188032 : && (rhs2_type || dt2 == vect_external_def))
6173 : return NULL;
6174 : /* When one input is a mask and the other is not create a pattern
6175 : stmt sequence that creates a mask for the non-mask input and
6176 : convert it to one suitable for the output mask used. */
6177 37454 : if (rhs1_type && !rhs2_type)
6178 : {
6179 22446 : tree rhs1_vectype = get_mask_type_for_scalar_type (vinfo, rhs1_type);
6180 22446 : if (!rhs1_vectype)
6181 : return NULL;
6182 22446 : tree rhs2_vectype = get_vectype_for_scalar_type (vinfo,
6183 22446 : TREE_TYPE (rhs2));
6184 22446 : if (!rhs2_vectype)
6185 : return NULL;
6186 22446 : tree new_vectype = truth_type_for (rhs2_vectype);
6187 22446 : tree tem = vect_recog_temp_ssa_var (TREE_TYPE (new_vectype), NULL);
6188 22446 : pattern_stmt = gimple_build_assign (tem, NE_EXPR, rhs2,
6189 : build_zero_cst
6190 22446 : (TREE_TYPE (rhs2)));
6191 22446 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
6192 22446 : new_vectype, TREE_TYPE (new_vectype));
6193 22446 : rhs2 = vect_convert_mask_for_vectype (tem, rhs1_vectype,
6194 : stmt_vinfo, vinfo);
6195 : }
6196 15008 : else if (!rhs1_type && rhs2_type)
6197 : {
6198 15008 : tree rhs2_vectype = get_mask_type_for_scalar_type (vinfo, rhs2_type);
6199 15008 : if (!rhs2_vectype)
6200 : return NULL;
6201 15008 : tree rhs1_vectype = get_vectype_for_scalar_type (vinfo,
6202 15008 : TREE_TYPE (var));
6203 15008 : if (!rhs1_vectype)
6204 : return NULL;
6205 15008 : tree new_vectype = truth_type_for (rhs1_vectype);
6206 15008 : tree tem = vect_recog_temp_ssa_var (TREE_TYPE (new_vectype), NULL);
6207 15008 : pattern_stmt = gimple_build_assign (tem, NE_EXPR, var,
6208 : build_zero_cst
6209 15008 : (TREE_TYPE (var)));
6210 15008 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
6211 15008 : new_vectype, TREE_TYPE (new_vectype));
6212 15008 : var = vect_convert_mask_for_vectype (tem, rhs2_vectype,
6213 : stmt_vinfo, vinfo);
6214 : }
6215 37454 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6216 37454 : pattern_stmt = gimple_build_assign (lhs, rhs_code, var, rhs2);
6217 37454 : vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
6218 37454 : *type_out = vectype;
6219 37454 : return pattern_stmt;
6220 : }
6221 93958 : else if (rhs_code == SSA_NAME
6222 25630 : && STMT_VINFO_DATA_REF (stmt_vinfo))
6223 : {
6224 7807 : stmt_vec_info pattern_stmt_info;
6225 7807 : vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
6226 7807 : if (!vectype || !VECTOR_MODE_P (TYPE_MODE (vectype)))
6227 0 : return NULL;
6228 :
6229 7807 : tree type = integer_type_for_mask (var, vinfo);
6230 7807 : if (!type)
6231 : return NULL;
6232 :
6233 4433 : var = vect_convert_mask_for_vectype (var, vectype, stmt_vinfo, vinfo);
6234 :
6235 4433 : tree cst0 = build_int_cst (TREE_TYPE (vectype), 0);
6236 4433 : tree cst1 = build_int_cst (TREE_TYPE (vectype), 1);
6237 4433 : rhs = vect_recog_temp_ssa_var (TREE_TYPE (vectype), NULL);
6238 4433 : pattern_stmt = gimple_build_assign (rhs, COND_EXPR, var, cst1, cst0);
6239 4433 : append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vectype);
6240 :
6241 4433 : lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vectype), lhs);
6242 4433 : pattern_stmt = gimple_build_assign (lhs, SSA_NAME, rhs);
6243 4433 : pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
6244 4433 : vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
6245 4433 : *type_out = vectype;
6246 4433 : vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
6247 :
6248 4433 : return pattern_stmt;
6249 : }
6250 : else
6251 : return NULL;
6252 782674 : }
6253 :
6254 :
6255 : /* Function vect_recog_mask_conversion_pattern
6256 :
6257 : Try to find statements which require boolean type
6258 : conversion. Additional conversion statements are
6259 : added to handle such cases. For example:
6260 :
6261 : bool m_1, m_2, m_3;
6262 : int i_4, i_5;
6263 : double d_6, d_7;
6264 : char c_1, c_2, c_3;
6265 :
6266 : S1 m_1 = i_4 > i_5;
6267 : S2 m_2 = d_6 < d_7;
6268 : S3 m_3 = m_1 & m_2;
6269 : S4 c_1 = m_3 ? c_2 : c_3;
6270 :
6271 : Will be transformed into:
6272 :
6273 : S1 m_1 = i_4 > i_5;
6274 : S2 m_2 = d_6 < d_7;
6275 : S3'' m_2' = (_Bool[bitsize=32])m_2
6276 : S3' m_3' = m_1 & m_2';
6277 : S4'' m_3'' = (_Bool[bitsize=8])m_3'
6278 : S4' c_1' = m_3'' ? c_2 : c_3; */
6279 :
6280 : static gimple *
6281 32449667 : vect_recog_mask_conversion_pattern (vec_info *vinfo,
6282 : stmt_vec_info stmt_vinfo, tree *type_out)
6283 : {
6284 32449667 : gimple *last_stmt = stmt_vinfo->stmt;
6285 32449667 : enum tree_code rhs_code;
6286 32449667 : tree lhs = NULL_TREE, rhs1, rhs2, tmp, rhs1_type, rhs2_type;
6287 32449667 : tree vectype1, vectype2;
6288 32449667 : stmt_vec_info pattern_stmt_info;
6289 :
6290 : /* Check for MASK_LOAD and MASK_STORE as well as COND_OP calls requiring mask
6291 : conversion. */
6292 32449667 : if (is_gimple_call (last_stmt)
6293 32449667 : && gimple_call_internal_p (last_stmt))
6294 : {
6295 127505 : gcall *pattern_stmt;
6296 :
6297 127505 : internal_fn ifn = gimple_call_internal_fn (last_stmt);
6298 127505 : int mask_argno = internal_fn_mask_index (ifn);
6299 127505 : if (mask_argno < 0)
6300 : return NULL;
6301 :
6302 15099 : bool store_p = internal_store_fn_p (ifn);
6303 15099 : bool load_p = internal_store_fn_p (ifn);
6304 15099 : if (store_p)
6305 : {
6306 2488 : int rhs_index = internal_fn_stored_value_index (ifn);
6307 2488 : tree rhs = gimple_call_arg (last_stmt, rhs_index);
6308 2488 : vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs));
6309 : }
6310 : else
6311 : {
6312 12611 : lhs = gimple_call_lhs (last_stmt);
6313 12611 : if (!lhs)
6314 : return NULL;
6315 12611 : vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
6316 : }
6317 :
6318 15099 : if (!vectype1)
6319 : return NULL;
6320 :
6321 14779 : tree mask_arg = gimple_call_arg (last_stmt, mask_argno);
6322 14779 : tree mask_arg_type = integer_type_for_mask (mask_arg, vinfo);
6323 14779 : if (mask_arg_type)
6324 : {
6325 13138 : vectype2 = get_mask_type_for_scalar_type (vinfo, mask_arg_type);
6326 :
6327 13138 : if (!vectype2
6328 13138 : || known_eq (TYPE_VECTOR_SUBPARTS (vectype1),
6329 : TYPE_VECTOR_SUBPARTS (vectype2)))
6330 8291 : return NULL;
6331 : }
6332 1641 : else if (store_p || load_p)
6333 : return NULL;
6334 :
6335 6220 : tmp = build_mask_conversion (vinfo, mask_arg, vectype1, stmt_vinfo);
6336 :
6337 6220 : auto_vec<tree, 8> args;
6338 6220 : unsigned int nargs = gimple_call_num_args (last_stmt);
6339 6220 : args.safe_grow (nargs, true);
6340 31100 : for (unsigned int i = 0; i < nargs; ++i)
6341 24880 : args[i] = ((int) i == mask_argno
6342 24880 : ? tmp
6343 18660 : : gimple_call_arg (last_stmt, i));
6344 6220 : pattern_stmt = gimple_build_call_internal_vec (ifn, args);
6345 :
6346 6220 : if (!store_p)
6347 : {
6348 5948 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6349 5948 : gimple_call_set_lhs (pattern_stmt, lhs);
6350 : }
6351 :
6352 5948 : if (load_p || store_p)
6353 272 : gimple_call_set_nothrow (pattern_stmt, true);
6354 :
6355 6220 : pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
6356 6220 : if (STMT_VINFO_DATA_REF (stmt_vinfo))
6357 1924 : vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
6358 :
6359 6220 : *type_out = vectype1;
6360 6220 : vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
6361 :
6362 6220 : return pattern_stmt;
6363 6220 : }
6364 :
6365 32322162 : if (!is_gimple_assign (last_stmt))
6366 : return NULL;
6367 :
6368 23117668 : gimple *pattern_stmt;
6369 23117668 : lhs = gimple_assign_lhs (last_stmt);
6370 23117668 : rhs1 = gimple_assign_rhs1 (last_stmt);
6371 23117668 : rhs_code = gimple_assign_rhs_code (last_stmt);
6372 :
6373 : /* Check for cond expression requiring mask conversion. */
6374 23117668 : if (rhs_code == COND_EXPR)
6375 : {
6376 198017 : vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
6377 :
6378 198017 : gcc_assert (! COMPARISON_CLASS_P (rhs1));
6379 198017 : if (TREE_CODE (rhs1) == SSA_NAME)
6380 : {
6381 198017 : rhs1_type = integer_type_for_mask (rhs1, vinfo);
6382 198017 : if (!rhs1_type)
6383 : return NULL;
6384 : }
6385 : else
6386 : return NULL;
6387 :
6388 184872 : vectype2 = get_mask_type_for_scalar_type (vinfo, rhs1_type);
6389 :
6390 184872 : if (!vectype1 || !vectype2)
6391 : return NULL;
6392 :
6393 : /* Continue if a conversion is needed. Also continue if we have
6394 : a comparison whose vector type would normally be different from
6395 : VECTYPE2 when considered in isolation. In that case we'll
6396 : replace the comparison with an SSA name (so that we can record
6397 : its vector type) and behave as though the comparison was an SSA
6398 : name from the outset. */
6399 182755 : if (known_eq (TYPE_VECTOR_SUBPARTS (vectype1),
6400 : TYPE_VECTOR_SUBPARTS (vectype2)))
6401 : return NULL;
6402 :
6403 45400 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
6404 90800 : TYPE_VECTOR_SUBPARTS (vectype2)))
6405 45400 : tmp = build_mask_conversion (vinfo, rhs1, vectype1, stmt_vinfo);
6406 : else
6407 : tmp = rhs1;
6408 :
6409 45400 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6410 45400 : pattern_stmt = gimple_build_assign (lhs, COND_EXPR, tmp,
6411 : gimple_assign_rhs2 (last_stmt),
6412 : gimple_assign_rhs3 (last_stmt));
6413 :
6414 45400 : *type_out = vectype1;
6415 45400 : vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
6416 :
6417 45400 : return pattern_stmt;
6418 : }
6419 :
6420 : /* Now check for binary boolean operations requiring conversion for
6421 : one of operands. */
6422 22919651 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
6423 : return NULL;
6424 :
6425 1906650 : if (rhs_code != BIT_IOR_EXPR
6426 : && rhs_code != BIT_XOR_EXPR
6427 1906650 : && rhs_code != BIT_AND_EXPR
6428 1569881 : && TREE_CODE_CLASS (rhs_code) != tcc_comparison)
6429 : return NULL;
6430 :
6431 1723810 : rhs2 = gimple_assign_rhs2 (last_stmt);
6432 :
6433 1723810 : rhs1_type = integer_type_for_mask (rhs1, vinfo);
6434 1723810 : rhs2_type = integer_type_for_mask (rhs2, vinfo);
6435 :
6436 1723810 : if (!rhs1_type || !rhs2_type
6437 1723810 : || TYPE_PRECISION (rhs1_type) == TYPE_PRECISION (rhs2_type))
6438 : return NULL;
6439 :
6440 31157 : if (TYPE_PRECISION (rhs1_type) < TYPE_PRECISION (rhs2_type))
6441 : {
6442 23576 : vectype1 = get_mask_type_for_scalar_type (vinfo, rhs1_type);
6443 23576 : if (!vectype1)
6444 : return NULL;
6445 23576 : rhs2 = build_mask_conversion (vinfo, rhs2, vectype1, stmt_vinfo);
6446 : }
6447 : else
6448 : {
6449 7581 : vectype1 = get_mask_type_for_scalar_type (vinfo, rhs2_type);
6450 7581 : if (!vectype1)
6451 : return NULL;
6452 7581 : rhs1 = build_mask_conversion (vinfo, rhs1, vectype1, stmt_vinfo);
6453 : }
6454 :
6455 31157 : lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6456 31157 : pattern_stmt = gimple_build_assign (lhs, rhs_code, rhs1, rhs2);
6457 :
6458 31157 : *type_out = vectype1;
6459 31157 : vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
6460 :
6461 31157 : return pattern_stmt;
6462 : }
6463 :
6464 : /* STMT_INFO is a load or store. If the load or store is conditional, return
6465 : the boolean condition under which it occurs, otherwise return null. */
6466 :
6467 : static tree
6468 101478 : vect_get_load_store_mask (stmt_vec_info stmt_info)
6469 : {
6470 101478 : if (gassign *def_assign = dyn_cast <gassign *> (stmt_info->stmt))
6471 : {
6472 100005 : gcc_assert (gimple_assign_single_p (def_assign));
6473 : return NULL_TREE;
6474 : }
6475 :
6476 1473 : if (gcall *def_call = dyn_cast <gcall *> (stmt_info->stmt))
6477 : {
6478 1473 : internal_fn ifn = gimple_call_internal_fn (def_call);
6479 1473 : int mask_index = internal_fn_mask_index (ifn);
6480 1473 : return gimple_call_arg (def_call, mask_index);
6481 : }
6482 :
6483 0 : gcc_unreachable ();
6484 : }
6485 :
6486 : /* Return the equivalent of:
6487 :
6488 : fold_convert (TYPE, VALUE)
6489 :
6490 : with the expectation that the operation will be vectorized.
6491 : If new statements are needed, add them as pattern statements
6492 : to STMT_INFO. */
6493 :
6494 : static tree
6495 0 : vect_add_conversion_to_pattern (vec_info *vinfo,
6496 : tree type, tree value, stmt_vec_info stmt_info)
6497 : {
6498 0 : if (useless_type_conversion_p (type, TREE_TYPE (value)))
6499 : return value;
6500 :
6501 0 : tree new_value = vect_recog_temp_ssa_var (type, NULL);
6502 0 : gassign *conversion = gimple_build_assign (new_value, CONVERT_EXPR, value);
6503 0 : append_pattern_def_seq (vinfo, stmt_info, conversion,
6504 : get_vectype_for_scalar_type (vinfo, type));
6505 0 : return new_value;
6506 : }
6507 :
6508 : /* Try to convert STMT_INFO into a call to a gather load or scatter store
6509 : internal function. Return the final statement on success and set
6510 : *TYPE_OUT to the vector type being loaded or stored.
6511 :
6512 : This function only handles gathers and scatters that were recognized
6513 : as such from the outset (indicated by STMT_VINFO_GATHER_SCATTER_P). */
6514 :
6515 : static gimple *
6516 32449667 : vect_recog_gather_scatter_pattern (vec_info *vinfo,
6517 : stmt_vec_info stmt_info, tree *type_out)
6518 : {
6519 : /* Currently we only support this for loop vectorization. */
6520 37027799 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6521 4578132 : if (!loop_vinfo)
6522 : return NULL;
6523 :
6524 : /* Make sure that we're looking at a gather load or scatter store. */
6525 4578132 : data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
6526 4578132 : if (!dr || !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
6527 : return NULL;
6528 :
6529 : /* Get the boolean that controls whether the load or store happens.
6530 : This is null if the operation is unconditional. */
6531 101478 : tree mask = vect_get_load_store_mask (stmt_info);
6532 :
6533 : /* DR analysis nailed down the vector type for the access. */
6534 101478 : tree gs_vectype = STMT_VINFO_VECTYPE (stmt_info);
6535 :
6536 : /* Make sure that the target supports an appropriate internal
6537 : function for the gather/scatter operation. */
6538 101478 : gather_scatter_info gs_info;
6539 101478 : if (!vect_check_gather_scatter (stmt_info, gs_vectype, loop_vinfo, &gs_info)
6540 101478 : || gs_info.ifn == IFN_LAST)
6541 : return NULL;
6542 :
6543 : /* Convert the mask to the right form. */
6544 0 : if (mask)
6545 0 : mask = vect_convert_mask_for_vectype (mask, gs_vectype, stmt_info,
6546 : loop_vinfo);
6547 0 : else if (gs_info.ifn == IFN_MASK_SCATTER_STORE
6548 0 : || gs_info.ifn == IFN_MASK_GATHER_LOAD
6549 0 : || gs_info.ifn == IFN_MASK_LEN_SCATTER_STORE
6550 0 : || gs_info.ifn == IFN_MASK_LEN_GATHER_LOAD)
6551 0 : mask = build_int_cst (TREE_TYPE (truth_type_for (gs_vectype)), -1);
6552 :
6553 : /* Get the invariant base and non-invariant offset, converting the
6554 : latter to the same width as the vector elements. */
6555 0 : tree base = gs_info.base;
6556 0 : tree offset_type = TREE_TYPE (gs_info.offset_vectype);
6557 0 : tree offset = vect_add_conversion_to_pattern (vinfo, offset_type,
6558 : gs_info.offset, stmt_info);
6559 :
6560 : /* Build the new pattern statement. */
6561 0 : tree scale = size_int (gs_info.scale);
6562 0 : gcall *pattern_stmt;
6563 :
6564 0 : if (DR_IS_READ (dr))
6565 : {
6566 0 : tree zero = build_zero_cst (gs_info.element_type);
6567 0 : if (mask != NULL)
6568 : {
6569 0 : int elsval = MASK_LOAD_ELSE_ZERO;
6570 :
6571 0 : tree vec_els
6572 0 : = vect_get_mask_load_else (elsval, TREE_TYPE (gs_vectype));
6573 0 : pattern_stmt = gimple_build_call_internal (gs_info.ifn, 7, base,
6574 : gs_info.alias_ptr,
6575 : offset, scale, zero, mask,
6576 : vec_els);
6577 : }
6578 : else
6579 0 : pattern_stmt = gimple_build_call_internal (gs_info.ifn, 5, base,
6580 : gs_info.alias_ptr,
6581 : offset, scale, zero);
6582 0 : tree lhs = gimple_get_lhs (stmt_info->stmt);
6583 0 : tree load_lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
6584 0 : gimple_call_set_lhs (pattern_stmt, load_lhs);
6585 : }
6586 : else
6587 : {
6588 0 : tree rhs = vect_get_store_rhs (stmt_info);
6589 0 : if (mask != NULL)
6590 0 : pattern_stmt = gimple_build_call_internal (gs_info.ifn, 6,
6591 : base, gs_info.alias_ptr,
6592 : offset, scale, rhs, mask);
6593 : else
6594 0 : pattern_stmt = gimple_build_call_internal (gs_info.ifn, 5,
6595 : base, gs_info.alias_ptr,
6596 : offset, scale, rhs);
6597 : }
6598 0 : gimple_call_set_nothrow (pattern_stmt, true);
6599 :
6600 : /* Copy across relevant vectorization info and associate DR with the
6601 : new pattern statement instead of the original statement. */
6602 0 : stmt_vec_info pattern_stmt_info = loop_vinfo->add_stmt (pattern_stmt);
6603 0 : loop_vinfo->move_dr (pattern_stmt_info, stmt_info);
6604 :
6605 0 : *type_out = gs_vectype;
6606 0 : vect_pattern_detected ("gather/scatter pattern", stmt_info->stmt);
6607 :
6608 0 : return pattern_stmt;
6609 : }
6610 :
6611 : /* Helper method of vect_recog_cond_store_pattern, checks to see if COND_ARG
6612 : is points to a load statement that reads the same data as that of
6613 : STORE_VINFO. */
6614 :
6615 : static bool
6616 35579 : vect_cond_store_pattern_same_ref (vec_info *vinfo,
6617 : stmt_vec_info store_vinfo, tree cond_arg)
6618 : {
6619 35579 : stmt_vec_info load_stmt_vinfo = vinfo->lookup_def (cond_arg);
6620 35579 : if (!load_stmt_vinfo
6621 20509 : || !STMT_VINFO_DATA_REF (load_stmt_vinfo)
6622 12349 : || DR_IS_WRITE (STMT_VINFO_DATA_REF (load_stmt_vinfo))
6623 47928 : || !same_data_refs (STMT_VINFO_DATA_REF (store_vinfo),
6624 : STMT_VINFO_DATA_REF (load_stmt_vinfo)))
6625 26292 : return false;
6626 :
6627 : return true;
6628 : }
6629 :
6630 : /* Function vect_recog_cond_store_pattern
6631 :
6632 : Try to find the following pattern:
6633 :
6634 : x = *_3;
6635 : c = a CMP b;
6636 : y = c ? t_20 : x;
6637 : *_3 = y;
6638 :
6639 : where the store of _3 happens on a conditional select on a value loaded
6640 : from the same location. In such case we can elide the initial load if
6641 : MASK_STORE is supported and instead only conditionally write out the result.
6642 :
6643 : The pattern produces for the above:
6644 :
6645 : c = a CMP b;
6646 : .MASK_STORE (_3, c, t_20)
6647 :
6648 : Input:
6649 :
6650 : * STMT_VINFO: The stmt from which the pattern search begins. In the
6651 : example, when this function is called with _3 then the search begins.
6652 :
6653 : Output:
6654 :
6655 : * TYPE_OUT: The type of the output of this pattern.
6656 :
6657 : * Return value: A new stmt that will be used to replace the sequence. */
6658 :
6659 : static gimple *
6660 32449667 : vect_recog_cond_store_pattern (vec_info *vinfo,
6661 : stmt_vec_info stmt_vinfo, tree *type_out)
6662 : {
6663 32449667 : loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6664 4578132 : if (!loop_vinfo)
6665 : return NULL;
6666 :
6667 4578132 : gimple *store_stmt = STMT_VINFO_STMT (stmt_vinfo);
6668 :
6669 : /* Needs to be a gimple store where we have DR info for. */
6670 4578132 : if (!STMT_VINFO_DATA_REF (stmt_vinfo)
6671 1095912 : || DR_IS_READ (STMT_VINFO_DATA_REF (stmt_vinfo))
6672 4964863 : || !gimple_store_p (store_stmt))
6673 4193789 : return NULL;
6674 :
6675 384343 : tree st_rhs = gimple_assign_rhs1 (store_stmt);
6676 :
6677 384343 : if (TREE_CODE (st_rhs) != SSA_NAME)
6678 : return NULL;
6679 :
6680 298515 : auto cond_vinfo = vinfo->lookup_def (st_rhs);
6681 :
6682 : /* If the condition isn't part of the loop then bool recog wouldn't have seen
6683 : it and so this transformation may not be valid. */
6684 298515 : if (!cond_vinfo)
6685 : return NULL;
6686 :
6687 280938 : cond_vinfo = vect_stmt_to_vectorize (cond_vinfo);
6688 32716356 : gassign *cond_stmt = dyn_cast<gassign *> (STMT_VINFO_STMT (cond_vinfo));
6689 348980 : if (!cond_stmt || gimple_assign_rhs_code (cond_stmt) != COND_EXPR)
6690 : return NULL;
6691 :
6692 : /* Check if the else value matches the original loaded one. */
6693 18825 : bool invert = false;
6694 18825 : tree cmp_ls = gimple_arg (cond_stmt, 0);
6695 18825 : if (TREE_CODE (cmp_ls) != SSA_NAME)
6696 : return NULL;
6697 :
6698 18825 : tree cond_arg1 = gimple_arg (cond_stmt, 1);
6699 18825 : tree cond_arg2 = gimple_arg (cond_stmt, 2);
6700 :
6701 18825 : if (!vect_cond_store_pattern_same_ref (vinfo, stmt_vinfo, cond_arg2)
6702 18825 : && !(invert = vect_cond_store_pattern_same_ref (vinfo, stmt_vinfo,
6703 : cond_arg1)))
6704 : return NULL;
6705 :
6706 9287 : vect_pattern_detected ("vect_recog_cond_store_pattern", store_stmt);
6707 :
6708 9287 : tree scalar_type = TREE_TYPE (st_rhs);
6709 9287 : if (VECTOR_TYPE_P (scalar_type))
6710 : return NULL;
6711 :
6712 9287 : tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
6713 9287 : if (vectype == NULL_TREE)
6714 : return NULL;
6715 :
6716 9287 : machine_mode mask_mode;
6717 9287 : machine_mode vecmode = TYPE_MODE (vectype);
6718 1858 : if (!VECTOR_MODE_P (vecmode)
6719 9287 : || targetm.vectorize.conditional_operation_is_expensive (IFN_MASK_STORE)
6720 0 : || !targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
6721 9287 : || !can_vec_mask_load_store_p (vecmode, mask_mode, false))
6722 9287 : return NULL;
6723 :
6724 0 : tree base = DR_REF (STMT_VINFO_DATA_REF (stmt_vinfo));
6725 0 : if (may_be_nonaddressable_p (base))
6726 : return NULL;
6727 :
6728 : /* We need to use the false parameter of the conditional select. */
6729 0 : tree cond_store_arg = invert ? cond_arg2 : cond_arg1;
6730 0 : tree cond_load_arg = invert ? cond_arg1 : cond_arg2;
6731 0 : gimple *load_stmt = SSA_NAME_DEF_STMT (cond_load_arg);
6732 :
6733 : /* This is a rough estimation to check that there aren't any aliasing stores
6734 : in between the load and store. It's a bit strict, but for now it's good
6735 : enough. */
6736 0 : if (gimple_vuse (load_stmt) != gimple_vuse (store_stmt))
6737 : return NULL;
6738 :
6739 : /* If we have to invert the condition, i.e. use the true argument rather than
6740 : the false argument, we have to negate the mask. */
6741 0 : if (invert)
6742 : {
6743 0 : tree var = vect_recog_temp_ssa_var (boolean_type_node, NULL);
6744 :
6745 : /* Invert the mask using ^ 1. */
6746 0 : tree itype = TREE_TYPE (cmp_ls);
6747 0 : gassign *conv = gimple_build_assign (var, BIT_XOR_EXPR, cmp_ls,
6748 : build_int_cst (itype, 1));
6749 :
6750 0 : tree mask_vec_type = get_mask_type_for_scalar_type (vinfo, itype);
6751 0 : append_pattern_def_seq (vinfo, stmt_vinfo, conv, mask_vec_type, itype);
6752 0 : cmp_ls= var;
6753 : }
6754 :
6755 0 : if (TREE_CODE (base) != MEM_REF)
6756 0 : base = build_fold_addr_expr (base);
6757 :
6758 0 : tree ptr = build_int_cst (reference_alias_ptr_type (base),
6759 0 : get_object_alignment (base));
6760 :
6761 : /* Convert the mask to the right form. */
6762 0 : tree mask = vect_convert_mask_for_vectype (cmp_ls, vectype, stmt_vinfo,
6763 : vinfo);
6764 :
6765 0 : gcall *call
6766 0 : = gimple_build_call_internal (IFN_MASK_STORE, 4, base, ptr, mask,
6767 : cond_store_arg);
6768 0 : gimple_set_location (call, gimple_location (store_stmt));
6769 :
6770 : /* Copy across relevant vectorization info and associate DR with the
6771 : new pattern statement instead of the original statement. */
6772 0 : stmt_vec_info pattern_stmt_info = loop_vinfo->add_stmt (call);
6773 0 : loop_vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
6774 :
6775 0 : *type_out = vectype;
6776 0 : return call;
6777 : }
6778 :
6779 : /* Return true if TYPE is a non-boolean integer type. These are the types
6780 : that we want to consider for narrowing. */
6781 :
6782 : static bool
6783 66543862 : vect_narrowable_type_p (tree type)
6784 : {
6785 66543862 : return INTEGRAL_TYPE_P (type) && !VECT_SCALAR_BOOLEAN_TYPE_P (type);
6786 : }
6787 :
6788 : /* Return true if the operation given by CODE can be truncated to N bits
6789 : when only N bits of the output are needed. This is only true if bit N+1
6790 : of the inputs has no effect on the low N bits of the result. */
6791 :
6792 : static bool
6793 16646165 : vect_truncatable_operation_p (tree_code code)
6794 : {
6795 16646165 : switch (code)
6796 : {
6797 : case NEGATE_EXPR:
6798 : case PLUS_EXPR:
6799 : case MINUS_EXPR:
6800 : case MULT_EXPR:
6801 : case BIT_NOT_EXPR:
6802 : case BIT_AND_EXPR:
6803 : case BIT_IOR_EXPR:
6804 : case BIT_XOR_EXPR:
6805 : case COND_EXPR:
6806 : return true;
6807 :
6808 6487916 : default:
6809 6487916 : return false;
6810 : }
6811 : }
6812 :
6813 : /* Record that STMT_INFO could be changed from operating on TYPE to
6814 : operating on a type with the precision and sign given by PRECISION
6815 : and SIGN respectively. PRECISION is an arbitrary bit precision;
6816 : it might not be a whole number of bytes. */
6817 :
6818 : static void
6819 2593919 : vect_set_operation_type (stmt_vec_info stmt_info, tree type,
6820 : unsigned int precision, signop sign)
6821 : {
6822 : /* Round the precision up to a whole number of bytes. */
6823 2593919 : precision = vect_element_precision (precision);
6824 2593919 : if (precision < TYPE_PRECISION (type)
6825 2593919 : && (!stmt_info->operation_precision
6826 40895 : || stmt_info->operation_precision > precision))
6827 : {
6828 1673660 : stmt_info->operation_precision = precision;
6829 1673660 : stmt_info->operation_sign = sign;
6830 : }
6831 2593919 : }
6832 :
6833 : /* Record that STMT_INFO only requires MIN_INPUT_PRECISION from its
6834 : non-boolean inputs, all of which have type TYPE. MIN_INPUT_PRECISION
6835 : is an arbitrary bit precision; it might not be a whole number of bytes. */
6836 :
6837 : static void
6838 11941725 : vect_set_min_input_precision (stmt_vec_info stmt_info, tree type,
6839 : unsigned int min_input_precision)
6840 : {
6841 : /* This operation in isolation only requires the inputs to have
6842 : MIN_INPUT_PRECISION of precision, However, that doesn't mean
6843 : that MIN_INPUT_PRECISION is a natural precision for the chain
6844 : as a whole. E.g. consider something like:
6845 :
6846 : unsigned short *x, *y;
6847 : *y = ((*x & 0xf0) >> 4) | (*y << 4);
6848 :
6849 : The right shift can be done on unsigned chars, and only requires the
6850 : result of "*x & 0xf0" to be done on unsigned chars. But taking that
6851 : approach would mean turning a natural chain of single-vector unsigned
6852 : short operations into one that truncates "*x" and then extends
6853 : "(*x & 0xf0) >> 4", with two vectors for each unsigned short
6854 : operation and one vector for each unsigned char operation.
6855 : This would be a significant pessimization.
6856 :
6857 : Instead only propagate the maximum of this precision and the precision
6858 : required by the users of the result. This means that we don't pessimize
6859 : the case above but continue to optimize things like:
6860 :
6861 : unsigned char *y;
6862 : unsigned short *x;
6863 : *y = ((*x & 0xf0) >> 4) | (*y << 4);
6864 :
6865 : Here we would truncate two vectors of *x to a single vector of
6866 : unsigned chars and use single-vector unsigned char operations for
6867 : everything else, rather than doing two unsigned short copies of
6868 : "(*x & 0xf0) >> 4" and then truncating the result. */
6869 11941725 : min_input_precision = MAX (min_input_precision,
6870 : stmt_info->min_output_precision);
6871 :
6872 11941725 : if (min_input_precision < TYPE_PRECISION (type)
6873 11941725 : && (!stmt_info->min_input_precision
6874 62731 : || stmt_info->min_input_precision > min_input_precision))
6875 595769 : stmt_info->min_input_precision = min_input_precision;
6876 11941725 : }
6877 :
6878 : /* Subroutine of vect_determine_min_output_precision. Return true if
6879 : we can calculate a reduced number of output bits for STMT_INFO,
6880 : whose result is LHS. */
6881 :
6882 : static bool
6883 15240425 : vect_determine_min_output_precision_1 (vec_info *vinfo,
6884 : stmt_vec_info stmt_info, tree lhs)
6885 : {
6886 : /* Take the maximum precision required by users of the result. */
6887 15240425 : unsigned int precision = 0;
6888 15240425 : imm_use_iterator iter;
6889 15240425 : use_operand_p use;
6890 31481480 : FOR_EACH_IMM_USE_FAST (use, iter, lhs)
6891 : {
6892 15957109 : gimple *use_stmt = USE_STMT (use);
6893 15957109 : if (is_gimple_debug (use_stmt))
6894 713825 : continue;
6895 15243284 : stmt_vec_info use_stmt_info = vinfo->lookup_stmt (use_stmt);
6896 15243284 : if (!use_stmt_info || !use_stmt_info->min_input_precision)
6897 : return false;
6898 : /* The input precision recorded for COND_EXPRs applies only to the
6899 : "then" and "else" values. */
6900 287365 : gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
6901 256496 : if (assign
6902 256496 : && gimple_assign_rhs_code (assign) == COND_EXPR
6903 560 : && use->use != gimple_assign_rhs2_ptr (assign)
6904 560 : && use->use != gimple_assign_rhs3_ptr (assign))
6905 : return false;
6906 1002358 : precision = MAX (precision, use_stmt_info->min_input_precision);
6907 14956479 : }
6908 :
6909 283946 : if (dump_enabled_p ())
6910 5884 : dump_printf_loc (MSG_NOTE, vect_location,
6911 : "only the low %d bits of %T are significant\n",
6912 : precision, lhs);
6913 283946 : stmt_info->min_output_precision = precision;
6914 283946 : return true;
6915 : }
6916 :
6917 : /* Calculate min_output_precision for STMT_INFO. */
6918 :
6919 : static void
6920 39109769 : vect_determine_min_output_precision (vec_info *vinfo, stmt_vec_info stmt_info)
6921 : {
6922 : /* We're only interested in statements with a narrowable result. */
6923 39109769 : tree lhs = gimple_get_lhs (stmt_info->stmt);
6924 39109769 : if (!lhs
6925 30926765 : || TREE_CODE (lhs) != SSA_NAME
6926 65313379 : || !vect_narrowable_type_p (TREE_TYPE (lhs)))
6927 : return;
6928 :
6929 15240425 : if (!vect_determine_min_output_precision_1 (vinfo, stmt_info, lhs))
6930 14956479 : stmt_info->min_output_precision = TYPE_PRECISION (TREE_TYPE (lhs));
6931 : }
6932 :
6933 : /* Use range information to decide whether STMT (described by STMT_INFO)
6934 : could be done in a narrower type. This is effectively a forward
6935 : propagation, since it uses context-independent information that applies
6936 : to all users of an SSA name. */
6937 :
6938 : static void
6939 22388179 : vect_determine_precisions_from_range (stmt_vec_info stmt_info, gassign *stmt)
6940 : {
6941 22388179 : tree lhs = gimple_assign_lhs (stmt);
6942 22388179 : if (!lhs || TREE_CODE (lhs) != SSA_NAME)
6943 19907633 : return;
6944 :
6945 17952073 : tree type = TREE_TYPE (lhs);
6946 17952073 : if (!vect_narrowable_type_p (type))
6947 : return;
6948 :
6949 : /* First see whether we have any useful range information for the result. */
6950 11804283 : unsigned int precision = TYPE_PRECISION (type);
6951 11804283 : signop sign = TYPE_SIGN (type);
6952 11804283 : wide_int min_value, max_value;
6953 11804283 : if (!vect_get_range_info (lhs, &min_value, &max_value))
6954 : return;
6955 :
6956 5768407 : tree_code code = gimple_assign_rhs_code (stmt);
6957 5768407 : unsigned int nops = gimple_num_ops (stmt);
6958 :
6959 5768407 : if (!vect_truncatable_operation_p (code))
6960 : {
6961 : /* Handle operations that can be computed in type T if all inputs
6962 : and outputs can be represented in type T. Also handle left and
6963 : right shifts, where (in addition) the maximum shift amount must
6964 : be less than the number of bits in T. */
6965 2118768 : bool is_shift;
6966 2118768 : switch (code)
6967 : {
6968 : case LSHIFT_EXPR:
6969 : case RSHIFT_EXPR:
6970 : is_shift = true;
6971 : break;
6972 :
6973 312344 : case ABS_EXPR:
6974 312344 : case MIN_EXPR:
6975 312344 : case MAX_EXPR:
6976 312344 : case TRUNC_DIV_EXPR:
6977 312344 : case CEIL_DIV_EXPR:
6978 312344 : case FLOOR_DIV_EXPR:
6979 312344 : case ROUND_DIV_EXPR:
6980 312344 : case EXACT_DIV_EXPR:
6981 : /* Modulus is excluded because it is typically calculated by doing
6982 : a division, for which minimum signed / -1 isn't representable in
6983 : the original signed type. We could take the division range into
6984 : account instead, if handling modulus ever becomes important. */
6985 312344 : is_shift = false;
6986 312344 : break;
6987 :
6988 : default:
6989 : return;
6990 : }
6991 1445570 : for (unsigned int i = 1; i < nops; ++i)
6992 : {
6993 1115749 : tree op = gimple_op (stmt, i);
6994 1115749 : wide_int op_min_value, op_max_value;
6995 1115749 : if (TREE_CODE (op) == INTEGER_CST)
6996 : {
6997 323858 : unsigned int op_precision = TYPE_PRECISION (TREE_TYPE (op));
6998 323858 : op_min_value = op_max_value = wi::to_wide (op, op_precision);
6999 : }
7000 791891 : else if (TREE_CODE (op) == SSA_NAME)
7001 : {
7002 791891 : if (!vect_get_range_info (op, &op_min_value, &op_max_value))
7003 : return;
7004 : }
7005 : else
7006 : return;
7007 :
7008 741171 : if (is_shift && i == 2)
7009 : {
7010 : /* There needs to be one more bit than the maximum shift amount.
7011 :
7012 : If the maximum shift amount is already 1 less than PRECISION
7013 : then we can't narrow the shift further. Dealing with that
7014 : case first ensures that we can safely use an unsigned range
7015 : below.
7016 :
7017 : op_min_value isn't relevant, since shifts by negative amounts
7018 : are UB. */
7019 210469 : if (wi::geu_p (op_max_value, precision - 1))
7020 : return;
7021 185548 : unsigned int min_bits = op_max_value.to_uhwi () + 1;
7022 :
7023 : /* As explained below, we can convert a signed shift into an
7024 : unsigned shift if the sign bit is always clear. At this
7025 : point we've already processed the ranges of the output and
7026 : the first input. */
7027 185548 : auto op_sign = sign;
7028 185548 : if (sign == SIGNED && !wi::neg_p (min_value))
7029 : op_sign = UNSIGNED;
7030 371096 : op_min_value = wide_int::from (wi::min_value (min_bits, op_sign),
7031 185548 : precision, op_sign);
7032 371096 : op_max_value = wide_int::from (wi::max_value (min_bits, op_sign),
7033 185548 : precision, op_sign);
7034 : }
7035 716250 : min_value = wi::min (min_value, op_min_value, sign);
7036 716250 : max_value = wi::max (max_value, op_max_value, sign);
7037 1115749 : }
7038 : }
7039 :
7040 : /* Try to switch signed types for unsigned types if we can.
7041 : This is better for two reasons. First, unsigned ops tend
7042 : to be cheaper than signed ops. Second, it means that we can
7043 : handle things like:
7044 :
7045 : signed char c;
7046 : int res = (int) c & 0xff00; // range [0x0000, 0xff00]
7047 :
7048 : as:
7049 :
7050 : signed char c;
7051 : unsigned short res_1 = (unsigned short) c & 0xff00;
7052 : int res = (int) res_1;
7053 :
7054 : where the intermediate result res_1 has unsigned rather than
7055 : signed type. */
7056 3979460 : if (sign == SIGNED && !wi::neg_p (min_value))
7057 : sign = UNSIGNED;
7058 :
7059 : /* See what precision is required for MIN_VALUE and MAX_VALUE. */
7060 3979460 : unsigned int precision1 = wi::min_precision (min_value, sign);
7061 3979460 : unsigned int precision2 = wi::min_precision (max_value, sign);
7062 3979460 : unsigned int value_precision = MAX (precision1, precision2);
7063 3979460 : if (value_precision >= precision)
7064 : return;
7065 :
7066 2480546 : if (dump_enabled_p ())
7067 111930 : dump_printf_loc (MSG_NOTE, vect_location, "can narrow to %s:%d"
7068 : " without loss of precision: %G",
7069 : sign == SIGNED ? "signed" : "unsigned",
7070 : value_precision, (gimple *) stmt);
7071 :
7072 2480546 : vect_set_operation_type (stmt_info, type, value_precision, sign);
7073 2480546 : vect_set_min_input_precision (stmt_info, type, value_precision);
7074 11804283 : }
7075 :
7076 : /* Use information about the users of STMT's result to decide whether
7077 : STMT (described by STMT_INFO) could be done in a narrower type.
7078 : This is effectively a backward propagation. */
7079 :
7080 : static void
7081 22388179 : vect_determine_precisions_from_users (stmt_vec_info stmt_info, gassign *stmt)
7082 : {
7083 22388179 : tree_code code = gimple_assign_rhs_code (stmt);
7084 22388179 : unsigned int opno = (code == COND_EXPR ? 2 : 1);
7085 22388179 : tree type = TREE_TYPE (gimple_op (stmt, opno));
7086 22388179 : if (!vect_narrowable_type_p (type))
7087 12927000 : return;
7088 :
7089 13866177 : unsigned int precision = TYPE_PRECISION (type);
7090 13866177 : unsigned int operation_precision, min_input_precision;
7091 13866177 : switch (code)
7092 : {
7093 2500199 : CASE_CONVERT:
7094 : /* Only the bits that contribute to the output matter. Don't change
7095 : the precision of the operation itself. */
7096 2500199 : operation_precision = precision;
7097 2500199 : min_input_precision = stmt_info->min_output_precision;
7098 2500199 : break;
7099 :
7100 488220 : case LSHIFT_EXPR:
7101 488220 : case RSHIFT_EXPR:
7102 488220 : {
7103 488220 : tree shift = gimple_assign_rhs2 (stmt);
7104 488220 : unsigned int min_const_shift, max_const_shift;
7105 488220 : wide_int min_shift, max_shift;
7106 488220 : if (TREE_CODE (shift) == SSA_NAME
7107 107443 : && vect_get_range_info (shift, &min_shift, &max_shift)
7108 82649 : && wi::ge_p (min_shift, 0, TYPE_SIGN (TREE_TYPE (shift)))
7109 568115 : && wi::lt_p (max_shift, TYPE_PRECISION (type),
7110 79895 : TYPE_SIGN (TREE_TYPE (shift))))
7111 : {
7112 71701 : min_const_shift = min_shift.to_uhwi ();
7113 71701 : max_const_shift = max_shift.to_uhwi ();
7114 : }
7115 416519 : else if (TREE_CODE (shift) == INTEGER_CST
7116 797296 : && wi::ltu_p (wi::to_widest (shift), precision))
7117 380669 : min_const_shift = max_const_shift = TREE_INT_CST_LOW (shift);
7118 : else
7119 35850 : return;
7120 452370 : if (code == LSHIFT_EXPR)
7121 : {
7122 : /* Avoid creating an undefined shift.
7123 :
7124 : ??? We could instead use min_output_precision as-is and
7125 : optimize out-of-range shifts to zero. However, only
7126 : degenerate testcases shift away all their useful input data,
7127 : and it isn't natural to drop input operations in the middle
7128 : of vectorization. This sort of thing should really be
7129 : handled before vectorization. */
7130 109360 : operation_precision = MAX (stmt_info->min_output_precision,
7131 : max_const_shift + 1);
7132 : /* We need CONST_SHIFT fewer bits of the input. */
7133 109360 : min_input_precision = (MAX (operation_precision, max_const_shift)
7134 : - min_const_shift);
7135 : }
7136 : else
7137 : {
7138 : /* We need CONST_SHIFT extra bits to do the operation. */
7139 343010 : operation_precision = (stmt_info->min_output_precision
7140 : + max_const_shift);
7141 343010 : min_input_precision = operation_precision;
7142 : }
7143 452370 : break;
7144 488220 : }
7145 :
7146 10877758 : default:
7147 10877758 : if (vect_truncatable_operation_p (code))
7148 : {
7149 : /* Input bit N has no effect on output bits N-1 and lower. */
7150 6508610 : operation_precision = stmt_info->min_output_precision;
7151 6508610 : min_input_precision = operation_precision;
7152 6508610 : break;
7153 : }
7154 : return;
7155 : }
7156 :
7157 9461179 : if (operation_precision < precision)
7158 : {
7159 113373 : if (dump_enabled_p ())
7160 2803 : dump_printf_loc (MSG_NOTE, vect_location, "can narrow to %s:%d"
7161 : " without affecting users: %G",
7162 2803 : TYPE_UNSIGNED (type) ? "unsigned" : "signed",
7163 : operation_precision, (gimple *) stmt);
7164 226746 : vect_set_operation_type (stmt_info, type, operation_precision,
7165 113373 : TYPE_SIGN (type));
7166 : }
7167 9461179 : vect_set_min_input_precision (stmt_info, type, min_input_precision);
7168 : }
7169 :
7170 : /* Return true if the statement described by STMT_INFO sets a boolean
7171 : SSA_NAME and if we know how to vectorize this kind of statement using
7172 : vector mask types. */
7173 :
7174 : static bool
7175 40204487 : possible_vector_mask_operation_p (stmt_vec_info stmt_info)
7176 : {
7177 40204487 : tree lhs = gimple_get_lhs (stmt_info->stmt);
7178 40204487 : tree_code code = ERROR_MARK;
7179 40204487 : gassign *assign = NULL;
7180 40204487 : gcond *cond = NULL;
7181 :
7182 40204487 : if ((assign = dyn_cast <gassign *> (stmt_info->stmt)))
7183 23169624 : code = gimple_assign_rhs_code (assign);
7184 17034863 : else if ((cond = dyn_cast <gcond *> (stmt_info->stmt)))
7185 : {
7186 5434616 : lhs = gimple_cond_lhs (cond);
7187 5434616 : code = gimple_cond_code (cond);
7188 : }
7189 :
7190 40204487 : if (!lhs
7191 37361765 : || TREE_CODE (lhs) != SSA_NAME
7192 72804442 : || !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
7193 : return false;
7194 :
7195 2261052 : if (code != ERROR_MARK)
7196 : {
7197 2005081 : switch (code)
7198 : {
7199 : CASE_CONVERT:
7200 : case SSA_NAME:
7201 : case BIT_NOT_EXPR:
7202 : case BIT_IOR_EXPR:
7203 : case BIT_XOR_EXPR:
7204 : case BIT_AND_EXPR:
7205 : return true;
7206 :
7207 1565523 : default:
7208 1565523 : return TREE_CODE_CLASS (code) == tcc_comparison;
7209 : }
7210 : }
7211 255971 : else if (is_a <gphi *> (stmt_info->stmt))
7212 144862 : return true;
7213 : return false;
7214 : }
7215 :
7216 : /* If STMT_INFO sets a boolean SSA_NAME, see whether we should use
7217 : a vector mask type instead of a normal vector type. Record the
7218 : result in STMT_INFO->mask_precision. Returns true when the
7219 : precision changed. */
7220 :
7221 : static bool
7222 40204487 : vect_determine_mask_precision (vec_info *vinfo, stmt_vec_info stmt_info)
7223 : {
7224 40204487 : if (!possible_vector_mask_operation_p (stmt_info))
7225 : return false;
7226 :
7227 : /* If at least one boolean input uses a vector mask type,
7228 : pick the mask type with the narrowest elements.
7229 :
7230 : ??? This is the traditional behavior. It should always produce
7231 : the smallest number of operations, but isn't necessarily the
7232 : optimal choice. For example, if we have:
7233 :
7234 : a = b & c
7235 :
7236 : where:
7237 :
7238 : - the user of a wants it to have a mask type for 16-bit elements (M16)
7239 : - b also uses M16
7240 : - c uses a mask type for 8-bit elements (M8)
7241 :
7242 : then picking M8 gives:
7243 :
7244 : - 1 M16->M8 pack for b
7245 : - 1 M8 AND for a
7246 : - 2 M8->M16 unpacks for the user of a
7247 :
7248 : whereas picking M16 would have given:
7249 :
7250 : - 2 M8->M16 unpacks for c
7251 : - 2 M16 ANDs for a
7252 :
7253 : The number of operations are equal, but M16 would have given
7254 : a shorter dependency chain and allowed more ILP. */
7255 2098301 : unsigned int precision = ~0U;
7256 2098301 : gimple *stmt = STMT_VINFO_STMT (stmt_info);
7257 :
7258 : /* If the statement compares two values that shouldn't use vector masks,
7259 : try comparing the values as normal scalars instead. */
7260 2098301 : tree_code code = ERROR_MARK;
7261 2098301 : tree op0_type;
7262 2098301 : unsigned int nops = -1;
7263 2098301 : unsigned int ops_start = 0;
7264 :
7265 2098301 : if (gassign *assign = dyn_cast <gassign *> (stmt))
7266 : {
7267 1384775 : code = gimple_assign_rhs_code (assign);
7268 1384775 : op0_type = TREE_TYPE (gimple_assign_rhs1 (assign));
7269 1384775 : nops = gimple_num_ops (assign);
7270 1384775 : ops_start = 1;
7271 : }
7272 713526 : else if (gcond *cond = dyn_cast <gcond *> (stmt))
7273 : {
7274 568664 : code = gimple_cond_code (cond);
7275 568664 : op0_type = TREE_TYPE (gimple_cond_lhs (cond));
7276 568664 : nops = 2;
7277 568664 : ops_start = 0;
7278 : }
7279 :
7280 1953439 : if (code != ERROR_MARK)
7281 : {
7282 5816264 : for (unsigned int i = ops_start; i < nops; ++i)
7283 : {
7284 3862825 : tree rhs = gimple_op (stmt, i);
7285 3862825 : if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (rhs)))
7286 1900464 : continue;
7287 :
7288 1962361 : stmt_vec_info def_stmt_info = vinfo->lookup_def (rhs);
7289 1962361 : if (!def_stmt_info)
7290 : /* Don't let external or constant operands influence the choice.
7291 : We can convert them to whichever vector type we pick. */
7292 584066 : continue;
7293 :
7294 1378295 : if (def_stmt_info->mask_precision)
7295 : {
7296 1155173 : if (precision > def_stmt_info->mask_precision)
7297 3862825 : precision = def_stmt_info->mask_precision;
7298 : }
7299 : }
7300 :
7301 1953439 : if (precision == ~0U
7302 1562619 : && TREE_CODE_CLASS (code) == tcc_comparison)
7303 : {
7304 1354706 : scalar_mode mode;
7305 1354706 : tree vectype, mask_type;
7306 1354706 : if (is_a <scalar_mode> (TYPE_MODE (op0_type), &mode)
7307 : /* Do not allow this to set vinfo->vector_mode, this might
7308 : disrupt the result for the next iteration. */
7309 1354706 : && (vectype = get_related_vectype_for_scalar_type
7310 1620278 : (vinfo->vector_mode, op0_type))
7311 1186932 : && (mask_type = truth_type_for (vectype))
7312 1186932 : && expand_vec_cmp_expr_p (vectype, mask_type, code))
7313 1842720 : precision = GET_MODE_BITSIZE (mode);
7314 : }
7315 : }
7316 : else
7317 : {
7318 144862 : gphi *phi = as_a <gphi *> (stmt_info->stmt);
7319 593482 : for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
7320 : {
7321 448620 : tree rhs = gimple_phi_arg_def (phi, i);
7322 :
7323 448620 : stmt_vec_info def_stmt_info = vinfo->lookup_def (rhs);
7324 448620 : if (!def_stmt_info)
7325 : /* Don't let external or constant operands influence the choice.
7326 : We can convert them to whichever vector type we pick. */
7327 291699 : continue;
7328 :
7329 156921 : if (def_stmt_info->mask_precision)
7330 : {
7331 131626 : if (precision > def_stmt_info->mask_precision)
7332 448620 : precision = def_stmt_info->mask_precision;
7333 : }
7334 : }
7335 : }
7336 :
7337 2098301 : if (stmt_info->mask_precision != precision)
7338 : {
7339 1969305 : if (dump_enabled_p ())
7340 : {
7341 7819 : if (precision == ~0U)
7342 1746 : dump_printf_loc (MSG_NOTE, vect_location,
7343 : "using normal nonmask vectors for %G",
7344 : stmt_info->stmt);
7345 : else
7346 6073 : dump_printf_loc (MSG_NOTE, vect_location,
7347 : "using boolean precision %d for %G",
7348 : precision, stmt_info->stmt);
7349 : }
7350 :
7351 : /* ??? We'd like to assert stmt_info->mask_precision == 0
7352 : || stmt_info->mask_precision > precision, thus that we only
7353 : decrease mask precisions throughout iteration, but the
7354 : tcc_comparison handling above means for comparisons of bools
7355 : we start with 8 but might increase in case the bools get mask
7356 : precision on their own. */
7357 1969305 : stmt_info->mask_precision = precision;
7358 1969305 : return true;
7359 : }
7360 : return false;
7361 : }
7362 :
7363 : /* Handle vect_determine_precisions for STMT_INFO, given that we
7364 : have already done so for the users of its result. */
7365 :
7366 : void
7367 39109769 : vect_determine_stmt_precisions (vec_info *vinfo, stmt_vec_info stmt_info)
7368 : {
7369 39109769 : vect_determine_min_output_precision (vinfo, stmt_info);
7370 39109769 : if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
7371 : {
7372 22388179 : vect_determine_precisions_from_range (stmt_info, stmt);
7373 22388179 : vect_determine_precisions_from_users (stmt_info, stmt);
7374 : }
7375 39109769 : }
7376 :
7377 : /* Walk backwards through the vectorizable region to determine the
7378 : values of these fields:
7379 :
7380 : - min_output_precision
7381 : - min_input_precision
7382 : - operation_precision
7383 : - operation_sign. */
7384 :
7385 : void
7386 1112883 : vect_determine_precisions (vec_info *vinfo)
7387 : {
7388 1112883 : basic_block *bbs = vinfo->bbs;
7389 1112883 : unsigned int nbbs = vinfo->nbbs;
7390 :
7391 1132435 : DUMP_VECT_SCOPE ("vect_determine_precisions");
7392 :
7393 : /* For mask precisions we have to iterate since otherwise we do not
7394 : get reduction PHI precision correct. For now do this only for
7395 : loop vectorization. */
7396 1183664 : bool changed;
7397 1183664 : do
7398 : {
7399 1183664 : changed = false;
7400 13227029 : for (unsigned int i = 0; i < nbbs; i++)
7401 : {
7402 12043365 : basic_block bb = bbs[i];
7403 12043365 : for (auto gsi = gsi_start_phis (bb);
7404 19454077 : !gsi_end_p (gsi); gsi_next (&gsi))
7405 : {
7406 7410712 : stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi.phi ());
7407 7410712 : if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
7408 7219091 : changed |= vect_determine_mask_precision (vinfo, stmt_info);
7409 : }
7410 127731708 : for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7411 : {
7412 103644978 : stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (gsi));
7413 103644978 : if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
7414 32985396 : changed |= vect_determine_mask_precision (vinfo, stmt_info);
7415 : }
7416 : }
7417 : }
7418 2296547 : while (changed && is_a <loop_vec_info> (vinfo));
7419 :
7420 12993232 : for (unsigned int i = 0; i < nbbs; i++)
7421 : {
7422 11880349 : basic_block bb = bbs[nbbs - i - 1];
7423 227732002 : for (auto gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
7424 : {
7425 101985652 : stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (gsi));
7426 101985652 : if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
7427 32096132 : vect_determine_stmt_precisions (vinfo, stmt_info);
7428 : }
7429 19085607 : for (auto gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7430 : {
7431 7205258 : stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi.phi ());
7432 7205258 : if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
7433 7013637 : vect_determine_stmt_precisions (vinfo, stmt_info);
7434 : }
7435 : }
7436 1112883 : }
7437 :
7438 : typedef gimple *(*vect_recog_func_ptr) (vec_info *, stmt_vec_info, tree *);
7439 :
7440 : struct vect_recog_func
7441 : {
7442 : vect_recog_func_ptr fn;
7443 : const char *name;
7444 : };
7445 :
7446 : /* Note that ordering matters - the first pattern matching on a stmt is
7447 : taken which means usually the more complex one needs to precede the
7448 : less comples onex (widen_sum only after dot_prod or sad for example). */
7449 : static vect_recog_func vect_vect_recog_func_ptrs[] = {
7450 : { vect_recog_bitfield_ref_pattern, "bitfield_ref" },
7451 : { vect_recog_bit_insert_pattern, "bit_insert" },
7452 : { vect_recog_abd_pattern, "abd" },
7453 : { vect_recog_over_widening_pattern, "over_widening" },
7454 : /* Must come after over_widening, which narrows the shift as much as
7455 : possible beforehand. */
7456 : { vect_recog_average_pattern, "average" },
7457 : { vect_recog_cond_expr_convert_pattern, "cond_expr_convert" },
7458 : { vect_recog_mulhs_pattern, "mult_high" },
7459 : { vect_recog_cast_forwprop_pattern, "cast_forwprop" },
7460 : { vect_recog_widen_mult_pattern, "widen_mult" },
7461 : { vect_recog_dot_prod_pattern, "dot_prod" },
7462 : { vect_recog_sad_pattern, "sad" },
7463 : { vect_recog_widen_sum_pattern, "widen_sum" },
7464 : { vect_recog_pow_pattern, "pow" },
7465 : { vect_recog_popcount_clz_ctz_ffs_pattern, "popcount_clz_ctz_ffs" },
7466 : { vect_recog_ctz_ffs_pattern, "ctz_ffs" },
7467 : { vect_recog_widen_shift_pattern, "widen_shift" },
7468 : { vect_recog_rotate_pattern, "rotate" },
7469 : { vect_recog_vector_vector_shift_pattern, "vector_vector_shift" },
7470 : { vect_recog_divmod_pattern, "divmod" },
7471 : { vect_recog_mod_var_pattern, "modvar" },
7472 : { vect_recog_mult_pattern, "mult" },
7473 : { vect_recog_sat_add_pattern, "sat_add" },
7474 : { vect_recog_sat_sub_pattern, "sat_sub" },
7475 : { vect_recog_sat_trunc_pattern, "sat_trunc" },
7476 : { vect_recog_gcond_pattern, "gcond" },
7477 : { vect_recog_bool_pattern, "bool" },
7478 : /* This must come before mask conversion, and includes the parts
7479 : of mask conversion that are needed for gather and scatter
7480 : internal functions. */
7481 : { vect_recog_gather_scatter_pattern, "gather_scatter" },
7482 : { vect_recog_cond_store_pattern, "cond_store" },
7483 : { vect_recog_mask_conversion_pattern, "mask_conversion" },
7484 : { vect_recog_widen_plus_pattern, "widen_plus" },
7485 : { vect_recog_widen_minus_pattern, "widen_minus" },
7486 : { vect_recog_widen_abd_pattern, "widen_abd" },
7487 : /* These must come after the double widening ones. */
7488 : };
7489 :
7490 : /* Mark statements that are involved in a pattern. */
7491 :
7492 : void
7493 1063141 : vect_mark_pattern_stmts (vec_info *vinfo,
7494 : stmt_vec_info orig_stmt_info, gimple *pattern_stmt,
7495 : tree pattern_vectype)
7496 : {
7497 1063141 : stmt_vec_info orig_stmt_info_saved = orig_stmt_info;
7498 1063141 : gimple *def_seq = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info);
7499 :
7500 1063141 : gimple *orig_pattern_stmt = NULL;
7501 1063141 : if (is_pattern_stmt_p (orig_stmt_info))
7502 : {
7503 : /* We're replacing a statement in an existing pattern definition
7504 : sequence. */
7505 11440 : orig_pattern_stmt = orig_stmt_info->stmt;
7506 11440 : if (dump_enabled_p ())
7507 666 : dump_printf_loc (MSG_NOTE, vect_location,
7508 : "replacing earlier pattern %G", orig_pattern_stmt);
7509 :
7510 : /* To keep the book-keeping simple, just swap the lhs of the
7511 : old and new statements, so that the old one has a valid but
7512 : unused lhs. */
7513 11440 : tree old_lhs = gimple_get_lhs (orig_pattern_stmt);
7514 11440 : gimple_set_lhs (orig_pattern_stmt, gimple_get_lhs (pattern_stmt));
7515 11440 : gimple_set_lhs (pattern_stmt, old_lhs);
7516 :
7517 11440 : if (dump_enabled_p ())
7518 666 : dump_printf_loc (MSG_NOTE, vect_location, "with %G", pattern_stmt);
7519 :
7520 : /* Switch to the statement that ORIG replaces. */
7521 11440 : orig_stmt_info = STMT_VINFO_RELATED_STMT (orig_stmt_info);
7522 :
7523 : /* We shouldn't be replacing the main pattern statement. */
7524 11440 : gcc_assert (STMT_VINFO_RELATED_STMT (orig_stmt_info)->stmt
7525 : != orig_pattern_stmt);
7526 : }
7527 :
7528 1063141 : if (def_seq)
7529 : for (gimple_stmt_iterator si = gsi_start (def_seq);
7530 2332394 : !gsi_end_p (si); gsi_next (&si))
7531 : {
7532 1405280 : if (dump_enabled_p ())
7533 24798 : dump_printf_loc (MSG_NOTE, vect_location,
7534 : "extra pattern stmt: %G", gsi_stmt (si));
7535 1405280 : stmt_vec_info pattern_stmt_info
7536 1405280 : = vect_init_pattern_stmt (vinfo, gsi_stmt (si),
7537 : orig_stmt_info, pattern_vectype);
7538 : /* Stmts in the def sequence are not vectorizable cycle or
7539 : induction defs, instead they should all be vect_internal_def
7540 : feeding the main pattern stmt which retains this def type. */
7541 1405280 : STMT_VINFO_DEF_TYPE (pattern_stmt_info) = vect_internal_def;
7542 : }
7543 :
7544 1063141 : if (orig_pattern_stmt)
7545 : {
7546 11440 : vect_init_pattern_stmt (vinfo, pattern_stmt,
7547 : orig_stmt_info, pattern_vectype);
7548 :
7549 : /* Insert all the new pattern statements before the original one. */
7550 11440 : gimple_seq *orig_def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info);
7551 11440 : gimple_stmt_iterator gsi = gsi_for_stmt (orig_pattern_stmt,
7552 : orig_def_seq);
7553 11440 : gsi_insert_seq_before_without_update (&gsi, def_seq, GSI_SAME_STMT);
7554 11440 : gsi_insert_before_without_update (&gsi, pattern_stmt, GSI_SAME_STMT);
7555 :
7556 : /* Remove the pattern statement that this new pattern replaces. */
7557 11440 : gsi_remove (&gsi, false);
7558 : }
7559 : else
7560 1051701 : vect_set_pattern_stmt (vinfo,
7561 : pattern_stmt, orig_stmt_info, pattern_vectype);
7562 :
7563 : /* For any conditionals mark them as vect_condition_def. */
7564 1063141 : if (is_a <gcond *> (pattern_stmt))
7565 376306 : STMT_VINFO_DEF_TYPE (STMT_VINFO_RELATED_STMT (orig_stmt_info)) = vect_condition_def;
7566 :
7567 : /* Transfer reduction path info to the pattern. */
7568 1063141 : if (STMT_VINFO_REDUC_IDX (orig_stmt_info_saved) != -1)
7569 : {
7570 16064 : gimple_match_op op;
7571 16064 : if (!gimple_extract_op (orig_stmt_info_saved->stmt, &op))
7572 0 : gcc_unreachable ();
7573 16064 : tree lookfor = op.ops[STMT_VINFO_REDUC_IDX (orig_stmt_info)];
7574 : /* Search the pattern def sequence and the main pattern stmt. Note
7575 : we may have inserted all into a containing pattern def sequence
7576 : so the following is a bit awkward. */
7577 16064 : gimple_stmt_iterator si;
7578 16064 : gimple *s;
7579 16064 : if (def_seq)
7580 : {
7581 14923 : si = gsi_start (def_seq);
7582 14923 : s = gsi_stmt (si);
7583 14923 : gsi_next (&si);
7584 : }
7585 : else
7586 : {
7587 : si = gsi_none ();
7588 : s = pattern_stmt;
7589 : }
7590 33800 : do
7591 : {
7592 33800 : bool found = false;
7593 33800 : if (gimple_extract_op (s, &op))
7594 : {
7595 82328 : for (unsigned i = 0; i < op.num_ops; ++i)
7596 64592 : if (op.ops[i] == lookfor)
7597 : {
7598 16064 : STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
7599 16064 : lookfor = gimple_get_lhs (s);
7600 16064 : found = true;
7601 16064 : break;
7602 : }
7603 : /* Try harder to find a mid-entry into an earlier pattern
7604 : sequence. Likewise an entry to a stmt skipping a conversion
7605 : on an input. This means that the initial 'lookfor' was
7606 : bogus. */
7607 16064 : if (!found)
7608 : {
7609 38479 : for (unsigned i = 0; i < op.num_ops; ++i)
7610 20743 : if (TREE_CODE (op.ops[i]) == SSA_NAME)
7611 17736 : if (auto def = vinfo->lookup_def (op.ops[i]))
7612 17543 : if (vect_is_reduction (def)
7613 17543 : || (is_a <gphi *> (def->stmt)
7614 0 : && STMT_VINFO_REDUC_DEF (def) != NULL))
7615 : {
7616 0 : STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
7617 0 : lookfor = gimple_get_lhs (s);
7618 0 : found = true;
7619 0 : break;
7620 : }
7621 : }
7622 : }
7623 33800 : if (s == pattern_stmt)
7624 : {
7625 16064 : if (!found && dump_enabled_p ())
7626 0 : dump_printf_loc (MSG_NOTE, vect_location,
7627 : "failed to update reduction index.\n");
7628 16064 : break;
7629 : }
7630 17736 : if (gsi_end_p (si))
7631 : s = pattern_stmt;
7632 : else
7633 : {
7634 2813 : s = gsi_stmt (si);
7635 2813 : if (s == pattern_stmt)
7636 : /* Found the end inside a bigger pattern def seq. */
7637 : si = gsi_none ();
7638 : else
7639 2813 : gsi_next (&si);
7640 : }
7641 : } while (1);
7642 : }
7643 1063141 : }
7644 :
7645 : /* Function vect_pattern_recog_1
7646 :
7647 : Input:
7648 : PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
7649 : computation pattern.
7650 : STMT_INFO: A stmt from which the pattern search should start.
7651 :
7652 : If PATTERN_RECOG_FUNC successfully detected the pattern, it creates
7653 : a sequence of statements that has the same functionality and can be
7654 : used to replace STMT_INFO. It returns the last statement in the sequence
7655 : and adds any earlier statements to STMT_INFO's STMT_VINFO_PATTERN_DEF_SEQ.
7656 : PATTERN_RECOG_FUNC also sets *TYPE_OUT to the vector type of the final
7657 : statement, having first checked that the target supports the new operation
7658 : in that type.
7659 :
7660 : This function also does some bookkeeping, as explained in the documentation
7661 : for vect_recog_pattern. */
7662 :
7663 : static void
7664 1045935192 : vect_pattern_recog_1 (vec_info *vinfo,
7665 : const vect_recog_func &recog_func, stmt_vec_info stmt_info)
7666 : {
7667 1045935192 : gimple *pattern_stmt;
7668 1045935192 : tree pattern_vectype;
7669 :
7670 : /* If this statement has already been replaced with pattern statements,
7671 : leave the original statement alone, since the first match wins.
7672 : Instead try to match against the definition statements that feed
7673 : the main pattern statement. */
7674 1045935192 : if (STMT_VINFO_IN_PATTERN_P (stmt_info))
7675 : {
7676 13384604 : gimple_stmt_iterator gsi;
7677 13384604 : for (gsi = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
7678 32243572 : !gsi_end_p (gsi); gsi_next (&gsi))
7679 18858968 : vect_pattern_recog_1 (vinfo, recog_func,
7680 : vinfo->lookup_stmt (gsi_stmt (gsi)));
7681 : return;
7682 : }
7683 :
7684 1032550588 : gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
7685 1032550588 : pattern_stmt = recog_func.fn (vinfo, stmt_info, &pattern_vectype);
7686 1032550588 : if (!pattern_stmt)
7687 : {
7688 : /* Clear any half-formed pattern definition sequence. */
7689 1031487447 : STMT_VINFO_PATTERN_DEF_SEQ (stmt_info) = NULL;
7690 1031487447 : return;
7691 : }
7692 :
7693 : /* Found a vectorizable pattern. */
7694 1063141 : if (dump_enabled_p ())
7695 19088 : dump_printf_loc (MSG_NOTE, vect_location,
7696 : "%s pattern recognized: %G",
7697 19088 : recog_func.name, pattern_stmt);
7698 :
7699 : /* Mark the stmts that are involved in the pattern. */
7700 1063141 : vect_mark_pattern_stmts (vinfo, stmt_info, pattern_stmt, pattern_vectype);
7701 : }
7702 :
7703 :
7704 : /* Function vect_pattern_recog
7705 :
7706 : Input:
7707 : LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
7708 : computation idioms.
7709 :
7710 : Output - for each computation idiom that is detected we create a new stmt
7711 : that provides the same functionality and that can be vectorized. We
7712 : also record some information in the struct_stmt_info of the relevant
7713 : stmts, as explained below:
7714 :
7715 : At the entry to this function we have the following stmts, with the
7716 : following initial value in the STMT_VINFO fields:
7717 :
7718 : stmt in_pattern_p related_stmt vec_stmt
7719 : S1: a_i = .... - - -
7720 : S2: a_2 = ..use(a_i).. - - -
7721 : S3: a_1 = ..use(a_2).. - - -
7722 : S4: a_0 = ..use(a_1).. - - -
7723 : S5: ... = ..use(a_0).. - - -
7724 :
7725 : Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
7726 : represented by a single stmt. We then:
7727 : - create a new stmt S6 equivalent to the pattern (the stmt is not
7728 : inserted into the code)
7729 : - fill in the STMT_VINFO fields as follows:
7730 :
7731 : in_pattern_p related_stmt vec_stmt
7732 : S1: a_i = .... - - -
7733 : S2: a_2 = ..use(a_i).. - - -
7734 : S3: a_1 = ..use(a_2).. - - -
7735 : S4: a_0 = ..use(a_1).. true S6 -
7736 : '---> S6: a_new = .... - S4 -
7737 : S5: ... = ..use(a_0).. - - -
7738 :
7739 : (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
7740 : to each other through the RELATED_STMT field).
7741 :
7742 : S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
7743 : of S4 because it will replace all its uses. Stmts {S1,S2,S3} will
7744 : remain irrelevant unless used by stmts other than S4.
7745 :
7746 : If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
7747 : (because they are marked as irrelevant). It will vectorize S6, and record
7748 : a pointer to the new vector stmt VS6 from S6 (as usual).
7749 : S4 will be skipped, and S5 will be vectorized as usual:
7750 :
7751 : in_pattern_p related_stmt vec_stmt
7752 : S1: a_i = .... - - -
7753 : S2: a_2 = ..use(a_i).. - - -
7754 : S3: a_1 = ..use(a_2).. - - -
7755 : > VS6: va_new = .... - - -
7756 : S4: a_0 = ..use(a_1).. true S6 VS6
7757 : '---> S6: a_new = .... - S4 VS6
7758 : > VS5: ... = ..vuse(va_new).. - - -
7759 : S5: ... = ..use(a_0).. - - -
7760 :
7761 : DCE could then get rid of {S1,S2,S3,S4,S5} (if their defs are not used
7762 : elsewhere), and we'll end up with:
7763 :
7764 : VS6: va_new = ....
7765 : VS5: ... = ..vuse(va_new)..
7766 :
7767 : In case of more than one pattern statements, e.g., widen-mult with
7768 : intermediate type:
7769 :
7770 : S1 a_t = ;
7771 : S2 a_T = (TYPE) a_t;
7772 : '--> S3: a_it = (interm_type) a_t;
7773 : S4 prod_T = a_T * CONST;
7774 : '--> S5: prod_T' = a_it w* CONST;
7775 :
7776 : there may be other users of a_T outside the pattern. In that case S2 will
7777 : be marked as relevant (as well as S3), and both S2 and S3 will be analyzed
7778 : and vectorized. The vector stmt VS2 will be recorded in S2, and VS3 will
7779 : be recorded in S3. */
7780 :
7781 : void
7782 1112883 : vect_pattern_recog (vec_info *vinfo)
7783 : {
7784 1112883 : basic_block *bbs = vinfo->bbs;
7785 1112883 : unsigned int nbbs = vinfo->nbbs;
7786 :
7787 1112883 : vect_determine_precisions (vinfo);
7788 :
7789 1112883 : DUMP_VECT_SCOPE ("vect_pattern_recog");
7790 :
7791 : /* Scan through the stmts in the region, applying the pattern recognition
7792 : functions starting at each stmt visited. */
7793 12993232 : for (unsigned i = 0; i < nbbs; i++)
7794 : {
7795 11880349 : basic_block bb = bbs[i];
7796 :
7797 125746350 : for (auto si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
7798 : {
7799 101985652 : stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (si));
7800 :
7801 101985652 : if (!stmt_info || !STMT_VINFO_VECTORIZABLE (stmt_info))
7802 69889520 : continue;
7803 :
7804 : /* Scan over all generic vect_recog_xxx_pattern functions. */
7805 1059172356 : for (const auto &func_ptr : vect_vect_recog_func_ptrs)
7806 1027076224 : vect_pattern_recog_1 (vinfo, func_ptr,
7807 : stmt_info);
7808 : }
7809 : }
7810 :
7811 : /* After this no more add_stmt calls are allowed. */
7812 1112883 : vinfo->stmt_vec_info_ro = true;
7813 1112883 : }
7814 :
7815 : /* Build a GIMPLE_ASSIGN or GIMPLE_CALL with the tree_code,
7816 : or internal_fn contained in ch, respectively. */
7817 : gimple *
7818 166933 : vect_gimple_build (tree lhs, code_helper ch, tree op0, tree op1)
7819 : {
7820 166933 : gcc_assert (op0 != NULL_TREE);
7821 166933 : if (ch.is_tree_code ())
7822 166933 : return gimple_build_assign (lhs, (tree_code) ch, op0, op1);
7823 :
7824 0 : gcc_assert (ch.is_internal_fn ());
7825 0 : gimple* stmt = gimple_build_call_internal (as_internal_fn ((combined_fn) ch),
7826 : op1 == NULL_TREE ? 1 : 2,
7827 : op0, op1);
7828 0 : gimple_call_set_lhs (stmt, lhs);
7829 0 : return stmt;
7830 : }
|