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