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