1 /* Analysis Utilities for Loop Vectorization.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
3 Contributed by Dorit Nuzman <dorit@il.ibm.com>
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
35 #include "tree-data-ref.h"
36 #include "tree-vectorizer.h"
37 #include "recog.h" /* FIXME: for insn_data */
38 #include "diagnostic-core.h"
41 /* Pattern recognition functions */
42 static gimple
vect_recog_widen_sum_pattern (vec
<gimple
> *, tree
*,
44 static gimple
vect_recog_widen_mult_pattern (vec
<gimple
> *, tree
*,
46 static gimple
vect_recog_dot_prod_pattern (vec
<gimple
> *, tree
*,
48 static gimple
vect_recog_pow_pattern (vec
<gimple
> *, tree
*, tree
*);
49 static gimple
vect_recog_over_widening_pattern (vec
<gimple
> *, tree
*,
51 static gimple
vect_recog_widen_shift_pattern (vec
<gimple
> *,
53 static gimple
vect_recog_vector_vector_shift_pattern (vec
<gimple
> *,
55 static gimple
vect_recog_divmod_pattern (vec
<gimple
> *,
57 static gimple
vect_recog_mixed_size_cond_pattern (vec
<gimple
> *,
59 static gimple
vect_recog_bool_pattern (vec
<gimple
> *, tree
*, tree
*);
60 static vect_recog_func_ptr vect_vect_recog_func_ptrs
[NUM_PATTERNS
] = {
61 vect_recog_widen_mult_pattern
,
62 vect_recog_widen_sum_pattern
,
63 vect_recog_dot_prod_pattern
,
64 vect_recog_pow_pattern
,
65 vect_recog_widen_shift_pattern
,
66 vect_recog_over_widening_pattern
,
67 vect_recog_vector_vector_shift_pattern
,
68 vect_recog_divmod_pattern
,
69 vect_recog_mixed_size_cond_pattern
,
70 vect_recog_bool_pattern
};
73 append_pattern_def_seq (stmt_vec_info stmt_info
, gimple stmt
)
75 gimple_seq_add_stmt_without_update (&STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
),
80 new_pattern_def_seq (stmt_vec_info stmt_info
, gimple stmt
)
82 STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
) = NULL
;
83 append_pattern_def_seq (stmt_info
, stmt
);
86 /* Check whether STMT2 is in the same loop or basic block as STMT1.
87 Which of the two applies depends on whether we're currently doing
88 loop-based or basic-block-based vectorization, as determined by
89 the vinfo_for_stmt for STMT1 (which must be defined).
91 If this returns true, vinfo_for_stmt for STMT2 is guaranteed
92 to be defined as well. */
95 vect_same_loop_or_bb_p (gimple stmt1
, gimple stmt2
)
97 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt1
);
98 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
99 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
101 if (!gimple_bb (stmt2
))
106 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
107 if (!flow_bb_inside_loop_p (loop
, gimple_bb (stmt2
)))
112 if (gimple_bb (stmt2
) != BB_VINFO_BB (bb_vinfo
)
113 || gimple_code (stmt2
) == GIMPLE_PHI
)
117 gcc_assert (vinfo_for_stmt (stmt2
));
121 /* If the LHS of DEF_STMT has a single use, and that statement is
122 in the same loop or basic block, return it. */
125 vect_single_imm_use (gimple def_stmt
)
127 tree lhs
= gimple_assign_lhs (def_stmt
);
131 if (!single_imm_use (lhs
, &use_p
, &use_stmt
))
134 if (!vect_same_loop_or_bb_p (def_stmt
, use_stmt
))
140 /* Check whether NAME, an ssa-name used in USE_STMT,
141 is a result of a type promotion or demotion, such that:
142 DEF_STMT: NAME = NOP (name0)
143 where the type of name0 (ORIG_TYPE) is smaller/bigger than the type of NAME.
144 If CHECK_SIGN is TRUE, check that either both types are signed or both are
148 type_conversion_p (tree name
, gimple use_stmt
, bool check_sign
,
149 tree
*orig_type
, gimple
*def_stmt
, bool *promotion
)
153 loop_vec_info loop_vinfo
;
154 stmt_vec_info stmt_vinfo
;
155 tree type
= TREE_TYPE (name
);
157 enum vect_def_type dt
;
159 bb_vec_info bb_vinfo
;
161 stmt_vinfo
= vinfo_for_stmt (use_stmt
);
162 loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
163 bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
164 if (!vect_is_simple_use (name
, use_stmt
, loop_vinfo
, bb_vinfo
, def_stmt
,
168 if (dt
!= vect_internal_def
169 && dt
!= vect_external_def
&& dt
!= vect_constant_def
)
175 if (!is_gimple_assign (*def_stmt
))
178 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (*def_stmt
)))
181 oprnd0
= gimple_assign_rhs1 (*def_stmt
);
183 *orig_type
= TREE_TYPE (oprnd0
);
184 if (!INTEGRAL_TYPE_P (type
) || !INTEGRAL_TYPE_P (*orig_type
)
185 || ((TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (*orig_type
)) && check_sign
))
188 if (TYPE_PRECISION (type
) >= (TYPE_PRECISION (*orig_type
) * 2))
190 else if (TYPE_PRECISION (*orig_type
) >= (TYPE_PRECISION (type
) * 2))
195 if (!vect_is_simple_use (oprnd0
, *def_stmt
, loop_vinfo
,
196 bb_vinfo
, &dummy_gimple
, &dummy
, &dt
))
202 /* Helper to return a new temporary for pattern of TYPE for STMT. If STMT
203 is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */
206 vect_recog_temp_ssa_var (tree type
, gimple stmt
)
208 return make_temp_ssa_name (type
, stmt
, "patt");
211 /* Function vect_recog_dot_prod_pattern
213 Try to find the following pattern:
219 sum_0 = phi <init, sum_1>
222 S3 x_T = (TYPE1) x_t;
223 S4 y_T = (TYPE1) y_t;
225 [S6 prod = (TYPE2) prod; #optional]
226 S7 sum_1 = prod + sum_0;
228 where 'TYPE1' is exactly double the size of type 'type', and 'TYPE2' is the
229 same size of 'TYPE1' or bigger. This is a special case of a reduction
234 * STMTS: Contains a stmt from which the pattern search begins. In the
235 example, when this function is called with S7, the pattern {S3,S4,S5,S6,S7}
240 * TYPE_IN: The type of the input arguments to the pattern.
242 * TYPE_OUT: The type of the output of this pattern.
244 * Return value: A new stmt that will be used to replace the sequence of
245 stmts that constitute the pattern. In this case it will be:
246 WIDEN_DOT_PRODUCT <x_t, y_t, sum_0>
248 Note: The dot-prod idiom is a widening reduction pattern that is
249 vectorized without preserving all the intermediate results. It
250 produces only N/2 (widened) results (by summing up pairs of
251 intermediate results) rather than all N results. Therefore, we
252 cannot allow this pattern when we want to get all the results and in
253 the correct order (as is the case when this computation is in an
254 inner-loop nested in an outer-loop that us being vectorized). */
257 vect_recog_dot_prod_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
260 gimple stmt
, last_stmt
= (*stmts
)[0];
262 tree oprnd00
, oprnd01
;
263 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
264 tree type
, half_type
;
267 loop_vec_info loop_info
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
275 loop
= LOOP_VINFO_LOOP (loop_info
);
277 if (!is_gimple_assign (last_stmt
))
280 type
= gimple_expr_type (last_stmt
);
282 /* Look for the following pattern
286 DDPROD = (TYPE2) DPROD;
287 sum_1 = DDPROD + sum_0;
289 - DX is double the size of X
290 - DY is double the size of Y
291 - DX, DY, DPROD all have the same type
292 - sum is the same size of DPROD or bigger
293 - sum has been recognized as a reduction variable.
295 This is equivalent to:
296 DPROD = X w* Y; #widen mult
297 sum_1 = DPROD w+ sum_0; #widen summation
299 DPROD = X w* Y; #widen mult
300 sum_1 = DPROD + sum_0; #summation
303 /* Starting from LAST_STMT, follow the defs of its uses in search
304 of the above pattern. */
306 if (gimple_assign_rhs_code (last_stmt
) != PLUS_EXPR
)
309 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
311 /* Has been detected as widening-summation? */
313 stmt
= STMT_VINFO_RELATED_STMT (stmt_vinfo
);
314 type
= gimple_expr_type (stmt
);
315 if (gimple_assign_rhs_code (stmt
) != WIDEN_SUM_EXPR
)
317 oprnd0
= gimple_assign_rhs1 (stmt
);
318 oprnd1
= gimple_assign_rhs2 (stmt
);
319 half_type
= TREE_TYPE (oprnd0
);
325 if (STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_reduction_def
)
327 oprnd0
= gimple_assign_rhs1 (last_stmt
);
328 oprnd1
= gimple_assign_rhs2 (last_stmt
);
329 if (!types_compatible_p (TREE_TYPE (oprnd0
), type
)
330 || !types_compatible_p (TREE_TYPE (oprnd1
), type
))
334 if (type_conversion_p (oprnd0
, stmt
, true, &half_type
, &def_stmt
,
339 oprnd0
= gimple_assign_rhs1 (stmt
);
345 /* So far so good. Since last_stmt was detected as a (summation) reduction,
346 we know that oprnd1 is the reduction variable (defined by a loop-header
347 phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
348 Left to check that oprnd0 is defined by a (widen_)mult_expr */
349 if (TREE_CODE (oprnd0
) != SSA_NAME
)
352 prod_type
= half_type
;
353 stmt
= SSA_NAME_DEF_STMT (oprnd0
);
355 /* It could not be the dot_prod pattern if the stmt is outside the loop. */
356 if (!gimple_bb (stmt
) || !flow_bb_inside_loop_p (loop
, gimple_bb (stmt
)))
359 /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
360 inside the loop (in case we are analyzing an outer-loop). */
361 if (!is_gimple_assign (stmt
))
363 stmt_vinfo
= vinfo_for_stmt (stmt
);
364 gcc_assert (stmt_vinfo
);
365 if (STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_internal_def
)
367 if (gimple_assign_rhs_code (stmt
) != MULT_EXPR
)
369 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
371 /* Has been detected as a widening multiplication? */
373 stmt
= STMT_VINFO_RELATED_STMT (stmt_vinfo
);
374 if (gimple_assign_rhs_code (stmt
) != WIDEN_MULT_EXPR
)
376 stmt_vinfo
= vinfo_for_stmt (stmt
);
377 gcc_assert (stmt_vinfo
);
378 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_internal_def
);
379 oprnd00
= gimple_assign_rhs1 (stmt
);
380 oprnd01
= gimple_assign_rhs2 (stmt
);
384 tree half_type0
, half_type1
;
388 oprnd0
= gimple_assign_rhs1 (stmt
);
389 oprnd1
= gimple_assign_rhs2 (stmt
);
390 if (!types_compatible_p (TREE_TYPE (oprnd0
), prod_type
)
391 || !types_compatible_p (TREE_TYPE (oprnd1
), prod_type
))
393 if (!type_conversion_p (oprnd0
, stmt
, true, &half_type0
, &def_stmt
,
397 oprnd00
= gimple_assign_rhs1 (def_stmt
);
398 if (!type_conversion_p (oprnd0
, stmt
, true, &half_type1
, &def_stmt
,
402 oprnd01
= gimple_assign_rhs1 (def_stmt
);
403 if (!types_compatible_p (half_type0
, half_type1
))
405 if (TYPE_PRECISION (prod_type
) != TYPE_PRECISION (half_type0
) * 2)
409 half_type
= TREE_TYPE (oprnd00
);
410 *type_in
= half_type
;
413 /* Pattern detected. Create a stmt to be used to replace the pattern: */
414 var
= vect_recog_temp_ssa_var (type
, NULL
);
415 pattern_stmt
= gimple_build_assign_with_ops (DOT_PROD_EXPR
, var
,
416 oprnd00
, oprnd01
, oprnd1
);
418 if (dump_enabled_p ())
420 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
421 "vect_recog_dot_prod_pattern: detected: ");
422 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
425 /* We don't allow changing the order of the computation in the inner-loop
426 when doing outer-loop vectorization. */
427 gcc_assert (!nested_in_vect_loop_p (loop
, last_stmt
));
433 /* Handle widening operation by a constant. At the moment we support MULT_EXPR
436 For MULT_EXPR we check that CONST_OPRND fits HALF_TYPE, and for LSHIFT_EXPR
437 we check that CONST_OPRND is less or equal to the size of HALF_TYPE.
439 Otherwise, if the type of the result (TYPE) is at least 4 times bigger than
440 HALF_TYPE, and there is an intermediate type (2 times smaller than TYPE)
441 that satisfies the above restrictions, we can perform a widening opeartion
442 from the intermediate type to TYPE and replace a_T = (TYPE) a_t;
443 with a_it = (interm_type) a_t; */
446 vect_handle_widen_op_by_const (gimple stmt
, enum tree_code code
,
447 tree const_oprnd
, tree
*oprnd
,
448 vec
<gimple
> *stmts
, tree type
,
449 tree
*half_type
, gimple def_stmt
)
451 tree new_type
, new_oprnd
;
454 if (code
!= MULT_EXPR
&& code
!= LSHIFT_EXPR
)
457 if (((code
== MULT_EXPR
&& int_fits_type_p (const_oprnd
, *half_type
))
458 || (code
== LSHIFT_EXPR
459 && compare_tree_int (const_oprnd
, TYPE_PRECISION (*half_type
))
461 && TYPE_PRECISION (type
) == (TYPE_PRECISION (*half_type
) * 2))
463 /* CONST_OPRND is a constant of HALF_TYPE. */
464 *oprnd
= gimple_assign_rhs1 (def_stmt
);
468 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (*half_type
) * 4))
471 if (!vect_same_loop_or_bb_p (stmt
, def_stmt
))
474 /* TYPE is 4 times bigger than HALF_TYPE, try widening operation for
475 a type 2 times bigger than HALF_TYPE. */
476 new_type
= build_nonstandard_integer_type (TYPE_PRECISION (type
) / 2,
477 TYPE_UNSIGNED (type
));
478 if ((code
== MULT_EXPR
&& !int_fits_type_p (const_oprnd
, new_type
))
479 || (code
== LSHIFT_EXPR
480 && compare_tree_int (const_oprnd
, TYPE_PRECISION (new_type
)) == 1))
483 /* Use NEW_TYPE for widening operation. */
484 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)))
486 new_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
));
487 /* Check if the already created pattern stmt is what we need. */
488 if (!is_gimple_assign (new_stmt
)
489 || gimple_assign_rhs_code (new_stmt
) != NOP_EXPR
490 || TREE_TYPE (gimple_assign_lhs (new_stmt
)) != new_type
)
493 stmts
->safe_push (def_stmt
);
494 *oprnd
= gimple_assign_lhs (new_stmt
);
498 /* Create a_T = (NEW_TYPE) a_t; */
499 *oprnd
= gimple_assign_rhs1 (def_stmt
);
500 new_oprnd
= make_ssa_name (new_type
, NULL
);
501 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
, *oprnd
,
503 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)) = new_stmt
;
504 stmts
->safe_push (def_stmt
);
508 *half_type
= new_type
;
513 /* Function vect_recog_widen_mult_pattern
515 Try to find the following pattern:
518 TYPE a_T, b_T, prod_T;
524 S5 prod_T = a_T * b_T;
526 where type 'TYPE' is at least double the size of type 'type'.
528 Also detect unsigned cases:
530 unsigned type a_t, b_t;
531 unsigned TYPE u_prod_T;
532 TYPE a_T, b_T, prod_T;
538 S5 prod_T = a_T * b_T;
539 S6 u_prod_T = (unsigned TYPE) prod_T;
541 and multiplication by constants:
548 S5 prod_T = a_T * CONST;
550 A special case of multiplication by constants is when 'TYPE' is 4 times
551 bigger than 'type', but CONST fits an intermediate type 2 times smaller
552 than 'TYPE'. In that case we create an additional pattern stmt for S3
553 to create a variable of the intermediate type, and perform widen-mult
554 on the intermediate type as well:
558 TYPE a_T, prod_T, prod_T';
562 '--> a_it = (interm_type) a_t;
563 S5 prod_T = a_T * CONST;
564 '--> prod_T' = a_it w* CONST;
568 * STMTS: Contains a stmt from which the pattern search begins. In the
569 example, when this function is called with S5, the pattern {S3,S4,S5,(S6)}
570 is detected. In case of unsigned widen-mult, the original stmt (S5) is
571 replaced with S6 in STMTS. In case of multiplication by a constant
572 of an intermediate type (the last case above), STMTS also contains S3
573 (inserted before S5).
577 * TYPE_IN: The type of the input arguments to the pattern.
579 * TYPE_OUT: The type of the output of this pattern.
581 * Return value: A new stmt that will be used to replace the sequence of
582 stmts that constitute the pattern. In this case it will be:
583 WIDEN_MULT <a_t, b_t>
587 vect_recog_widen_mult_pattern (vec
<gimple
> *stmts
,
588 tree
*type_in
, tree
*type_out
)
590 gimple last_stmt
= stmts
->pop ();
591 gimple def_stmt0
, def_stmt1
;
593 tree type
, half_type0
, half_type1
;
595 tree vectype
, vectype_out
= NULL_TREE
;
597 enum tree_code dummy_code
;
603 if (!is_gimple_assign (last_stmt
))
606 type
= gimple_expr_type (last_stmt
);
608 /* Starting from LAST_STMT, follow the defs of its uses in search
609 of the above pattern. */
611 if (gimple_assign_rhs_code (last_stmt
) != MULT_EXPR
)
614 oprnd0
= gimple_assign_rhs1 (last_stmt
);
615 oprnd1
= gimple_assign_rhs2 (last_stmt
);
616 if (!types_compatible_p (TREE_TYPE (oprnd0
), type
)
617 || !types_compatible_p (TREE_TYPE (oprnd1
), type
))
620 /* Check argument 0. */
621 if (!type_conversion_p (oprnd0
, last_stmt
, false, &half_type0
, &def_stmt0
,
625 /* Check argument 1. */
626 op1_ok
= type_conversion_p (oprnd1
, last_stmt
, false, &half_type1
,
627 &def_stmt1
, &promotion
);
629 if (op1_ok
&& promotion
)
631 oprnd0
= gimple_assign_rhs1 (def_stmt0
);
632 oprnd1
= gimple_assign_rhs1 (def_stmt1
);
636 if (TREE_CODE (oprnd1
) == INTEGER_CST
637 && TREE_CODE (half_type0
) == INTEGER_TYPE
638 && vect_handle_widen_op_by_const (last_stmt
, MULT_EXPR
, oprnd1
,
639 &oprnd0
, stmts
, type
,
640 &half_type0
, def_stmt0
))
642 half_type1
= half_type0
;
643 oprnd1
= fold_convert (half_type1
, oprnd1
);
649 /* Handle unsigned case. Look for
650 S6 u_prod_T = (unsigned TYPE) prod_T;
651 Use unsigned TYPE as the type for WIDEN_MULT_EXPR. */
652 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (half_type0
))
658 if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (half_type1
))
661 use_stmt
= vect_single_imm_use (last_stmt
);
662 if (!use_stmt
|| !is_gimple_assign (use_stmt
)
663 || gimple_assign_rhs_code (use_stmt
) != NOP_EXPR
)
666 use_lhs
= gimple_assign_lhs (use_stmt
);
667 use_type
= TREE_TYPE (use_lhs
);
668 if (!INTEGRAL_TYPE_P (use_type
)
669 || (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (use_type
))
670 || (TYPE_PRECISION (type
) != TYPE_PRECISION (use_type
)))
674 last_stmt
= use_stmt
;
677 if (!types_compatible_p (half_type0
, half_type1
))
680 /* Pattern detected. */
681 if (dump_enabled_p ())
682 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
683 "vect_recog_widen_mult_pattern: detected: ");
685 /* Check target support */
686 vectype
= get_vectype_for_scalar_type (half_type0
);
687 vectype_out
= get_vectype_for_scalar_type (type
);
690 || !supportable_widening_operation (WIDEN_MULT_EXPR
, last_stmt
,
691 vectype_out
, vectype
,
692 &dummy_code
, &dummy_code
,
693 &dummy_int
, &dummy_vec
))
697 *type_out
= vectype_out
;
699 /* Pattern supported. Create a stmt to be used to replace the pattern: */
700 var
= vect_recog_temp_ssa_var (type
, NULL
);
701 pattern_stmt
= gimple_build_assign_with_ops (WIDEN_MULT_EXPR
, var
, oprnd0
,
704 if (dump_enabled_p ())
705 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
707 stmts
->safe_push (last_stmt
);
712 /* Function vect_recog_pow_pattern
714 Try to find the following pattern:
718 with POW being one of pow, powf, powi, powif and N being
723 * LAST_STMT: A stmt from which the pattern search begins.
727 * TYPE_IN: The type of the input arguments to the pattern.
729 * TYPE_OUT: The type of the output of this pattern.
731 * Return value: A new stmt that will be used to replace the sequence of
732 stmts that constitute the pattern. In this case it will be:
739 vect_recog_pow_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
742 gimple last_stmt
= (*stmts
)[0];
743 tree fn
, base
, exp
= NULL
;
747 if (!is_gimple_call (last_stmt
) || gimple_call_lhs (last_stmt
) == NULL
)
750 fn
= gimple_call_fndecl (last_stmt
);
751 if (fn
== NULL_TREE
|| DECL_BUILT_IN_CLASS (fn
) != BUILT_IN_NORMAL
)
754 switch (DECL_FUNCTION_CODE (fn
))
760 base
= gimple_call_arg (last_stmt
, 0);
761 exp
= gimple_call_arg (last_stmt
, 1);
762 if (TREE_CODE (exp
) != REAL_CST
763 && TREE_CODE (exp
) != INTEGER_CST
)
771 /* We now have a pow or powi builtin function call with a constant
774 *type_out
= NULL_TREE
;
776 /* Catch squaring. */
777 if ((host_integerp (exp
, 0)
778 && tree_low_cst (exp
, 0) == 2)
779 || (TREE_CODE (exp
) == REAL_CST
780 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp
), dconst2
)))
782 *type_in
= TREE_TYPE (base
);
784 var
= vect_recog_temp_ssa_var (TREE_TYPE (base
), NULL
);
785 stmt
= gimple_build_assign_with_ops (MULT_EXPR
, var
, base
, base
);
789 /* Catch square root. */
790 if (TREE_CODE (exp
) == REAL_CST
791 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp
), dconsthalf
))
793 tree newfn
= mathfn_built_in (TREE_TYPE (base
), BUILT_IN_SQRT
);
794 *type_in
= get_vectype_for_scalar_type (TREE_TYPE (base
));
797 gimple stmt
= gimple_build_call (newfn
, 1, base
);
798 if (vectorizable_function (stmt
, *type_in
, *type_in
)
801 var
= vect_recog_temp_ssa_var (TREE_TYPE (base
), stmt
);
802 gimple_call_set_lhs (stmt
, var
);
812 /* Function vect_recog_widen_sum_pattern
814 Try to find the following pattern:
817 TYPE x_T, sum = init;
819 sum_0 = phi <init, sum_1>
822 S3 sum_1 = x_T + sum_0;
824 where type 'TYPE' is at least double the size of type 'type', i.e - we're
825 summing elements of type 'type' into an accumulator of type 'TYPE'. This is
826 a special case of a reduction computation.
830 * LAST_STMT: A stmt from which the pattern search begins. In the example,
831 when this function is called with S3, the pattern {S2,S3} will be detected.
835 * TYPE_IN: The type of the input arguments to the pattern.
837 * TYPE_OUT: The type of the output of this pattern.
839 * Return value: A new stmt that will be used to replace the sequence of
840 stmts that constitute the pattern. In this case it will be:
841 WIDEN_SUM <x_t, sum_0>
843 Note: The widening-sum idiom is a widening reduction pattern that is
844 vectorized without preserving all the intermediate results. It
845 produces only N/2 (widened) results (by summing up pairs of
846 intermediate results) rather than all N results. Therefore, we
847 cannot allow this pattern when we want to get all the results and in
848 the correct order (as is the case when this computation is in an
849 inner-loop nested in an outer-loop that us being vectorized). */
852 vect_recog_widen_sum_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
855 gimple stmt
, last_stmt
= (*stmts
)[0];
857 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
858 tree type
, half_type
;
860 loop_vec_info loop_info
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
868 loop
= LOOP_VINFO_LOOP (loop_info
);
870 if (!is_gimple_assign (last_stmt
))
873 type
= gimple_expr_type (last_stmt
);
875 /* Look for the following pattern
878 In which DX is at least double the size of X, and sum_1 has been
879 recognized as a reduction variable.
882 /* Starting from LAST_STMT, follow the defs of its uses in search
883 of the above pattern. */
885 if (gimple_assign_rhs_code (last_stmt
) != PLUS_EXPR
)
888 if (STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_reduction_def
)
891 oprnd0
= gimple_assign_rhs1 (last_stmt
);
892 oprnd1
= gimple_assign_rhs2 (last_stmt
);
893 if (!types_compatible_p (TREE_TYPE (oprnd0
), type
)
894 || !types_compatible_p (TREE_TYPE (oprnd1
), type
))
897 /* So far so good. Since last_stmt was detected as a (summation) reduction,
898 we know that oprnd1 is the reduction variable (defined by a loop-header
899 phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
900 Left to check that oprnd0 is defined by a cast from type 'type' to type
903 if (!type_conversion_p (oprnd0
, last_stmt
, true, &half_type
, &stmt
,
908 oprnd0
= gimple_assign_rhs1 (stmt
);
909 *type_in
= half_type
;
912 /* Pattern detected. Create a stmt to be used to replace the pattern: */
913 var
= vect_recog_temp_ssa_var (type
, NULL
);
914 pattern_stmt
= gimple_build_assign_with_ops (WIDEN_SUM_EXPR
, var
,
917 if (dump_enabled_p ())
919 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
920 "vect_recog_widen_sum_pattern: detected: ");
921 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
924 /* We don't allow changing the order of the computation in the inner-loop
925 when doing outer-loop vectorization. */
926 gcc_assert (!nested_in_vect_loop_p (loop
, last_stmt
));
932 /* Return TRUE if the operation in STMT can be performed on a smaller type.
935 STMT - a statement to check.
936 DEF - we support operations with two operands, one of which is constant.
937 The other operand can be defined by a demotion operation, or by a
938 previous statement in a sequence of over-promoted operations. In the
939 later case DEF is used to replace that operand. (It is defined by a
940 pattern statement we created for the previous statement in the
944 NEW_TYPE - Output: a smaller type that we are trying to use. Input: if not
945 NULL, it's the type of DEF.
946 STMTS - additional pattern statements. If a pattern statement (type
947 conversion) is created in this function, its original statement is
951 OP0, OP1 - if the operation fits a smaller type, OP0 and OP1 are the new
952 operands to use in the new pattern statement for STMT (will be created
953 in vect_recog_over_widening_pattern ()).
954 NEW_DEF_STMT - in case DEF has to be promoted, we create two pattern
955 statements for STMT: the first one is a type promotion and the second
956 one is the operation itself. We return the type promotion statement
957 in NEW_DEF_STMT and further store it in STMT_VINFO_PATTERN_DEF_SEQ of
958 the second pattern statement. */
961 vect_operation_fits_smaller_type (gimple stmt
, tree def
, tree
*new_type
,
962 tree
*op0
, tree
*op1
, gimple
*new_def_stmt
,
966 tree const_oprnd
, oprnd
;
967 tree interm_type
= NULL_TREE
, half_type
, new_oprnd
, type
;
968 gimple def_stmt
, new_stmt
;
974 *new_def_stmt
= NULL
;
976 if (!is_gimple_assign (stmt
))
979 code
= gimple_assign_rhs_code (stmt
);
980 if (code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
981 && code
!= BIT_IOR_EXPR
&& code
!= BIT_XOR_EXPR
&& code
!= BIT_AND_EXPR
)
984 oprnd
= gimple_assign_rhs1 (stmt
);
985 const_oprnd
= gimple_assign_rhs2 (stmt
);
986 type
= gimple_expr_type (stmt
);
988 if (TREE_CODE (oprnd
) != SSA_NAME
989 || TREE_CODE (const_oprnd
) != INTEGER_CST
)
992 /* If oprnd has other uses besides that in stmt we cannot mark it
993 as being part of a pattern only. */
994 if (!has_single_use (oprnd
))
997 /* If we are in the middle of a sequence, we use DEF from a previous
998 statement. Otherwise, OPRND has to be a result of type promotion. */
1001 half_type
= *new_type
;
1007 if (!type_conversion_p (oprnd
, stmt
, false, &half_type
, &def_stmt
,
1010 || !vect_same_loop_or_bb_p (stmt
, def_stmt
))
1014 /* Can we perform the operation on a smaller type? */
1020 if (!int_fits_type_p (const_oprnd
, half_type
))
1022 /* HALF_TYPE is not enough. Try a bigger type if possible. */
1023 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1026 interm_type
= build_nonstandard_integer_type (
1027 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1028 if (!int_fits_type_p (const_oprnd
, interm_type
))
1035 /* Try intermediate type - HALF_TYPE is not enough for sure. */
1036 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1039 /* Check that HALF_TYPE size + shift amount <= INTERM_TYPE size.
1040 (e.g., if the original value was char, the shift amount is at most 8
1041 if we want to use short). */
1042 if (compare_tree_int (const_oprnd
, TYPE_PRECISION (half_type
)) == 1)
1045 interm_type
= build_nonstandard_integer_type (
1046 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1048 if (!vect_supportable_shift (code
, interm_type
))
1054 if (vect_supportable_shift (code
, half_type
))
1057 /* Try intermediate type - HALF_TYPE is not supported. */
1058 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1061 interm_type
= build_nonstandard_integer_type (
1062 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1064 if (!vect_supportable_shift (code
, interm_type
))
1073 /* There are four possible cases:
1074 1. OPRND is defined by a type promotion (in that case FIRST is TRUE, it's
1075 the first statement in the sequence)
1076 a. The original, HALF_TYPE, is not enough - we replace the promotion
1077 from HALF_TYPE to TYPE with a promotion to INTERM_TYPE.
1078 b. HALF_TYPE is sufficient, OPRND is set as the RHS of the original
1080 2. OPRND is defined by a pattern statement we created.
1081 a. Its type is not sufficient for the operation, we create a new stmt:
1082 a type conversion for OPRND from HALF_TYPE to INTERM_TYPE. We store
1083 this statement in NEW_DEF_STMT, and it is later put in
1084 STMT_VINFO_PATTERN_DEF_SEQ of the pattern statement for STMT.
1085 b. OPRND is good to use in the new statement. */
1090 /* Replace the original type conversion HALF_TYPE->TYPE with
1091 HALF_TYPE->INTERM_TYPE. */
1092 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)))
1094 new_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
));
1095 /* Check if the already created pattern stmt is what we need. */
1096 if (!is_gimple_assign (new_stmt
)
1097 || gimple_assign_rhs_code (new_stmt
) != NOP_EXPR
1098 || TREE_TYPE (gimple_assign_lhs (new_stmt
)) != interm_type
)
1101 stmts
->safe_push (def_stmt
);
1102 oprnd
= gimple_assign_lhs (new_stmt
);
1106 /* Create NEW_OPRND = (INTERM_TYPE) OPRND. */
1107 oprnd
= gimple_assign_rhs1 (def_stmt
);
1108 new_oprnd
= make_ssa_name (interm_type
, NULL
);
1109 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1111 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)) = new_stmt
;
1112 stmts
->safe_push (def_stmt
);
1118 /* Retrieve the operand before the type promotion. */
1119 oprnd
= gimple_assign_rhs1 (def_stmt
);
1126 /* Create a type conversion HALF_TYPE->INTERM_TYPE. */
1127 new_oprnd
= make_ssa_name (interm_type
, NULL
);
1128 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1131 *new_def_stmt
= new_stmt
;
1134 /* Otherwise, OPRND is already set. */
1138 *new_type
= interm_type
;
1140 *new_type
= half_type
;
1143 *op1
= fold_convert (*new_type
, const_oprnd
);
1149 /* Try to find a statement or a sequence of statements that can be performed
1153 TYPE x_T, res0_T, res1_T;
1156 S2 x_T = (TYPE) x_t;
1157 S3 res0_T = op (x_T, C0);
1158 S4 res1_T = op (res0_T, C1);
1159 S5 ... = () res1_T; - type demotion
1161 where type 'TYPE' is at least double the size of type 'type', C0 and C1 are
1163 Check if S3 and S4 can be done on a smaller type than 'TYPE', it can either
1164 be 'type' or some intermediate type. For now, we expect S5 to be a type
1165 demotion operation. We also check that S3 and S4 have only one use. */
1168 vect_recog_over_widening_pattern (vec
<gimple
> *stmts
,
1169 tree
*type_in
, tree
*type_out
)
1171 gimple stmt
= stmts
->pop ();
1172 gimple pattern_stmt
= NULL
, new_def_stmt
, prev_stmt
= NULL
, use_stmt
= NULL
;
1173 tree op0
, op1
, vectype
= NULL_TREE
, use_lhs
, use_type
;
1174 tree var
= NULL_TREE
, new_type
= NULL_TREE
, new_oprnd
;
1181 if (!vinfo_for_stmt (stmt
)
1182 || STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (stmt
)))
1185 new_def_stmt
= NULL
;
1186 if (!vect_operation_fits_smaller_type (stmt
, var
, &new_type
,
1187 &op0
, &op1
, &new_def_stmt
,
1196 /* STMT can be performed on a smaller type. Check its uses. */
1197 use_stmt
= vect_single_imm_use (stmt
);
1198 if (!use_stmt
|| !is_gimple_assign (use_stmt
))
1201 /* Create pattern statement for STMT. */
1202 vectype
= get_vectype_for_scalar_type (new_type
);
1206 /* We want to collect all the statements for which we create pattern
1207 statetments, except for the case when the last statement in the
1208 sequence doesn't have a corresponding pattern statement. In such
1209 case we associate the last pattern statement with the last statement
1210 in the sequence. Therefore, we only add the original statement to
1211 the list if we know that it is not the last. */
1213 stmts
->safe_push (prev_stmt
);
1215 var
= vect_recog_temp_ssa_var (new_type
, NULL
);
1217 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), var
,
1219 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
)) = pattern_stmt
;
1220 new_pattern_def_seq (vinfo_for_stmt (stmt
), new_def_stmt
);
1222 if (dump_enabled_p ())
1224 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1225 "created pattern stmt: ");
1226 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
1229 type
= gimple_expr_type (stmt
);
1236 /* We got a sequence. We expect it to end with a type demotion operation.
1237 Otherwise, we quit (for now). There are three possible cases: the
1238 conversion is to NEW_TYPE (we don't do anything), the conversion is to
1239 a type bigger than NEW_TYPE and/or the signedness of USE_TYPE and
1240 NEW_TYPE differs (we create a new conversion statement). */
1241 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt
)))
1243 use_lhs
= gimple_assign_lhs (use_stmt
);
1244 use_type
= TREE_TYPE (use_lhs
);
1245 /* Support only type demotion or signedess change. */
1246 if (!INTEGRAL_TYPE_P (use_type
)
1247 || TYPE_PRECISION (type
) <= TYPE_PRECISION (use_type
))
1250 /* Check that NEW_TYPE is not bigger than the conversion result. */
1251 if (TYPE_PRECISION (new_type
) > TYPE_PRECISION (use_type
))
1254 if (TYPE_UNSIGNED (new_type
) != TYPE_UNSIGNED (use_type
)
1255 || TYPE_PRECISION (new_type
) != TYPE_PRECISION (use_type
))
1257 /* Create NEW_TYPE->USE_TYPE conversion. */
1258 new_oprnd
= make_ssa_name (use_type
, NULL
);
1259 pattern_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1261 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use_stmt
)) = pattern_stmt
;
1263 *type_in
= get_vectype_for_scalar_type (new_type
);
1264 *type_out
= get_vectype_for_scalar_type (use_type
);
1266 /* We created a pattern statement for the last statement in the
1267 sequence, so we don't need to associate it with the pattern
1268 statement created for PREV_STMT. Therefore, we add PREV_STMT
1269 to the list in order to mark it later in vect_pattern_recog_1. */
1271 stmts
->safe_push (prev_stmt
);
1276 STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (use_stmt
))
1277 = STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (prev_stmt
));
1280 *type_out
= NULL_TREE
;
1283 stmts
->safe_push (use_stmt
);
1286 /* TODO: support general case, create a conversion to the correct type. */
1289 /* Pattern detected. */
1290 if (dump_enabled_p ())
1292 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1293 "vect_recog_over_widening_pattern: detected: ");
1294 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
1297 return pattern_stmt
;
1300 /* Detect widening shift pattern:
1306 S2 a_T = (TYPE) a_t;
1307 S3 res_T = a_T << CONST;
1309 where type 'TYPE' is at least double the size of type 'type'.
1311 Also detect cases where the shift result is immediately converted
1312 to another type 'result_type' that is no larger in size than 'TYPE'.
1313 In those cases we perform a widen-shift that directly results in
1314 'result_type', to avoid a possible over-widening situation:
1318 result_type res_result;
1321 S2 a_T = (TYPE) a_t;
1322 S3 res_T = a_T << CONST;
1323 S4 res_result = (result_type) res_T;
1324 '--> res_result' = a_t w<< CONST;
1326 And a case when 'TYPE' is 4 times bigger than 'type'. In that case we
1327 create an additional pattern stmt for S2 to create a variable of an
1328 intermediate type, and perform widen-shift on the intermediate type:
1332 TYPE a_T, res_T, res_T';
1335 S2 a_T = (TYPE) a_t;
1336 '--> a_it = (interm_type) a_t;
1337 S3 res_T = a_T << CONST;
1338 '--> res_T' = a_it <<* CONST;
1342 * STMTS: Contains a stmt from which the pattern search begins.
1343 In case of unsigned widen-shift, the original stmt (S3) is replaced with S4
1344 in STMTS. When an intermediate type is used and a pattern statement is
1345 created for S2, we also put S2 here (before S3).
1349 * TYPE_IN: The type of the input arguments to the pattern.
1351 * TYPE_OUT: The type of the output of this pattern.
1353 * Return value: A new stmt that will be used to replace the sequence of
1354 stmts that constitute the pattern. In this case it will be:
1355 WIDEN_LSHIFT_EXPR <a_t, CONST>. */
1358 vect_recog_widen_shift_pattern (vec
<gimple
> *stmts
,
1359 tree
*type_in
, tree
*type_out
)
1361 gimple last_stmt
= stmts
->pop ();
1363 tree oprnd0
, oprnd1
;
1364 tree type
, half_type0
;
1365 gimple pattern_stmt
;
1366 tree vectype
, vectype_out
= NULL_TREE
;
1368 enum tree_code dummy_code
;
1370 vec
<tree
> dummy_vec
;
1374 if (!is_gimple_assign (last_stmt
) || !vinfo_for_stmt (last_stmt
))
1377 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (last_stmt
)))
1380 if (gimple_assign_rhs_code (last_stmt
) != LSHIFT_EXPR
)
1383 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1384 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1385 if (TREE_CODE (oprnd0
) != SSA_NAME
|| TREE_CODE (oprnd1
) != INTEGER_CST
)
1388 /* Check operand 0: it has to be defined by a type promotion. */
1389 if (!type_conversion_p (oprnd0
, last_stmt
, false, &half_type0
, &def_stmt0
,
1394 /* Check operand 1: has to be positive. We check that it fits the type
1395 in vect_handle_widen_op_by_const (). */
1396 if (tree_int_cst_compare (oprnd1
, size_zero_node
) <= 0)
1399 oprnd0
= gimple_assign_rhs1 (def_stmt0
);
1400 type
= gimple_expr_type (last_stmt
);
1402 /* Check for subsequent conversion to another type. */
1403 use_stmt
= vect_single_imm_use (last_stmt
);
1404 if (use_stmt
&& is_gimple_assign (use_stmt
)
1405 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt
))
1406 && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt
)))
1408 tree use_lhs
= gimple_assign_lhs (use_stmt
);
1409 tree use_type
= TREE_TYPE (use_lhs
);
1411 if (INTEGRAL_TYPE_P (use_type
)
1412 && TYPE_PRECISION (use_type
) <= TYPE_PRECISION (type
))
1414 last_stmt
= use_stmt
;
1419 /* Check if this a widening operation. */
1420 if (!vect_handle_widen_op_by_const (last_stmt
, LSHIFT_EXPR
, oprnd1
,
1422 type
, &half_type0
, def_stmt0
))
1425 /* Pattern detected. */
1426 if (dump_enabled_p ())
1427 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1428 "vect_recog_widen_shift_pattern: detected: ");
1430 /* Check target support. */
1431 vectype
= get_vectype_for_scalar_type (half_type0
);
1432 vectype_out
= get_vectype_for_scalar_type (type
);
1436 || !supportable_widening_operation (WIDEN_LSHIFT_EXPR
, last_stmt
,
1437 vectype_out
, vectype
,
1438 &dummy_code
, &dummy_code
,
1439 &dummy_int
, &dummy_vec
))
1443 *type_out
= vectype_out
;
1445 /* Pattern supported. Create a stmt to be used to replace the pattern. */
1446 var
= vect_recog_temp_ssa_var (type
, NULL
);
1448 gimple_build_assign_with_ops (WIDEN_LSHIFT_EXPR
, var
, oprnd0
, oprnd1
);
1450 if (dump_enabled_p ())
1451 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
1453 stmts
->safe_push (last_stmt
);
1454 return pattern_stmt
;
1457 /* Detect a vector by vector shift pattern that wouldn't be otherwise
1465 S3 res_T = b_T op a_t;
1467 where type 'TYPE' is a type with different size than 'type',
1468 and op is <<, >> or rotate.
1473 TYPE b_T, c_T, res_T;
1476 S1 a_t = (type) c_T;
1478 S3 res_T = b_T op a_t;
1482 * STMTS: Contains a stmt from which the pattern search begins,
1483 i.e. the shift/rotate stmt. The original stmt (S3) is replaced
1484 with a shift/rotate which has same type on both operands, in the
1485 second case just b_T op c_T, in the first case with added cast
1486 from a_t to c_T in STMT_VINFO_PATTERN_DEF_SEQ.
1490 * TYPE_IN: The type of the input arguments to the pattern.
1492 * TYPE_OUT: The type of the output of this pattern.
1494 * Return value: A new stmt that will be used to replace the shift/rotate
1498 vect_recog_vector_vector_shift_pattern (vec
<gimple
> *stmts
,
1499 tree
*type_in
, tree
*type_out
)
1501 gimple last_stmt
= stmts
->pop ();
1502 tree oprnd0
, oprnd1
, lhs
, var
;
1503 gimple pattern_stmt
, def_stmt
;
1504 enum tree_code rhs_code
;
1505 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
1506 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
1507 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
1508 enum vect_def_type dt
;
1511 if (!is_gimple_assign (last_stmt
))
1514 rhs_code
= gimple_assign_rhs_code (last_stmt
);
1526 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
1529 lhs
= gimple_assign_lhs (last_stmt
);
1530 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1531 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1532 if (TREE_CODE (oprnd0
) != SSA_NAME
1533 || TREE_CODE (oprnd1
) != SSA_NAME
1534 || TYPE_MODE (TREE_TYPE (oprnd0
)) == TYPE_MODE (TREE_TYPE (oprnd1
))
1535 || TYPE_PRECISION (TREE_TYPE (oprnd1
))
1536 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (oprnd1
)))
1537 || TYPE_PRECISION (TREE_TYPE (lhs
))
1538 != TYPE_PRECISION (TREE_TYPE (oprnd0
)))
1541 if (!vect_is_simple_use (oprnd1
, last_stmt
, loop_vinfo
, bb_vinfo
, &def_stmt
,
1545 if (dt
!= vect_internal_def
)
1548 *type_in
= get_vectype_for_scalar_type (TREE_TYPE (oprnd0
));
1549 *type_out
= *type_in
;
1550 if (*type_in
== NULL_TREE
)
1554 if (gimple_assign_cast_p (def_stmt
))
1556 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
1557 if (TYPE_MODE (TREE_TYPE (rhs1
)) == TYPE_MODE (TREE_TYPE (oprnd0
))
1558 && TYPE_PRECISION (TREE_TYPE (rhs1
))
1559 == TYPE_PRECISION (TREE_TYPE (oprnd0
)))
1563 if (def
== NULL_TREE
)
1565 def
= vect_recog_temp_ssa_var (TREE_TYPE (oprnd0
), NULL
);
1566 def_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, def
, oprnd1
,
1568 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
1571 /* Pattern detected. */
1572 if (dump_enabled_p ())
1573 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1574 "vect_recog_vector_vector_shift_pattern: detected: ");
1576 /* Pattern supported. Create a stmt to be used to replace the pattern. */
1577 var
= vect_recog_temp_ssa_var (TREE_TYPE (oprnd0
), NULL
);
1578 pattern_stmt
= gimple_build_assign_with_ops (rhs_code
, var
, oprnd0
, def
);
1580 if (dump_enabled_p ())
1581 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
1583 stmts
->safe_push (last_stmt
);
1584 return pattern_stmt
;
1587 /* Detect a signed division by a constant that wouldn't be
1588 otherwise vectorized:
1594 where type 'type' is an integral type and N is a constant.
1596 Similarly handle modulo by a constant:
1602 * STMTS: Contains a stmt from which the pattern search begins,
1603 i.e. the division stmt. S1 is replaced by if N is a power
1604 of two constant and type is signed:
1605 S3 y_t = b_t < 0 ? N - 1 : 0;
1607 S1' a_t = x_t >> log2 (N);
1609 S4 is replaced if N is a power of two constant and
1610 type is signed by (where *_T temporaries have unsigned type):
1611 S9 y_T = b_t < 0 ? -1U : 0U;
1612 S8 z_T = y_T >> (sizeof (type_t) * CHAR_BIT - log2 (N));
1613 S7 z_t = (type) z_T;
1615 S5 x_t = w_t & (N - 1);
1616 S4' a_t = x_t - z_t;
1620 * TYPE_IN: The type of the input arguments to the pattern.
1622 * TYPE_OUT: The type of the output of this pattern.
1624 * Return value: A new stmt that will be used to replace the division
1625 S1 or modulo S4 stmt. */
1628 vect_recog_divmod_pattern (vec
<gimple
> *stmts
,
1629 tree
*type_in
, tree
*type_out
)
1631 gimple last_stmt
= stmts
->pop ();
1632 tree oprnd0
, oprnd1
, vectype
, itype
, cond
;
1633 gimple pattern_stmt
, def_stmt
;
1634 enum tree_code rhs_code
;
1635 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
1636 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
1637 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
1640 int dummy_int
, prec
;
1641 stmt_vec_info def_stmt_vinfo
;
1643 if (!is_gimple_assign (last_stmt
))
1646 rhs_code
= gimple_assign_rhs_code (last_stmt
);
1649 case TRUNC_DIV_EXPR
:
1650 case TRUNC_MOD_EXPR
:
1656 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
1659 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1660 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1661 itype
= TREE_TYPE (oprnd0
);
1662 if (TREE_CODE (oprnd0
) != SSA_NAME
1663 || TREE_CODE (oprnd1
) != INTEGER_CST
1664 || TREE_CODE (itype
) != INTEGER_TYPE
1665 || TYPE_PRECISION (itype
) != GET_MODE_PRECISION (TYPE_MODE (itype
)))
1668 vectype
= get_vectype_for_scalar_type (itype
);
1669 if (vectype
== NULL_TREE
)
1672 /* If the target can handle vectorized division or modulo natively,
1673 don't attempt to optimize this. */
1674 optab
= optab_for_tree_code (rhs_code
, vectype
, optab_default
);
1675 if (optab
!= unknown_optab
)
1677 enum machine_mode vec_mode
= TYPE_MODE (vectype
);
1678 int icode
= (int) optab_handler (optab
, vec_mode
);
1679 if (icode
!= CODE_FOR_nothing
)
1683 prec
= TYPE_PRECISION (itype
);
1684 if (integer_pow2p (oprnd1
))
1686 if (TYPE_UNSIGNED (itype
) || tree_int_cst_sgn (oprnd1
) != 1)
1689 /* Pattern detected. */
1690 if (dump_enabled_p ())
1691 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1692 "vect_recog_divmod_pattern: detected: ");
1694 cond
= build2 (LT_EXPR
, boolean_type_node
, oprnd0
,
1695 build_int_cst (itype
, 0));
1696 if (rhs_code
== TRUNC_DIV_EXPR
)
1698 tree var
= vect_recog_temp_ssa_var (itype
, NULL
);
1701 = gimple_build_assign_with_ops (COND_EXPR
, var
, cond
,
1702 fold_build2 (MINUS_EXPR
, itype
,
1704 build_int_cst (itype
,
1706 build_int_cst (itype
, 0));
1707 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
1708 var
= vect_recog_temp_ssa_var (itype
, NULL
);
1710 = gimple_build_assign_with_ops (PLUS_EXPR
, var
, oprnd0
,
1711 gimple_assign_lhs (def_stmt
));
1712 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1714 shift
= build_int_cst (itype
, tree_log2 (oprnd1
));
1716 = gimple_build_assign_with_ops (RSHIFT_EXPR
,
1717 vect_recog_temp_ssa_var (itype
,
1724 STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
) = NULL
;
1725 if (compare_tree_int (oprnd1
, 2) == 0)
1727 signmask
= vect_recog_temp_ssa_var (itype
, NULL
);
1729 = gimple_build_assign_with_ops (COND_EXPR
, signmask
, cond
,
1730 build_int_cst (itype
, 1),
1731 build_int_cst (itype
, 0));
1732 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1737 = build_nonstandard_integer_type (prec
, 1);
1738 tree vecutype
= get_vectype_for_scalar_type (utype
);
1740 = build_int_cst (utype
, GET_MODE_BITSIZE (TYPE_MODE (itype
))
1741 - tree_log2 (oprnd1
));
1742 tree var
= vect_recog_temp_ssa_var (utype
, NULL
);
1745 = gimple_build_assign_with_ops (COND_EXPR
, var
, cond
,
1746 build_int_cst (utype
, -1),
1747 build_int_cst (utype
, 0));
1749 = new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
1750 set_vinfo_for_stmt (def_stmt
, def_stmt_vinfo
);
1751 STMT_VINFO_VECTYPE (def_stmt_vinfo
) = vecutype
;
1752 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1753 var
= vect_recog_temp_ssa_var (utype
, NULL
);
1755 = gimple_build_assign_with_ops (RSHIFT_EXPR
, var
,
1756 gimple_assign_lhs (def_stmt
),
1759 = new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
1760 set_vinfo_for_stmt (def_stmt
, def_stmt_vinfo
);
1761 STMT_VINFO_VECTYPE (def_stmt_vinfo
) = vecutype
;
1762 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1763 signmask
= vect_recog_temp_ssa_var (itype
, NULL
);
1765 = gimple_build_assign_with_ops (NOP_EXPR
, signmask
, var
,
1767 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1770 = gimple_build_assign_with_ops (PLUS_EXPR
,
1771 vect_recog_temp_ssa_var (itype
,
1774 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1776 = gimple_build_assign_with_ops (BIT_AND_EXPR
,
1777 vect_recog_temp_ssa_var (itype
,
1779 gimple_assign_lhs (def_stmt
),
1780 fold_build2 (MINUS_EXPR
, itype
,
1782 build_int_cst (itype
,
1784 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1787 = gimple_build_assign_with_ops (MINUS_EXPR
,
1788 vect_recog_temp_ssa_var (itype
,
1790 gimple_assign_lhs (def_stmt
),
1794 if (dump_enabled_p ())
1795 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
,
1798 stmts
->safe_push (last_stmt
);
1801 *type_out
= vectype
;
1802 return pattern_stmt
;
1805 if (!host_integerp (oprnd1
, TYPE_UNSIGNED (itype
))
1806 || integer_zerop (oprnd1
)
1807 || prec
> HOST_BITS_PER_WIDE_INT
)
1810 if (!can_mult_highpart_p (TYPE_MODE (vectype
), TYPE_UNSIGNED (itype
)))
1813 STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
) = NULL
;
1815 if (TYPE_UNSIGNED (itype
))
1817 unsigned HOST_WIDE_INT mh
, ml
;
1818 int pre_shift
, post_shift
;
1819 unsigned HOST_WIDE_INT d
= tree_low_cst (oprnd1
, 1)
1820 & GET_MODE_MASK (TYPE_MODE (itype
));
1821 tree t1
, t2
, t3
, t4
;
1823 if (d
>= ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1)))
1824 /* FIXME: Can transform this into oprnd0 >= oprnd1 ? 1 : 0. */
1827 /* Find a suitable multiplier and right shift count
1828 instead of multiplying with D. */
1829 mh
= choose_multiplier (d
, prec
, prec
, &ml
, &post_shift
, &dummy_int
);
1831 /* If the suggested multiplier is more than SIZE bits, we can do better
1832 for even divisors, using an initial right shift. */
1833 if (mh
!= 0 && (d
& 1) == 0)
1835 pre_shift
= floor_log2 (d
& -d
);
1836 mh
= choose_multiplier (d
>> pre_shift
, prec
, prec
- pre_shift
,
1837 &ml
, &post_shift
, &dummy_int
);
1845 if (post_shift
- 1 >= prec
)
1848 /* t1 = oprnd0 h* ml;
1852 q = t4 >> (post_shift - 1); */
1853 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1855 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t1
, oprnd0
,
1856 build_int_cst (itype
, ml
));
1857 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1859 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1861 = gimple_build_assign_with_ops (MINUS_EXPR
, t2
, oprnd0
, t1
);
1862 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1864 t3
= vect_recog_temp_ssa_var (itype
, NULL
);
1866 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t3
, t2
,
1868 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1870 t4
= vect_recog_temp_ssa_var (itype
, NULL
);
1872 = gimple_build_assign_with_ops (PLUS_EXPR
, t4
, t1
, t3
);
1874 if (post_shift
!= 1)
1876 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1878 q
= vect_recog_temp_ssa_var (itype
, NULL
);
1880 = gimple_build_assign_with_ops (RSHIFT_EXPR
, q
, t4
,
1881 build_int_cst (itype
,
1888 pattern_stmt
= def_stmt
;
1893 if (pre_shift
>= prec
|| post_shift
>= prec
)
1896 /* t1 = oprnd0 >> pre_shift;
1898 q = t2 >> post_shift; */
1901 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1903 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t1
, oprnd0
,
1904 build_int_cst (NULL
,
1906 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1911 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1913 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t2
, t1
,
1914 build_int_cst (itype
, ml
));
1918 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1920 q
= vect_recog_temp_ssa_var (itype
, NULL
);
1922 = gimple_build_assign_with_ops (RSHIFT_EXPR
, q
, t2
,
1923 build_int_cst (itype
,
1929 pattern_stmt
= def_stmt
;
1934 unsigned HOST_WIDE_INT ml
;
1936 HOST_WIDE_INT d
= tree_low_cst (oprnd1
, 0);
1937 unsigned HOST_WIDE_INT abs_d
;
1939 tree t1
, t2
, t3
, t4
;
1941 /* Give up for -1. */
1945 /* Since d might be INT_MIN, we have to cast to
1946 unsigned HOST_WIDE_INT before negating to avoid
1947 undefined signed overflow. */
1949 ? (unsigned HOST_WIDE_INT
) d
1950 : - (unsigned HOST_WIDE_INT
) d
);
1952 /* n rem d = n rem -d */
1953 if (rhs_code
== TRUNC_MOD_EXPR
&& d
< 0)
1956 oprnd1
= build_int_cst (itype
, abs_d
);
1958 else if (HOST_BITS_PER_WIDE_INT
>= prec
1959 && abs_d
== (unsigned HOST_WIDE_INT
) 1 << (prec
- 1))
1960 /* This case is not handled correctly below. */
1963 choose_multiplier (abs_d
, prec
, prec
- 1, &ml
, &post_shift
, &dummy_int
);
1964 if (ml
>= (unsigned HOST_WIDE_INT
) 1 << (prec
- 1))
1967 ml
|= (~(unsigned HOST_WIDE_INT
) 0) << (prec
- 1);
1969 if (post_shift
>= prec
)
1972 /* t1 = oprnd1 h* ml; */
1973 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1975 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t1
, oprnd0
,
1976 build_int_cst (itype
, ml
));
1977 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1981 /* t2 = t1 + oprnd0; */
1982 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1984 = gimple_build_assign_with_ops (PLUS_EXPR
, t2
, t1
, oprnd0
);
1985 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1992 /* t3 = t2 >> post_shift; */
1993 t3
= vect_recog_temp_ssa_var (itype
, NULL
);
1995 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t3
, t2
,
1996 build_int_cst (itype
, post_shift
));
1997 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
2002 /* t4 = oprnd0 >> (prec - 1); */
2003 t4
= vect_recog_temp_ssa_var (itype
, NULL
);
2005 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t4
, oprnd0
,
2006 build_int_cst (itype
, prec
- 1));
2007 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
2009 /* q = t3 - t4; or q = t4 - t3; */
2010 q
= vect_recog_temp_ssa_var (itype
, NULL
);
2012 = gimple_build_assign_with_ops (MINUS_EXPR
, q
, d
< 0 ? t4
: t3
,
2016 if (rhs_code
== TRUNC_MOD_EXPR
)
2020 /* We divided. Now finish by:
2023 append_pattern_def_seq (stmt_vinfo
, pattern_stmt
);
2025 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
2027 = gimple_build_assign_with_ops (MULT_EXPR
, t1
, q
, oprnd1
);
2028 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
2030 r
= vect_recog_temp_ssa_var (itype
, NULL
);
2032 = gimple_build_assign_with_ops (MINUS_EXPR
, r
, oprnd0
, t1
);
2035 /* Pattern detected. */
2036 if (dump_enabled_p ())
2038 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2039 "vect_recog_divmod_pattern: detected: ");
2040 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2043 stmts
->safe_push (last_stmt
);
2046 *type_out
= vectype
;
2047 return pattern_stmt
;
2050 /* Function vect_recog_mixed_size_cond_pattern
2052 Try to find the following pattern:
2057 S1 a_T = x_t CMP y_t ? b_T : c_T;
2059 where type 'TYPE' is an integral type which has different size
2060 from 'type'. b_T and c_T are either constants (and if 'TYPE' is wider
2061 than 'type', the constants need to fit into an integer type
2062 with the same width as 'type') or results of conversion from 'type'.
2066 * LAST_STMT: A stmt from which the pattern search begins.
2070 * TYPE_IN: The type of the input arguments to the pattern.
2072 * TYPE_OUT: The type of the output of this pattern.
2074 * Return value: A new stmt that will be used to replace the pattern.
2075 Additionally a def_stmt is added.
2077 a_it = x_t CMP y_t ? b_it : c_it;
2078 a_T = (TYPE) a_it; */
2081 vect_recog_mixed_size_cond_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
2084 gimple last_stmt
= (*stmts
)[0];
2085 tree cond_expr
, then_clause
, else_clause
;
2086 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
), def_stmt_info
;
2087 tree type
, vectype
, comp_vectype
, itype
= NULL_TREE
, vecitype
;
2088 enum machine_mode cmpmode
;
2089 gimple pattern_stmt
, def_stmt
;
2090 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2091 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
2092 tree orig_type0
= NULL_TREE
, orig_type1
= NULL_TREE
;
2093 gimple def_stmt0
= NULL
, def_stmt1
= NULL
;
2095 tree comp_scalar_type
;
2097 if (!is_gimple_assign (last_stmt
)
2098 || gimple_assign_rhs_code (last_stmt
) != COND_EXPR
2099 || STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_internal_def
)
2102 cond_expr
= gimple_assign_rhs1 (last_stmt
);
2103 then_clause
= gimple_assign_rhs2 (last_stmt
);
2104 else_clause
= gimple_assign_rhs3 (last_stmt
);
2106 if (!COMPARISON_CLASS_P (cond_expr
))
2109 comp_scalar_type
= TREE_TYPE (TREE_OPERAND (cond_expr
, 0));
2110 comp_vectype
= get_vectype_for_scalar_type (comp_scalar_type
);
2111 if (comp_vectype
== NULL_TREE
)
2114 type
= gimple_expr_type (last_stmt
);
2115 if (types_compatible_p (type
, comp_scalar_type
)
2116 || ((TREE_CODE (then_clause
) != INTEGER_CST
2117 || TREE_CODE (else_clause
) != INTEGER_CST
)
2118 && !INTEGRAL_TYPE_P (comp_scalar_type
))
2119 || !INTEGRAL_TYPE_P (type
))
2122 if ((TREE_CODE (then_clause
) != INTEGER_CST
2123 && !type_conversion_p (then_clause
, last_stmt
, false, &orig_type0
,
2124 &def_stmt0
, &promotion
))
2125 || (TREE_CODE (else_clause
) != INTEGER_CST
2126 && !type_conversion_p (else_clause
, last_stmt
, false, &orig_type1
,
2127 &def_stmt1
, &promotion
)))
2130 if (orig_type0
&& orig_type1
2131 && !types_compatible_p (orig_type0
, orig_type1
))
2136 if (!types_compatible_p (orig_type0
, comp_scalar_type
))
2138 then_clause
= gimple_assign_rhs1 (def_stmt0
);
2144 if (!types_compatible_p (orig_type1
, comp_scalar_type
))
2146 else_clause
= gimple_assign_rhs1 (def_stmt1
);
2150 cmpmode
= GET_MODE_INNER (TYPE_MODE (comp_vectype
));
2152 if (GET_MODE_BITSIZE (TYPE_MODE (type
)) == GET_MODE_BITSIZE (cmpmode
))
2155 vectype
= get_vectype_for_scalar_type (type
);
2156 if (vectype
== NULL_TREE
)
2159 if (expand_vec_cond_expr_p (vectype
, comp_vectype
))
2162 if (itype
== NULL_TREE
)
2163 itype
= build_nonstandard_integer_type (GET_MODE_BITSIZE (cmpmode
),
2164 TYPE_UNSIGNED (type
));
2166 if (itype
== NULL_TREE
2167 || GET_MODE_BITSIZE (TYPE_MODE (itype
)) != GET_MODE_BITSIZE (cmpmode
))
2170 vecitype
= get_vectype_for_scalar_type (itype
);
2171 if (vecitype
== NULL_TREE
)
2174 if (!expand_vec_cond_expr_p (vecitype
, comp_vectype
))
2177 if (GET_MODE_BITSIZE (TYPE_MODE (type
)) > GET_MODE_BITSIZE (cmpmode
))
2179 if ((TREE_CODE (then_clause
) == INTEGER_CST
2180 && !int_fits_type_p (then_clause
, itype
))
2181 || (TREE_CODE (else_clause
) == INTEGER_CST
2182 && !int_fits_type_p (else_clause
, itype
)))
2187 = gimple_build_assign_with_ops (COND_EXPR
,
2188 vect_recog_temp_ssa_var (itype
, NULL
),
2189 unshare_expr (cond_expr
),
2190 fold_convert (itype
, then_clause
),
2191 fold_convert (itype
, else_clause
));
2193 = gimple_build_assign_with_ops (NOP_EXPR
,
2194 vect_recog_temp_ssa_var (type
, NULL
),
2195 gimple_assign_lhs (def_stmt
), NULL_TREE
);
2197 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
2198 def_stmt_info
= new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
2199 set_vinfo_for_stmt (def_stmt
, def_stmt_info
);
2200 STMT_VINFO_VECTYPE (def_stmt_info
) = vecitype
;
2201 *type_in
= vecitype
;
2202 *type_out
= vectype
;
2204 if (dump_enabled_p ())
2205 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2206 "vect_recog_mixed_size_cond_pattern: detected: ");
2208 return pattern_stmt
;
2212 /* Helper function of vect_recog_bool_pattern. Called recursively, return
2213 true if bool VAR can be optimized that way. */
2216 check_bool_pattern (tree var
, loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2219 enum vect_def_type dt
;
2221 enum tree_code rhs_code
;
2223 if (!vect_is_simple_use (var
, NULL
, loop_vinfo
, bb_vinfo
, &def_stmt
, &def
,
2227 if (dt
!= vect_internal_def
)
2230 if (!is_gimple_assign (def_stmt
))
2233 if (!has_single_use (def
))
2236 rhs1
= gimple_assign_rhs1 (def_stmt
);
2237 rhs_code
= gimple_assign_rhs_code (def_stmt
);
2241 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2244 if ((TYPE_PRECISION (TREE_TYPE (rhs1
)) != 1
2245 || !TYPE_UNSIGNED (TREE_TYPE (rhs1
)))
2246 && TREE_CODE (TREE_TYPE (rhs1
)) != BOOLEAN_TYPE
)
2248 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2251 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2256 if (!check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
))
2258 return check_bool_pattern (gimple_assign_rhs2 (def_stmt
), loop_vinfo
,
2262 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
2264 tree vecitype
, comp_vectype
;
2266 /* If the comparison can throw, then is_gimple_condexpr will be
2267 false and we can't make a COND_EXPR/VEC_COND_EXPR out of it. */
2268 if (stmt_could_throw_p (def_stmt
))
2271 comp_vectype
= get_vectype_for_scalar_type (TREE_TYPE (rhs1
));
2272 if (comp_vectype
== NULL_TREE
)
2275 if (TREE_CODE (TREE_TYPE (rhs1
)) != INTEGER_TYPE
)
2277 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (rhs1
));
2279 = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode
), 1);
2280 vecitype
= get_vectype_for_scalar_type (itype
);
2281 if (vecitype
== NULL_TREE
)
2285 vecitype
= comp_vectype
;
2286 return expand_vec_cond_expr_p (vecitype
, comp_vectype
);
2293 /* Helper function of adjust_bool_pattern. Add a cast to TYPE to a previous
2294 stmt (SSA_NAME_DEF_STMT of VAR) by moving the COND_EXPR from RELATED_STMT
2295 to PATTERN_DEF_SEQ and adding a cast as RELATED_STMT. */
2298 adjust_bool_pattern_cast (tree type
, tree var
)
2300 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (SSA_NAME_DEF_STMT (var
));
2301 gimple cast_stmt
, pattern_stmt
;
2303 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
));
2304 pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_vinfo
);
2305 new_pattern_def_seq (stmt_vinfo
, pattern_stmt
);
2307 = gimple_build_assign_with_ops (NOP_EXPR
,
2308 vect_recog_temp_ssa_var (type
, NULL
),
2309 gimple_assign_lhs (pattern_stmt
),
2311 STMT_VINFO_RELATED_STMT (stmt_vinfo
) = cast_stmt
;
2312 return gimple_assign_lhs (cast_stmt
);
2316 /* Helper function of vect_recog_bool_pattern. Do the actual transformations,
2317 recursively. VAR is an SSA_NAME that should be transformed from bool
2318 to a wider integer type, OUT_TYPE is the desired final integer type of
2319 the whole pattern, TRUEVAL should be NULL unless optimizing
2320 BIT_AND_EXPR into a COND_EXPR with one integer from one of the operands
2321 in the then_clause, STMTS is where statements with added pattern stmts
2322 should be pushed to. */
2325 adjust_bool_pattern (tree var
, tree out_type
, tree trueval
,
2328 gimple stmt
= SSA_NAME_DEF_STMT (var
);
2329 enum tree_code rhs_code
, def_rhs_code
;
2330 tree itype
, cond_expr
, rhs1
, rhs2
, irhs1
, irhs2
;
2332 gimple pattern_stmt
, def_stmt
;
2334 rhs1
= gimple_assign_rhs1 (stmt
);
2335 rhs2
= gimple_assign_rhs2 (stmt
);
2336 rhs_code
= gimple_assign_rhs_code (stmt
);
2337 loc
= gimple_location (stmt
);
2342 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2343 itype
= TREE_TYPE (irhs1
);
2345 = gimple_build_assign_with_ops (SSA_NAME
,
2346 vect_recog_temp_ssa_var (itype
, NULL
),
2351 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2352 itype
= TREE_TYPE (irhs1
);
2354 = gimple_build_assign_with_ops (BIT_XOR_EXPR
,
2355 vect_recog_temp_ssa_var (itype
, NULL
),
2356 irhs1
, build_int_cst (itype
, 1));
2360 /* Try to optimize x = y & (a < b ? 1 : 0); into
2361 x = (a < b ? y : 0);
2367 S1 a_b = x1 CMP1 y1;
2368 S2 b_b = x2 CMP2 y2;
2370 S4 d_T = (TYPE) c_b;
2372 we would normally emit:
2374 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2375 S2' b_T = x2 CMP2 y2 ? 1 : 0;
2376 S3' c_T = a_T & b_T;
2379 but we can save one stmt by using the
2380 result of one of the COND_EXPRs in the other COND_EXPR and leave
2381 BIT_AND_EXPR stmt out:
2383 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2384 S3' c_T = x2 CMP2 y2 ? a_T : 0;
2387 At least when VEC_COND_EXPR is implemented using masks
2388 cond ? 1 : 0 is as expensive as cond ? var : 0, in both cases it
2389 computes the comparison masks and ands it, in one case with
2390 all ones vector, in the other case with a vector register.
2391 Don't do this for BIT_IOR_EXPR, because cond ? 1 : var; is
2392 often more expensive. */
2393 def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2394 def_rhs_code
= gimple_assign_rhs_code (def_stmt
);
2395 if (TREE_CODE_CLASS (def_rhs_code
) == tcc_comparison
)
2397 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2398 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2399 if (TYPE_PRECISION (TREE_TYPE (irhs1
))
2400 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1
))))
2403 stmt_vec_info stmt_def_vinfo
= vinfo_for_stmt (def_stmt
);
2404 irhs2
= adjust_bool_pattern (rhs2
, out_type
, irhs1
, stmts
);
2405 tstmt
= stmts
->pop ();
2406 gcc_assert (tstmt
== def_stmt
);
2407 stmts
->quick_push (stmt
);
2408 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
))
2409 = STMT_VINFO_RELATED_STMT (stmt_def_vinfo
);
2410 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_def_vinfo
));
2411 STMT_VINFO_RELATED_STMT (stmt_def_vinfo
) = NULL
;
2415 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2418 def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2419 def_rhs_code
= gimple_assign_rhs_code (def_stmt
);
2420 if (TREE_CODE_CLASS (def_rhs_code
) == tcc_comparison
)
2422 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2423 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2424 if (TYPE_PRECISION (TREE_TYPE (irhs2
))
2425 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1
))))
2428 stmt_vec_info stmt_def_vinfo
= vinfo_for_stmt (def_stmt
);
2429 irhs1
= adjust_bool_pattern (rhs1
, out_type
, irhs2
, stmts
);
2430 tstmt
= stmts
->pop ();
2431 gcc_assert (tstmt
== def_stmt
);
2432 stmts
->quick_push (stmt
);
2433 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
))
2434 = STMT_VINFO_RELATED_STMT (stmt_def_vinfo
);
2435 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_def_vinfo
));
2436 STMT_VINFO_RELATED_STMT (stmt_def_vinfo
) = NULL
;
2440 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2446 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2447 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2449 if (TYPE_PRECISION (TREE_TYPE (irhs1
))
2450 != TYPE_PRECISION (TREE_TYPE (irhs2
)))
2452 int prec1
= TYPE_PRECISION (TREE_TYPE (irhs1
));
2453 int prec2
= TYPE_PRECISION (TREE_TYPE (irhs2
));
2454 int out_prec
= TYPE_PRECISION (out_type
);
2455 if (absu_hwi (out_prec
- prec1
) < absu_hwi (out_prec
- prec2
))
2456 irhs2
= adjust_bool_pattern_cast (TREE_TYPE (irhs1
), rhs2
);
2457 else if (absu_hwi (out_prec
- prec1
) > absu_hwi (out_prec
- prec2
))
2458 irhs1
= adjust_bool_pattern_cast (TREE_TYPE (irhs2
), rhs1
);
2461 irhs1
= adjust_bool_pattern_cast (out_type
, rhs1
);
2462 irhs2
= adjust_bool_pattern_cast (out_type
, rhs2
);
2465 itype
= TREE_TYPE (irhs1
);
2467 = gimple_build_assign_with_ops (rhs_code
,
2468 vect_recog_temp_ssa_var (itype
, NULL
),
2473 gcc_assert (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
);
2474 if (TREE_CODE (TREE_TYPE (rhs1
)) != INTEGER_TYPE
2475 || !TYPE_UNSIGNED (TREE_TYPE (rhs1
))
2476 || (TYPE_PRECISION (TREE_TYPE (rhs1
))
2477 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1
)))))
2479 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (rhs1
));
2481 = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode
), 1);
2484 itype
= TREE_TYPE (rhs1
);
2485 cond_expr
= build2_loc (loc
, rhs_code
, itype
, rhs1
, rhs2
);
2486 if (trueval
== NULL_TREE
)
2487 trueval
= build_int_cst (itype
, 1);
2489 gcc_checking_assert (useless_type_conversion_p (itype
,
2490 TREE_TYPE (trueval
)));
2492 = gimple_build_assign_with_ops (COND_EXPR
,
2493 vect_recog_temp_ssa_var (itype
, NULL
),
2495 build_int_cst (itype
, 0));
2499 stmts
->safe_push (stmt
);
2500 gimple_set_location (pattern_stmt
, loc
);
2501 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
)) = pattern_stmt
;
2502 return gimple_assign_lhs (pattern_stmt
);
2506 /* Function vect_recog_bool_pattern
2508 Try to find pattern like following:
2510 bool a_b, b_b, c_b, d_b, e_b;
2513 S1 a_b = x1 CMP1 y1;
2514 S2 b_b = x2 CMP2 y2;
2516 S4 d_b = x3 CMP3 y3;
2518 S6 f_T = (TYPE) e_b;
2520 where type 'TYPE' is an integral type.
2524 * LAST_STMT: A stmt at the end from which the pattern
2525 search begins, i.e. cast of a bool to
2530 * TYPE_IN: The type of the input arguments to the pattern.
2532 * TYPE_OUT: The type of the output of this pattern.
2534 * Return value: A new stmt that will be used to replace the pattern.
2536 Assuming size of TYPE is the same as size of all comparisons
2537 (otherwise some casts would be added where needed), the above
2538 sequence we create related pattern stmts:
2539 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2540 S3' c_T = x2 CMP2 y2 ? a_T : 0;
2541 S4' d_T = x3 CMP3 y3 ? 1 : 0;
2542 S5' e_T = c_T | d_T;
2545 Instead of the above S3' we could emit:
2546 S2' b_T = x2 CMP2 y2 ? 1 : 0;
2547 S3' c_T = a_T | b_T;
2548 but the above is more efficient. */
2551 vect_recog_bool_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
2554 gimple last_stmt
= stmts
->pop ();
2555 enum tree_code rhs_code
;
2556 tree var
, lhs
, rhs
, vectype
;
2557 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
2558 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2559 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
2560 gimple pattern_stmt
;
2562 if (!is_gimple_assign (last_stmt
))
2565 var
= gimple_assign_rhs1 (last_stmt
);
2566 lhs
= gimple_assign_lhs (last_stmt
);
2568 if ((TYPE_PRECISION (TREE_TYPE (var
)) != 1
2569 || !TYPE_UNSIGNED (TREE_TYPE (var
)))
2570 && TREE_CODE (TREE_TYPE (var
)) != BOOLEAN_TYPE
)
2573 rhs_code
= gimple_assign_rhs_code (last_stmt
);
2574 if (CONVERT_EXPR_CODE_P (rhs_code
))
2576 if (TREE_CODE (TREE_TYPE (lhs
)) != INTEGER_TYPE
2577 || TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
2579 vectype
= get_vectype_for_scalar_type (TREE_TYPE (lhs
));
2580 if (vectype
== NULL_TREE
)
2583 if (!check_bool_pattern (var
, loop_vinfo
, bb_vinfo
))
2586 rhs
= adjust_bool_pattern (var
, TREE_TYPE (lhs
), NULL_TREE
, stmts
);
2587 lhs
= vect_recog_temp_ssa_var (TREE_TYPE (lhs
), NULL
);
2588 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2590 = gimple_build_assign_with_ops (SSA_NAME
, lhs
, rhs
, NULL_TREE
);
2593 = gimple_build_assign_with_ops (NOP_EXPR
, lhs
, rhs
, NULL_TREE
);
2594 *type_out
= vectype
;
2596 stmts
->safe_push (last_stmt
);
2597 if (dump_enabled_p ())
2598 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2599 "vect_recog_bool_pattern: detected: ");
2601 return pattern_stmt
;
2603 else if (rhs_code
== SSA_NAME
2604 && STMT_VINFO_DATA_REF (stmt_vinfo
))
2606 stmt_vec_info pattern_stmt_info
;
2607 vectype
= STMT_VINFO_VECTYPE (stmt_vinfo
);
2608 gcc_assert (vectype
!= NULL_TREE
);
2609 if (!VECTOR_MODE_P (TYPE_MODE (vectype
)))
2611 if (!check_bool_pattern (var
, loop_vinfo
, bb_vinfo
))
2614 rhs
= adjust_bool_pattern (var
, TREE_TYPE (vectype
), NULL_TREE
, stmts
);
2615 lhs
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (vectype
), lhs
);
2616 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2618 tree rhs2
= vect_recog_temp_ssa_var (TREE_TYPE (lhs
), NULL
);
2620 = gimple_build_assign_with_ops (NOP_EXPR
, rhs2
, rhs
, NULL_TREE
);
2621 new_pattern_def_seq (stmt_vinfo
, cast_stmt
);
2625 = gimple_build_assign_with_ops (SSA_NAME
, lhs
, rhs
, NULL_TREE
);
2626 pattern_stmt_info
= new_stmt_vec_info (pattern_stmt
, loop_vinfo
,
2628 set_vinfo_for_stmt (pattern_stmt
, pattern_stmt_info
);
2629 STMT_VINFO_DATA_REF (pattern_stmt_info
)
2630 = STMT_VINFO_DATA_REF (stmt_vinfo
);
2631 STMT_VINFO_DR_BASE_ADDRESS (pattern_stmt_info
)
2632 = STMT_VINFO_DR_BASE_ADDRESS (stmt_vinfo
);
2633 STMT_VINFO_DR_INIT (pattern_stmt_info
) = STMT_VINFO_DR_INIT (stmt_vinfo
);
2634 STMT_VINFO_DR_OFFSET (pattern_stmt_info
)
2635 = STMT_VINFO_DR_OFFSET (stmt_vinfo
);
2636 STMT_VINFO_DR_STEP (pattern_stmt_info
) = STMT_VINFO_DR_STEP (stmt_vinfo
);
2637 STMT_VINFO_DR_ALIGNED_TO (pattern_stmt_info
)
2638 = STMT_VINFO_DR_ALIGNED_TO (stmt_vinfo
);
2639 DR_STMT (STMT_VINFO_DATA_REF (stmt_vinfo
)) = pattern_stmt
;
2640 *type_out
= vectype
;
2642 stmts
->safe_push (last_stmt
);
2643 if (dump_enabled_p ())
2644 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2645 "vect_recog_bool_pattern: detected: ");
2646 return pattern_stmt
;
2653 /* Mark statements that are involved in a pattern. */
2656 vect_mark_pattern_stmts (gimple orig_stmt
, gimple pattern_stmt
,
2657 tree pattern_vectype
)
2659 stmt_vec_info pattern_stmt_info
, def_stmt_info
;
2660 stmt_vec_info orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
2661 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (orig_stmt_info
);
2662 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (orig_stmt_info
);
2665 pattern_stmt_info
= vinfo_for_stmt (pattern_stmt
);
2666 if (pattern_stmt_info
== NULL
)
2668 pattern_stmt_info
= new_stmt_vec_info (pattern_stmt
, loop_vinfo
,
2670 set_vinfo_for_stmt (pattern_stmt
, pattern_stmt_info
);
2672 gimple_set_bb (pattern_stmt
, gimple_bb (orig_stmt
));
2674 STMT_VINFO_RELATED_STMT (pattern_stmt_info
) = orig_stmt
;
2675 STMT_VINFO_DEF_TYPE (pattern_stmt_info
)
2676 = STMT_VINFO_DEF_TYPE (orig_stmt_info
);
2677 STMT_VINFO_VECTYPE (pattern_stmt_info
) = pattern_vectype
;
2678 STMT_VINFO_IN_PATTERN_P (orig_stmt_info
) = true;
2679 STMT_VINFO_RELATED_STMT (orig_stmt_info
) = pattern_stmt
;
2680 STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
)
2681 = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info
);
2682 if (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
))
2684 gimple_stmt_iterator si
;
2685 for (si
= gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
));
2686 !gsi_end_p (si
); gsi_next (&si
))
2688 def_stmt
= gsi_stmt (si
);
2689 def_stmt_info
= vinfo_for_stmt (def_stmt
);
2690 if (def_stmt_info
== NULL
)
2692 def_stmt_info
= new_stmt_vec_info (def_stmt
, loop_vinfo
,
2694 set_vinfo_for_stmt (def_stmt
, def_stmt_info
);
2696 gimple_set_bb (def_stmt
, gimple_bb (orig_stmt
));
2697 STMT_VINFO_RELATED_STMT (def_stmt_info
) = orig_stmt
;
2698 STMT_VINFO_DEF_TYPE (def_stmt_info
)
2699 = STMT_VINFO_DEF_TYPE (orig_stmt_info
);
2700 if (STMT_VINFO_VECTYPE (def_stmt_info
) == NULL_TREE
)
2701 STMT_VINFO_VECTYPE (def_stmt_info
) = pattern_vectype
;
2706 /* Function vect_pattern_recog_1
2709 PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
2710 computation pattern.
2711 STMT: A stmt from which the pattern search should start.
2713 If PATTERN_RECOG_FUNC successfully detected the pattern, it creates an
2714 expression that computes the same functionality and can be used to
2715 replace the sequence of stmts that are involved in the pattern.
2718 This function checks if the expression returned by PATTERN_RECOG_FUNC is
2719 supported in vector form by the target. We use 'TYPE_IN' to obtain the
2720 relevant vector type. If 'TYPE_IN' is already a vector type, then this
2721 indicates that target support had already been checked by PATTERN_RECOG_FUNC.
2722 If 'TYPE_OUT' is also returned by PATTERN_RECOG_FUNC, we check that it fits
2723 to the available target pattern.
2725 This function also does some bookkeeping, as explained in the documentation
2726 for vect_recog_pattern. */
2729 vect_pattern_recog_1 (vect_recog_func_ptr vect_recog_func
,
2730 gimple_stmt_iterator si
,
2731 vec
<gimple
> *stmts_to_replace
)
2733 gimple stmt
= gsi_stmt (si
), pattern_stmt
;
2734 stmt_vec_info stmt_info
;
2735 loop_vec_info loop_vinfo
;
2736 tree pattern_vectype
;
2737 tree type_in
, type_out
;
2738 enum tree_code code
;
2742 stmts_to_replace
->truncate (0);
2743 stmts_to_replace
->quick_push (stmt
);
2744 pattern_stmt
= (* vect_recog_func
) (stmts_to_replace
, &type_in
, &type_out
);
2748 stmt
= stmts_to_replace
->last ();
2749 stmt_info
= vinfo_for_stmt (stmt
);
2750 loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2752 if (VECTOR_MODE_P (TYPE_MODE (type_in
)))
2754 /* No need to check target support (already checked by the pattern
2755 recognition function). */
2756 pattern_vectype
= type_out
? type_out
: type_in
;
2760 enum machine_mode vec_mode
;
2761 enum insn_code icode
;
2764 /* Check target support */
2765 type_in
= get_vectype_for_scalar_type (type_in
);
2769 type_out
= get_vectype_for_scalar_type (type_out
);
2774 pattern_vectype
= type_out
;
2776 if (is_gimple_assign (pattern_stmt
))
2777 code
= gimple_assign_rhs_code (pattern_stmt
);
2780 gcc_assert (is_gimple_call (pattern_stmt
));
2784 optab
= optab_for_tree_code (code
, type_in
, optab_default
);
2785 vec_mode
= TYPE_MODE (type_in
);
2787 || (icode
= optab_handler (optab
, vec_mode
)) == CODE_FOR_nothing
2788 || (insn_data
[icode
].operand
[0].mode
!= TYPE_MODE (type_out
)))
2792 /* Found a vectorizable pattern. */
2793 if (dump_enabled_p ())
2795 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2796 "pattern recognized: ");
2797 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2800 /* Mark the stmts that are involved in the pattern. */
2801 vect_mark_pattern_stmts (stmt
, pattern_stmt
, pattern_vectype
);
2803 /* Patterns cannot be vectorized using SLP, because they change the order of
2806 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTIONS (loop_vinfo
), i
, next
)
2808 LOOP_VINFO_REDUCTIONS (loop_vinfo
).ordered_remove (i
);
2810 /* It is possible that additional pattern stmts are created and inserted in
2811 STMTS_TO_REPLACE. We create a stmt_info for each of them, and mark the
2812 relevant statements. */
2813 for (i
= 0; stmts_to_replace
->iterate (i
, &stmt
)
2814 && (unsigned) i
< (stmts_to_replace
->length () - 1);
2817 stmt_info
= vinfo_for_stmt (stmt
);
2818 pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
2819 if (dump_enabled_p ())
2821 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2822 "additional pattern stmt: ");
2823 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2826 vect_mark_pattern_stmts (stmt
, pattern_stmt
, NULL_TREE
);
2831 /* Function vect_pattern_recog
2834 LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
2837 Output - for each computation idiom that is detected we create a new stmt
2838 that provides the same functionality and that can be vectorized. We
2839 also record some information in the struct_stmt_info of the relevant
2840 stmts, as explained below:
2842 At the entry to this function we have the following stmts, with the
2843 following initial value in the STMT_VINFO fields:
2845 stmt in_pattern_p related_stmt vec_stmt
2846 S1: a_i = .... - - -
2847 S2: a_2 = ..use(a_i).. - - -
2848 S3: a_1 = ..use(a_2).. - - -
2849 S4: a_0 = ..use(a_1).. - - -
2850 S5: ... = ..use(a_0).. - - -
2852 Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
2853 represented by a single stmt. We then:
2854 - create a new stmt S6 equivalent to the pattern (the stmt is not
2855 inserted into the code)
2856 - fill in the STMT_VINFO fields as follows:
2858 in_pattern_p related_stmt vec_stmt
2859 S1: a_i = .... - - -
2860 S2: a_2 = ..use(a_i).. - - -
2861 S3: a_1 = ..use(a_2).. - - -
2862 S4: a_0 = ..use(a_1).. true S6 -
2863 '---> S6: a_new = .... - S4 -
2864 S5: ... = ..use(a_0).. - - -
2866 (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
2867 to each other through the RELATED_STMT field).
2869 S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
2870 of S4 because it will replace all its uses. Stmts {S1,S2,S3} will
2871 remain irrelevant unless used by stmts other than S4.
2873 If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
2874 (because they are marked as irrelevant). It will vectorize S6, and record
2875 a pointer to the new vector stmt VS6 from S6 (as usual).
2876 S4 will be skipped, and S5 will be vectorized as usual:
2878 in_pattern_p related_stmt vec_stmt
2879 S1: a_i = .... - - -
2880 S2: a_2 = ..use(a_i).. - - -
2881 S3: a_1 = ..use(a_2).. - - -
2882 > VS6: va_new = .... - - -
2883 S4: a_0 = ..use(a_1).. true S6 VS6
2884 '---> S6: a_new = .... - S4 VS6
2885 > VS5: ... = ..vuse(va_new).. - - -
2886 S5: ... = ..use(a_0).. - - -
2888 DCE could then get rid of {S1,S2,S3,S4,S5} (if their defs are not used
2889 elsewhere), and we'll end up with:
2892 VS5: ... = ..vuse(va_new)..
2894 In case of more than one pattern statements, e.g., widen-mult with
2898 S2 a_T = (TYPE) a_t;
2899 '--> S3: a_it = (interm_type) a_t;
2900 S4 prod_T = a_T * CONST;
2901 '--> S5: prod_T' = a_it w* CONST;
2903 there may be other users of a_T outside the pattern. In that case S2 will
2904 be marked as relevant (as well as S3), and both S2 and S3 will be analyzed
2905 and vectorized. The vector stmt VS2 will be recorded in S2, and VS3 will
2906 be recorded in S3. */
2909 vect_pattern_recog (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2914 gimple_stmt_iterator si
;
2916 vect_recog_func_ptr vect_recog_func
;
2917 vec
<gimple
> stmts_to_replace
;
2918 stmts_to_replace
.create (1);
2921 if (dump_enabled_p ())
2922 dump_printf_loc (MSG_NOTE
, vect_location
,
2923 "=== vect_pattern_recog ===");
2927 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2928 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
2929 nbbs
= loop
->num_nodes
;
2933 bbs
= &BB_VINFO_BB (bb_vinfo
);
2937 /* Scan through the loop stmts, applying the pattern recognition
2938 functions starting at each stmt visited: */
2939 for (i
= 0; i
< nbbs
; i
++)
2941 basic_block bb
= bbs
[i
];
2942 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2944 if (bb_vinfo
&& (stmt
= gsi_stmt (si
))
2945 && vinfo_for_stmt (stmt
)
2946 && !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt
)))
2949 /* Scan over all generic vect_recog_xxx_pattern functions. */
2950 for (j
= 0; j
< NUM_PATTERNS
; j
++)
2952 vect_recog_func
= vect_vect_recog_func_ptrs
[j
];
2953 vect_pattern_recog_1 (vect_recog_func
, si
,
2959 stmts_to_replace
.release ();