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