Debian Codegraph @

gcc/tree-vect-patterns.cc

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