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
))
641 half_type1
= half_type0
;
646 /* Handle unsigned case. Look for
647 S6 u_prod_T = (unsigned TYPE) prod_T;
648 Use unsigned TYPE as the type for WIDEN_MULT_EXPR. */
649 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (half_type0
))
655 if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (half_type1
))
658 use_stmt
= vect_single_imm_use (last_stmt
);
659 if (!use_stmt
|| !is_gimple_assign (use_stmt
)
660 || gimple_assign_rhs_code (use_stmt
) != NOP_EXPR
)
663 use_lhs
= gimple_assign_lhs (use_stmt
);
664 use_type
= TREE_TYPE (use_lhs
);
665 if (!INTEGRAL_TYPE_P (use_type
)
666 || (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (use_type
))
667 || (TYPE_PRECISION (type
) != TYPE_PRECISION (use_type
)))
671 last_stmt
= use_stmt
;
674 if (!types_compatible_p (half_type0
, half_type1
))
677 /* Pattern detected. */
678 if (dump_enabled_p ())
679 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
680 "vect_recog_widen_mult_pattern: detected: ");
682 /* Check target support */
683 vectype
= get_vectype_for_scalar_type (half_type0
);
684 vectype_out
= get_vectype_for_scalar_type (type
);
687 || !supportable_widening_operation (WIDEN_MULT_EXPR
, last_stmt
,
688 vectype_out
, vectype
,
689 &dummy_code
, &dummy_code
,
690 &dummy_int
, &dummy_vec
))
694 *type_out
= vectype_out
;
696 /* Pattern supported. Create a stmt to be used to replace the pattern: */
697 var
= vect_recog_temp_ssa_var (type
, NULL
);
698 pattern_stmt
= gimple_build_assign_with_ops (WIDEN_MULT_EXPR
, var
, oprnd0
,
701 if (dump_enabled_p ())
702 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
704 stmts
->safe_push (last_stmt
);
709 /* Function vect_recog_pow_pattern
711 Try to find the following pattern:
715 with POW being one of pow, powf, powi, powif and N being
720 * LAST_STMT: A stmt from which the pattern search begins.
724 * TYPE_IN: The type of the input arguments to the pattern.
726 * TYPE_OUT: The type of the output of this pattern.
728 * Return value: A new stmt that will be used to replace the sequence of
729 stmts that constitute the pattern. In this case it will be:
736 vect_recog_pow_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
739 gimple last_stmt
= (*stmts
)[0];
740 tree fn
, base
, exp
= NULL
;
744 if (!is_gimple_call (last_stmt
) || gimple_call_lhs (last_stmt
) == NULL
)
747 fn
= gimple_call_fndecl (last_stmt
);
748 if (fn
== NULL_TREE
|| DECL_BUILT_IN_CLASS (fn
) != BUILT_IN_NORMAL
)
751 switch (DECL_FUNCTION_CODE (fn
))
757 base
= gimple_call_arg (last_stmt
, 0);
758 exp
= gimple_call_arg (last_stmt
, 1);
759 if (TREE_CODE (exp
) != REAL_CST
760 && TREE_CODE (exp
) != INTEGER_CST
)
768 /* We now have a pow or powi builtin function call with a constant
771 *type_out
= NULL_TREE
;
773 /* Catch squaring. */
774 if ((host_integerp (exp
, 0)
775 && tree_low_cst (exp
, 0) == 2)
776 || (TREE_CODE (exp
) == REAL_CST
777 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp
), dconst2
)))
779 *type_in
= TREE_TYPE (base
);
781 var
= vect_recog_temp_ssa_var (TREE_TYPE (base
), NULL
);
782 stmt
= gimple_build_assign_with_ops (MULT_EXPR
, var
, base
, base
);
786 /* Catch square root. */
787 if (TREE_CODE (exp
) == REAL_CST
788 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp
), dconsthalf
))
790 tree newfn
= mathfn_built_in (TREE_TYPE (base
), BUILT_IN_SQRT
);
791 *type_in
= get_vectype_for_scalar_type (TREE_TYPE (base
));
794 gimple stmt
= gimple_build_call (newfn
, 1, base
);
795 if (vectorizable_function (stmt
, *type_in
, *type_in
)
798 var
= vect_recog_temp_ssa_var (TREE_TYPE (base
), stmt
);
799 gimple_call_set_lhs (stmt
, var
);
809 /* Function vect_recog_widen_sum_pattern
811 Try to find the following pattern:
814 TYPE x_T, sum = init;
816 sum_0 = phi <init, sum_1>
819 S3 sum_1 = x_T + sum_0;
821 where type 'TYPE' is at least double the size of type 'type', i.e - we're
822 summing elements of type 'type' into an accumulator of type 'TYPE'. This is
823 a special case of a reduction computation.
827 * LAST_STMT: A stmt from which the pattern search begins. In the example,
828 when this function is called with S3, the pattern {S2,S3} will be detected.
832 * TYPE_IN: The type of the input arguments to the pattern.
834 * TYPE_OUT: The type of the output of this pattern.
836 * Return value: A new stmt that will be used to replace the sequence of
837 stmts that constitute the pattern. In this case it will be:
838 WIDEN_SUM <x_t, sum_0>
840 Note: The widening-sum idiom is a widening reduction pattern that is
841 vectorized without preserving all the intermediate results. It
842 produces only N/2 (widened) results (by summing up pairs of
843 intermediate results) rather than all N results. Therefore, we
844 cannot allow this pattern when we want to get all the results and in
845 the correct order (as is the case when this computation is in an
846 inner-loop nested in an outer-loop that us being vectorized). */
849 vect_recog_widen_sum_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
852 gimple stmt
, last_stmt
= (*stmts
)[0];
854 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
855 tree type
, half_type
;
857 loop_vec_info loop_info
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
865 loop
= LOOP_VINFO_LOOP (loop_info
);
867 if (!is_gimple_assign (last_stmt
))
870 type
= gimple_expr_type (last_stmt
);
872 /* Look for the following pattern
875 In which DX is at least double the size of X, and sum_1 has been
876 recognized as a reduction variable.
879 /* Starting from LAST_STMT, follow the defs of its uses in search
880 of the above pattern. */
882 if (gimple_assign_rhs_code (last_stmt
) != PLUS_EXPR
)
885 if (STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_reduction_def
)
888 oprnd0
= gimple_assign_rhs1 (last_stmt
);
889 oprnd1
= gimple_assign_rhs2 (last_stmt
);
890 if (!types_compatible_p (TREE_TYPE (oprnd0
), type
)
891 || !types_compatible_p (TREE_TYPE (oprnd1
), type
))
894 /* So far so good. Since last_stmt was detected as a (summation) reduction,
895 we know that oprnd1 is the reduction variable (defined by a loop-header
896 phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
897 Left to check that oprnd0 is defined by a cast from type 'type' to type
900 if (!type_conversion_p (oprnd0
, last_stmt
, true, &half_type
, &stmt
,
905 oprnd0
= gimple_assign_rhs1 (stmt
);
906 *type_in
= half_type
;
909 /* Pattern detected. Create a stmt to be used to replace the pattern: */
910 var
= vect_recog_temp_ssa_var (type
, NULL
);
911 pattern_stmt
= gimple_build_assign_with_ops (WIDEN_SUM_EXPR
, var
,
914 if (dump_enabled_p ())
916 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
917 "vect_recog_widen_sum_pattern: detected: ");
918 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
921 /* We don't allow changing the order of the computation in the inner-loop
922 when doing outer-loop vectorization. */
923 gcc_assert (!nested_in_vect_loop_p (loop
, last_stmt
));
929 /* Return TRUE if the operation in STMT can be performed on a smaller type.
932 STMT - a statement to check.
933 DEF - we support operations with two operands, one of which is constant.
934 The other operand can be defined by a demotion operation, or by a
935 previous statement in a sequence of over-promoted operations. In the
936 later case DEF is used to replace that operand. (It is defined by a
937 pattern statement we created for the previous statement in the
941 NEW_TYPE - Output: a smaller type that we are trying to use. Input: if not
942 NULL, it's the type of DEF.
943 STMTS - additional pattern statements. If a pattern statement (type
944 conversion) is created in this function, its original statement is
948 OP0, OP1 - if the operation fits a smaller type, OP0 and OP1 are the new
949 operands to use in the new pattern statement for STMT (will be created
950 in vect_recog_over_widening_pattern ()).
951 NEW_DEF_STMT - in case DEF has to be promoted, we create two pattern
952 statements for STMT: the first one is a type promotion and the second
953 one is the operation itself. We return the type promotion statement
954 in NEW_DEF_STMT and further store it in STMT_VINFO_PATTERN_DEF_SEQ of
955 the second pattern statement. */
958 vect_operation_fits_smaller_type (gimple stmt
, tree def
, tree
*new_type
,
959 tree
*op0
, tree
*op1
, gimple
*new_def_stmt
,
963 tree const_oprnd
, oprnd
;
964 tree interm_type
= NULL_TREE
, half_type
, new_oprnd
, type
;
965 gimple def_stmt
, new_stmt
;
971 *new_def_stmt
= NULL
;
973 if (!is_gimple_assign (stmt
))
976 code
= gimple_assign_rhs_code (stmt
);
977 if (code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
978 && code
!= BIT_IOR_EXPR
&& code
!= BIT_XOR_EXPR
&& code
!= BIT_AND_EXPR
)
981 oprnd
= gimple_assign_rhs1 (stmt
);
982 const_oprnd
= gimple_assign_rhs2 (stmt
);
983 type
= gimple_expr_type (stmt
);
985 if (TREE_CODE (oprnd
) != SSA_NAME
986 || TREE_CODE (const_oprnd
) != INTEGER_CST
)
989 /* If oprnd has other uses besides that in stmt we cannot mark it
990 as being part of a pattern only. */
991 if (!has_single_use (oprnd
))
994 /* If we are in the middle of a sequence, we use DEF from a previous
995 statement. Otherwise, OPRND has to be a result of type promotion. */
998 half_type
= *new_type
;
1004 if (!type_conversion_p (oprnd
, stmt
, false, &half_type
, &def_stmt
,
1007 || !vect_same_loop_or_bb_p (stmt
, def_stmt
))
1011 /* Can we perform the operation on a smaller type? */
1017 if (!int_fits_type_p (const_oprnd
, half_type
))
1019 /* HALF_TYPE is not enough. Try a bigger type if possible. */
1020 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1023 interm_type
= build_nonstandard_integer_type (
1024 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1025 if (!int_fits_type_p (const_oprnd
, interm_type
))
1032 /* Try intermediate type - HALF_TYPE is not enough for sure. */
1033 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1036 /* Check that HALF_TYPE size + shift amount <= INTERM_TYPE size.
1037 (e.g., if the original value was char, the shift amount is at most 8
1038 if we want to use short). */
1039 if (compare_tree_int (const_oprnd
, TYPE_PRECISION (half_type
)) == 1)
1042 interm_type
= build_nonstandard_integer_type (
1043 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1045 if (!vect_supportable_shift (code
, interm_type
))
1051 if (vect_supportable_shift (code
, half_type
))
1054 /* Try intermediate type - HALF_TYPE is not supported. */
1055 if (TYPE_PRECISION (type
) < (TYPE_PRECISION (half_type
) * 4))
1058 interm_type
= build_nonstandard_integer_type (
1059 TYPE_PRECISION (half_type
) * 2, TYPE_UNSIGNED (type
));
1061 if (!vect_supportable_shift (code
, interm_type
))
1070 /* There are four possible cases:
1071 1. OPRND is defined by a type promotion (in that case FIRST is TRUE, it's
1072 the first statement in the sequence)
1073 a. The original, HALF_TYPE, is not enough - we replace the promotion
1074 from HALF_TYPE to TYPE with a promotion to INTERM_TYPE.
1075 b. HALF_TYPE is sufficient, OPRND is set as the RHS of the original
1077 2. OPRND is defined by a pattern statement we created.
1078 a. Its type is not sufficient for the operation, we create a new stmt:
1079 a type conversion for OPRND from HALF_TYPE to INTERM_TYPE. We store
1080 this statement in NEW_DEF_STMT, and it is later put in
1081 STMT_VINFO_PATTERN_DEF_SEQ of the pattern statement for STMT.
1082 b. OPRND is good to use in the new statement. */
1087 /* Replace the original type conversion HALF_TYPE->TYPE with
1088 HALF_TYPE->INTERM_TYPE. */
1089 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)))
1091 new_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
));
1092 /* Check if the already created pattern stmt is what we need. */
1093 if (!is_gimple_assign (new_stmt
)
1094 || gimple_assign_rhs_code (new_stmt
) != NOP_EXPR
1095 || TREE_TYPE (gimple_assign_lhs (new_stmt
)) != interm_type
)
1098 stmts
->safe_push (def_stmt
);
1099 oprnd
= gimple_assign_lhs (new_stmt
);
1103 /* Create NEW_OPRND = (INTERM_TYPE) OPRND. */
1104 oprnd
= gimple_assign_rhs1 (def_stmt
);
1105 new_oprnd
= make_ssa_name (interm_type
, NULL
);
1106 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1108 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt
)) = new_stmt
;
1109 stmts
->safe_push (def_stmt
);
1115 /* Retrieve the operand before the type promotion. */
1116 oprnd
= gimple_assign_rhs1 (def_stmt
);
1123 /* Create a type conversion HALF_TYPE->INTERM_TYPE. */
1124 new_oprnd
= make_ssa_name (interm_type
, NULL
);
1125 new_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1128 *new_def_stmt
= new_stmt
;
1131 /* Otherwise, OPRND is already set. */
1135 *new_type
= interm_type
;
1137 *new_type
= half_type
;
1140 *op1
= fold_convert (*new_type
, const_oprnd
);
1146 /* Try to find a statement or a sequence of statements that can be performed
1150 TYPE x_T, res0_T, res1_T;
1153 S2 x_T = (TYPE) x_t;
1154 S3 res0_T = op (x_T, C0);
1155 S4 res1_T = op (res0_T, C1);
1156 S5 ... = () res1_T; - type demotion
1158 where type 'TYPE' is at least double the size of type 'type', C0 and C1 are
1160 Check if S3 and S4 can be done on a smaller type than 'TYPE', it can either
1161 be 'type' or some intermediate type. For now, we expect S5 to be a type
1162 demotion operation. We also check that S3 and S4 have only one use. */
1165 vect_recog_over_widening_pattern (vec
<gimple
> *stmts
,
1166 tree
*type_in
, tree
*type_out
)
1168 gimple stmt
= stmts
->pop ();
1169 gimple pattern_stmt
= NULL
, new_def_stmt
, prev_stmt
= NULL
, use_stmt
= NULL
;
1170 tree op0
, op1
, vectype
= NULL_TREE
, use_lhs
, use_type
;
1171 tree var
= NULL_TREE
, new_type
= NULL_TREE
, new_oprnd
;
1178 if (!vinfo_for_stmt (stmt
)
1179 || STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (stmt
)))
1182 new_def_stmt
= NULL
;
1183 if (!vect_operation_fits_smaller_type (stmt
, var
, &new_type
,
1184 &op0
, &op1
, &new_def_stmt
,
1193 /* STMT can be performed on a smaller type. Check its uses. */
1194 use_stmt
= vect_single_imm_use (stmt
);
1195 if (!use_stmt
|| !is_gimple_assign (use_stmt
))
1198 /* Create pattern statement for STMT. */
1199 vectype
= get_vectype_for_scalar_type (new_type
);
1203 /* We want to collect all the statements for which we create pattern
1204 statetments, except for the case when the last statement in the
1205 sequence doesn't have a corresponding pattern statement. In such
1206 case we associate the last pattern statement with the last statement
1207 in the sequence. Therefore, we only add the original statement to
1208 the list if we know that it is not the last. */
1210 stmts
->safe_push (prev_stmt
);
1212 var
= vect_recog_temp_ssa_var (new_type
, NULL
);
1214 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt
), var
,
1216 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
)) = pattern_stmt
;
1217 new_pattern_def_seq (vinfo_for_stmt (stmt
), new_def_stmt
);
1219 if (dump_enabled_p ())
1221 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1222 "created pattern stmt: ");
1223 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
1226 type
= gimple_expr_type (stmt
);
1233 /* We got a sequence. We expect it to end with a type demotion operation.
1234 Otherwise, we quit (for now). There are three possible cases: the
1235 conversion is to NEW_TYPE (we don't do anything), the conversion is to
1236 a type bigger than NEW_TYPE and/or the signedness of USE_TYPE and
1237 NEW_TYPE differs (we create a new conversion statement). */
1238 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt
)))
1240 use_lhs
= gimple_assign_lhs (use_stmt
);
1241 use_type
= TREE_TYPE (use_lhs
);
1242 /* Support only type demotion or signedess change. */
1243 if (!INTEGRAL_TYPE_P (use_type
)
1244 || TYPE_PRECISION (type
) <= TYPE_PRECISION (use_type
))
1247 /* Check that NEW_TYPE is not bigger than the conversion result. */
1248 if (TYPE_PRECISION (new_type
) > TYPE_PRECISION (use_type
))
1251 if (TYPE_UNSIGNED (new_type
) != TYPE_UNSIGNED (use_type
)
1252 || TYPE_PRECISION (new_type
) != TYPE_PRECISION (use_type
))
1254 /* Create NEW_TYPE->USE_TYPE conversion. */
1255 new_oprnd
= make_ssa_name (use_type
, NULL
);
1256 pattern_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, new_oprnd
,
1258 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use_stmt
)) = pattern_stmt
;
1260 *type_in
= get_vectype_for_scalar_type (new_type
);
1261 *type_out
= get_vectype_for_scalar_type (use_type
);
1263 /* We created a pattern statement for the last statement in the
1264 sequence, so we don't need to associate it with the pattern
1265 statement created for PREV_STMT. Therefore, we add PREV_STMT
1266 to the list in order to mark it later in vect_pattern_recog_1. */
1268 stmts
->safe_push (prev_stmt
);
1273 STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (use_stmt
))
1274 = STMT_VINFO_PATTERN_DEF_SEQ (vinfo_for_stmt (prev_stmt
));
1277 *type_out
= NULL_TREE
;
1280 stmts
->safe_push (use_stmt
);
1283 /* TODO: support general case, create a conversion to the correct type. */
1286 /* Pattern detected. */
1287 if (dump_enabled_p ())
1289 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1290 "vect_recog_over_widening_pattern: detected: ");
1291 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
1294 return pattern_stmt
;
1297 /* Detect widening shift pattern:
1303 S2 a_T = (TYPE) a_t;
1304 S3 res_T = a_T << CONST;
1306 where type 'TYPE' is at least double the size of type 'type'.
1308 Also detect cases where the shift result is immediately converted
1309 to another type 'result_type' that is no larger in size than 'TYPE'.
1310 In those cases we perform a widen-shift that directly results in
1311 'result_type', to avoid a possible over-widening situation:
1315 result_type res_result;
1318 S2 a_T = (TYPE) a_t;
1319 S3 res_T = a_T << CONST;
1320 S4 res_result = (result_type) res_T;
1321 '--> res_result' = a_t w<< CONST;
1323 And a case when 'TYPE' is 4 times bigger than 'type'. In that case we
1324 create an additional pattern stmt for S2 to create a variable of an
1325 intermediate type, and perform widen-shift on the intermediate type:
1329 TYPE a_T, res_T, res_T';
1332 S2 a_T = (TYPE) a_t;
1333 '--> a_it = (interm_type) a_t;
1334 S3 res_T = a_T << CONST;
1335 '--> res_T' = a_it <<* CONST;
1339 * STMTS: Contains a stmt from which the pattern search begins.
1340 In case of unsigned widen-shift, the original stmt (S3) is replaced with S4
1341 in STMTS. When an intermediate type is used and a pattern statement is
1342 created for S2, we also put S2 here (before S3).
1346 * TYPE_IN: The type of the input arguments to the pattern.
1348 * TYPE_OUT: The type of the output of this pattern.
1350 * Return value: A new stmt that will be used to replace the sequence of
1351 stmts that constitute the pattern. In this case it will be:
1352 WIDEN_LSHIFT_EXPR <a_t, CONST>. */
1355 vect_recog_widen_shift_pattern (vec
<gimple
> *stmts
,
1356 tree
*type_in
, tree
*type_out
)
1358 gimple last_stmt
= stmts
->pop ();
1360 tree oprnd0
, oprnd1
;
1361 tree type
, half_type0
;
1362 gimple pattern_stmt
;
1363 tree vectype
, vectype_out
= NULL_TREE
;
1365 enum tree_code dummy_code
;
1367 vec
<tree
> dummy_vec
;
1371 if (!is_gimple_assign (last_stmt
) || !vinfo_for_stmt (last_stmt
))
1374 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (last_stmt
)))
1377 if (gimple_assign_rhs_code (last_stmt
) != LSHIFT_EXPR
)
1380 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1381 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1382 if (TREE_CODE (oprnd0
) != SSA_NAME
|| TREE_CODE (oprnd1
) != INTEGER_CST
)
1385 /* Check operand 0: it has to be defined by a type promotion. */
1386 if (!type_conversion_p (oprnd0
, last_stmt
, false, &half_type0
, &def_stmt0
,
1391 /* Check operand 1: has to be positive. We check that it fits the type
1392 in vect_handle_widen_op_by_const (). */
1393 if (tree_int_cst_compare (oprnd1
, size_zero_node
) <= 0)
1396 oprnd0
= gimple_assign_rhs1 (def_stmt0
);
1397 type
= gimple_expr_type (last_stmt
);
1399 /* Check for subsequent conversion to another type. */
1400 use_stmt
= vect_single_imm_use (last_stmt
);
1401 if (use_stmt
&& is_gimple_assign (use_stmt
)
1402 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt
))
1403 && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt
)))
1405 tree use_lhs
= gimple_assign_lhs (use_stmt
);
1406 tree use_type
= TREE_TYPE (use_lhs
);
1408 if (INTEGRAL_TYPE_P (use_type
)
1409 && TYPE_PRECISION (use_type
) <= TYPE_PRECISION (type
))
1411 last_stmt
= use_stmt
;
1416 /* Check if this a widening operation. */
1417 if (!vect_handle_widen_op_by_const (last_stmt
, LSHIFT_EXPR
, oprnd1
,
1419 type
, &half_type0
, def_stmt0
))
1422 /* Pattern detected. */
1423 if (dump_enabled_p ())
1424 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1425 "vect_recog_widen_shift_pattern: detected: ");
1427 /* Check target support. */
1428 vectype
= get_vectype_for_scalar_type (half_type0
);
1429 vectype_out
= get_vectype_for_scalar_type (type
);
1433 || !supportable_widening_operation (WIDEN_LSHIFT_EXPR
, last_stmt
,
1434 vectype_out
, vectype
,
1435 &dummy_code
, &dummy_code
,
1436 &dummy_int
, &dummy_vec
))
1440 *type_out
= vectype_out
;
1442 /* Pattern supported. Create a stmt to be used to replace the pattern. */
1443 var
= vect_recog_temp_ssa_var (type
, NULL
);
1445 gimple_build_assign_with_ops (WIDEN_LSHIFT_EXPR
, var
, oprnd0
, oprnd1
);
1447 if (dump_enabled_p ())
1448 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
1450 stmts
->safe_push (last_stmt
);
1451 return pattern_stmt
;
1454 /* Detect a vector by vector shift pattern that wouldn't be otherwise
1462 S3 res_T = b_T op a_t;
1464 where type 'TYPE' is a type with different size than 'type',
1465 and op is <<, >> or rotate.
1470 TYPE b_T, c_T, res_T;
1473 S1 a_t = (type) c_T;
1475 S3 res_T = b_T op a_t;
1479 * STMTS: Contains a stmt from which the pattern search begins,
1480 i.e. the shift/rotate stmt. The original stmt (S3) is replaced
1481 with a shift/rotate which has same type on both operands, in the
1482 second case just b_T op c_T, in the first case with added cast
1483 from a_t to c_T in STMT_VINFO_PATTERN_DEF_SEQ.
1487 * TYPE_IN: The type of the input arguments to the pattern.
1489 * TYPE_OUT: The type of the output of this pattern.
1491 * Return value: A new stmt that will be used to replace the shift/rotate
1495 vect_recog_vector_vector_shift_pattern (vec
<gimple
> *stmts
,
1496 tree
*type_in
, tree
*type_out
)
1498 gimple last_stmt
= stmts
->pop ();
1499 tree oprnd0
, oprnd1
, lhs
, var
;
1500 gimple pattern_stmt
, def_stmt
;
1501 enum tree_code rhs_code
;
1502 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
1503 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
1504 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
1505 enum vect_def_type dt
;
1508 if (!is_gimple_assign (last_stmt
))
1511 rhs_code
= gimple_assign_rhs_code (last_stmt
);
1523 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
1526 lhs
= gimple_assign_lhs (last_stmt
);
1527 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1528 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1529 if (TREE_CODE (oprnd0
) != SSA_NAME
1530 || TREE_CODE (oprnd1
) != SSA_NAME
1531 || TYPE_MODE (TREE_TYPE (oprnd0
)) == TYPE_MODE (TREE_TYPE (oprnd1
))
1532 || TYPE_PRECISION (TREE_TYPE (oprnd1
))
1533 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (oprnd1
)))
1534 || TYPE_PRECISION (TREE_TYPE (lhs
))
1535 != TYPE_PRECISION (TREE_TYPE (oprnd0
)))
1538 if (!vect_is_simple_use (oprnd1
, last_stmt
, loop_vinfo
, bb_vinfo
, &def_stmt
,
1542 if (dt
!= vect_internal_def
)
1545 *type_in
= get_vectype_for_scalar_type (TREE_TYPE (oprnd0
));
1546 *type_out
= *type_in
;
1547 if (*type_in
== NULL_TREE
)
1551 if (gimple_assign_cast_p (def_stmt
))
1553 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
1554 if (TYPE_MODE (TREE_TYPE (rhs1
)) == TYPE_MODE (TREE_TYPE (oprnd0
))
1555 && TYPE_PRECISION (TREE_TYPE (rhs1
))
1556 == TYPE_PRECISION (TREE_TYPE (oprnd0
)))
1560 if (def
== NULL_TREE
)
1562 def
= vect_recog_temp_ssa_var (TREE_TYPE (oprnd0
), NULL
);
1563 def_stmt
= gimple_build_assign_with_ops (NOP_EXPR
, def
, oprnd1
,
1565 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
1568 /* Pattern detected. */
1569 if (dump_enabled_p ())
1570 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1571 "vect_recog_vector_vector_shift_pattern: detected: ");
1573 /* Pattern supported. Create a stmt to be used to replace the pattern. */
1574 var
= vect_recog_temp_ssa_var (TREE_TYPE (oprnd0
), NULL
);
1575 pattern_stmt
= gimple_build_assign_with_ops (rhs_code
, var
, oprnd0
, def
);
1577 if (dump_enabled_p ())
1578 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
, 0);
1580 stmts
->safe_push (last_stmt
);
1581 return pattern_stmt
;
1584 /* Detect a signed division by a constant that wouldn't be
1585 otherwise vectorized:
1591 where type 'type' is an integral type and N is a constant.
1593 Similarly handle modulo by a constant:
1599 * STMTS: Contains a stmt from which the pattern search begins,
1600 i.e. the division stmt. S1 is replaced by if N is a power
1601 of two constant and type is signed:
1602 S3 y_t = b_t < 0 ? N - 1 : 0;
1604 S1' a_t = x_t >> log2 (N);
1606 S4 is replaced if N is a power of two constant and
1607 type is signed by (where *_T temporaries have unsigned type):
1608 S9 y_T = b_t < 0 ? -1U : 0U;
1609 S8 z_T = y_T >> (sizeof (type_t) * CHAR_BIT - log2 (N));
1610 S7 z_t = (type) z_T;
1612 S5 x_t = w_t & (N - 1);
1613 S4' a_t = x_t - z_t;
1617 * TYPE_IN: The type of the input arguments to the pattern.
1619 * TYPE_OUT: The type of the output of this pattern.
1621 * Return value: A new stmt that will be used to replace the division
1622 S1 or modulo S4 stmt. */
1625 vect_recog_divmod_pattern (vec
<gimple
> *stmts
,
1626 tree
*type_in
, tree
*type_out
)
1628 gimple last_stmt
= stmts
->pop ();
1629 tree oprnd0
, oprnd1
, vectype
, itype
, cond
;
1630 gimple pattern_stmt
, def_stmt
;
1631 enum tree_code rhs_code
;
1632 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
1633 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
1634 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
1637 int dummy_int
, prec
;
1638 stmt_vec_info def_stmt_vinfo
;
1640 if (!is_gimple_assign (last_stmt
))
1643 rhs_code
= gimple_assign_rhs_code (last_stmt
);
1646 case TRUNC_DIV_EXPR
:
1647 case TRUNC_MOD_EXPR
:
1653 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
))
1656 oprnd0
= gimple_assign_rhs1 (last_stmt
);
1657 oprnd1
= gimple_assign_rhs2 (last_stmt
);
1658 itype
= TREE_TYPE (oprnd0
);
1659 if (TREE_CODE (oprnd0
) != SSA_NAME
1660 || TREE_CODE (oprnd1
) != INTEGER_CST
1661 || TREE_CODE (itype
) != INTEGER_TYPE
1662 || TYPE_PRECISION (itype
) != GET_MODE_PRECISION (TYPE_MODE (itype
)))
1665 vectype
= get_vectype_for_scalar_type (itype
);
1666 if (vectype
== NULL_TREE
)
1669 /* If the target can handle vectorized division or modulo natively,
1670 don't attempt to optimize this. */
1671 optab
= optab_for_tree_code (rhs_code
, vectype
, optab_default
);
1672 if (optab
!= unknown_optab
)
1674 enum machine_mode vec_mode
= TYPE_MODE (vectype
);
1675 int icode
= (int) optab_handler (optab
, vec_mode
);
1676 if (icode
!= CODE_FOR_nothing
)
1680 prec
= TYPE_PRECISION (itype
);
1681 if (integer_pow2p (oprnd1
))
1683 if (TYPE_UNSIGNED (itype
) || tree_int_cst_sgn (oprnd1
) != 1)
1686 /* Pattern detected. */
1687 if (dump_enabled_p ())
1688 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
1689 "vect_recog_divmod_pattern: detected: ");
1691 cond
= build2 (LT_EXPR
, boolean_type_node
, oprnd0
,
1692 build_int_cst (itype
, 0));
1693 if (rhs_code
== TRUNC_DIV_EXPR
)
1695 tree var
= vect_recog_temp_ssa_var (itype
, NULL
);
1698 = gimple_build_assign_with_ops (COND_EXPR
, var
, cond
,
1699 fold_build2 (MINUS_EXPR
, itype
,
1701 build_int_cst (itype
,
1703 build_int_cst (itype
, 0));
1704 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
1705 var
= vect_recog_temp_ssa_var (itype
, NULL
);
1707 = gimple_build_assign_with_ops (PLUS_EXPR
, var
, oprnd0
,
1708 gimple_assign_lhs (def_stmt
));
1709 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1711 shift
= build_int_cst (itype
, tree_log2 (oprnd1
));
1713 = gimple_build_assign_with_ops (RSHIFT_EXPR
,
1714 vect_recog_temp_ssa_var (itype
,
1721 STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
) = NULL
;
1722 if (compare_tree_int (oprnd1
, 2) == 0)
1724 signmask
= vect_recog_temp_ssa_var (itype
, NULL
);
1726 = gimple_build_assign_with_ops (COND_EXPR
, signmask
, cond
,
1727 build_int_cst (itype
, 1),
1728 build_int_cst (itype
, 0));
1729 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1734 = build_nonstandard_integer_type (prec
, 1);
1735 tree vecutype
= get_vectype_for_scalar_type (utype
);
1737 = build_int_cst (utype
, GET_MODE_BITSIZE (TYPE_MODE (itype
))
1738 - tree_log2 (oprnd1
));
1739 tree var
= vect_recog_temp_ssa_var (utype
, NULL
);
1742 = gimple_build_assign_with_ops (COND_EXPR
, var
, cond
,
1743 build_int_cst (utype
, -1),
1744 build_int_cst (utype
, 0));
1746 = new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
1747 set_vinfo_for_stmt (def_stmt
, def_stmt_vinfo
);
1748 STMT_VINFO_VECTYPE (def_stmt_vinfo
) = vecutype
;
1749 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1750 var
= vect_recog_temp_ssa_var (utype
, NULL
);
1752 = gimple_build_assign_with_ops (RSHIFT_EXPR
, var
,
1753 gimple_assign_lhs (def_stmt
),
1756 = new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
1757 set_vinfo_for_stmt (def_stmt
, def_stmt_vinfo
);
1758 STMT_VINFO_VECTYPE (def_stmt_vinfo
) = vecutype
;
1759 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1760 signmask
= vect_recog_temp_ssa_var (itype
, NULL
);
1762 = gimple_build_assign_with_ops (NOP_EXPR
, signmask
, var
,
1764 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1767 = gimple_build_assign_with_ops (PLUS_EXPR
,
1768 vect_recog_temp_ssa_var (itype
,
1771 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1773 = gimple_build_assign_with_ops (BIT_AND_EXPR
,
1774 vect_recog_temp_ssa_var (itype
,
1776 gimple_assign_lhs (def_stmt
),
1777 fold_build2 (MINUS_EXPR
, itype
,
1779 build_int_cst (itype
,
1781 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1784 = gimple_build_assign_with_ops (MINUS_EXPR
,
1785 vect_recog_temp_ssa_var (itype
,
1787 gimple_assign_lhs (def_stmt
),
1791 if (dump_enabled_p ())
1792 dump_gimple_stmt_loc (MSG_NOTE
, vect_location
, TDF_SLIM
, pattern_stmt
,
1795 stmts
->safe_push (last_stmt
);
1798 *type_out
= vectype
;
1799 return pattern_stmt
;
1802 if (!host_integerp (oprnd1
, TYPE_UNSIGNED (itype
))
1803 || integer_zerop (oprnd1
)
1804 || prec
> HOST_BITS_PER_WIDE_INT
)
1807 if (!can_mult_highpart_p (TYPE_MODE (vectype
), TYPE_UNSIGNED (itype
)))
1810 STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
) = NULL
;
1812 if (TYPE_UNSIGNED (itype
))
1814 unsigned HOST_WIDE_INT mh
, ml
;
1815 int pre_shift
, post_shift
;
1816 unsigned HOST_WIDE_INT d
= tree_low_cst (oprnd1
, 1)
1817 & GET_MODE_MASK (TYPE_MODE (itype
));
1818 tree t1
, t2
, t3
, t4
;
1820 if (d
>= ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1)))
1821 /* FIXME: Can transform this into oprnd0 >= oprnd1 ? 1 : 0. */
1824 /* Find a suitable multiplier and right shift count
1825 instead of multiplying with D. */
1826 mh
= choose_multiplier (d
, prec
, prec
, &ml
, &post_shift
, &dummy_int
);
1828 /* If the suggested multiplier is more than SIZE bits, we can do better
1829 for even divisors, using an initial right shift. */
1830 if (mh
!= 0 && (d
& 1) == 0)
1832 pre_shift
= floor_log2 (d
& -d
);
1833 mh
= choose_multiplier (d
>> pre_shift
, prec
, prec
- pre_shift
,
1834 &ml
, &post_shift
, &dummy_int
);
1842 if (post_shift
- 1 >= prec
)
1845 /* t1 = oprnd0 h* ml;
1849 q = t4 >> (post_shift - 1); */
1850 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1852 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t1
, oprnd0
,
1853 build_int_cst (itype
, ml
));
1854 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1856 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1858 = gimple_build_assign_with_ops (MINUS_EXPR
, t2
, oprnd0
, t1
);
1859 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1861 t3
= vect_recog_temp_ssa_var (itype
, NULL
);
1863 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t3
, t2
,
1865 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1867 t4
= vect_recog_temp_ssa_var (itype
, NULL
);
1869 = gimple_build_assign_with_ops (PLUS_EXPR
, t4
, t1
, t3
);
1871 if (post_shift
!= 1)
1873 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1875 q
= vect_recog_temp_ssa_var (itype
, NULL
);
1877 = gimple_build_assign_with_ops (RSHIFT_EXPR
, q
, t4
,
1878 build_int_cst (itype
,
1885 pattern_stmt
= def_stmt
;
1890 if (pre_shift
>= prec
|| post_shift
>= prec
)
1893 /* t1 = oprnd0 >> pre_shift;
1895 q = t2 >> post_shift; */
1898 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1900 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t1
, oprnd0
,
1901 build_int_cst (NULL
,
1903 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1908 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1910 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t2
, t1
,
1911 build_int_cst (itype
, ml
));
1915 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1917 q
= vect_recog_temp_ssa_var (itype
, NULL
);
1919 = gimple_build_assign_with_ops (RSHIFT_EXPR
, q
, t2
,
1920 build_int_cst (itype
,
1926 pattern_stmt
= def_stmt
;
1931 unsigned HOST_WIDE_INT ml
;
1933 HOST_WIDE_INT d
= tree_low_cst (oprnd1
, 0);
1934 unsigned HOST_WIDE_INT abs_d
;
1936 tree t1
, t2
, t3
, t4
;
1938 /* Give up for -1. */
1942 /* Since d might be INT_MIN, we have to cast to
1943 unsigned HOST_WIDE_INT before negating to avoid
1944 undefined signed overflow. */
1946 ? (unsigned HOST_WIDE_INT
) d
1947 : - (unsigned HOST_WIDE_INT
) d
);
1949 /* n rem d = n rem -d */
1950 if (rhs_code
== TRUNC_MOD_EXPR
&& d
< 0)
1953 oprnd1
= build_int_cst (itype
, abs_d
);
1955 else if (HOST_BITS_PER_WIDE_INT
>= prec
1956 && abs_d
== (unsigned HOST_WIDE_INT
) 1 << (prec
- 1))
1957 /* This case is not handled correctly below. */
1960 choose_multiplier (abs_d
, prec
, prec
- 1, &ml
, &post_shift
, &dummy_int
);
1961 if (ml
>= (unsigned HOST_WIDE_INT
) 1 << (prec
- 1))
1964 ml
|= (~(unsigned HOST_WIDE_INT
) 0) << (prec
- 1);
1966 if (post_shift
>= prec
)
1969 /* t1 = oprnd1 h* ml; */
1970 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
1972 = gimple_build_assign_with_ops (MULT_HIGHPART_EXPR
, t1
, oprnd0
,
1973 build_int_cst (itype
, ml
));
1974 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1978 /* t2 = t1 + oprnd0; */
1979 t2
= vect_recog_temp_ssa_var (itype
, NULL
);
1981 = gimple_build_assign_with_ops (PLUS_EXPR
, t2
, t1
, oprnd0
);
1982 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1989 /* t3 = t2 >> post_shift; */
1990 t3
= vect_recog_temp_ssa_var (itype
, NULL
);
1992 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t3
, t2
,
1993 build_int_cst (itype
, post_shift
));
1994 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
1999 /* t4 = oprnd0 >> (prec - 1); */
2000 t4
= vect_recog_temp_ssa_var (itype
, NULL
);
2002 = gimple_build_assign_with_ops (RSHIFT_EXPR
, t4
, oprnd0
,
2003 build_int_cst (itype
, prec
- 1));
2004 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
2006 /* q = t3 - t4; or q = t4 - t3; */
2007 q
= vect_recog_temp_ssa_var (itype
, NULL
);
2009 = gimple_build_assign_with_ops (MINUS_EXPR
, q
, d
< 0 ? t4
: t3
,
2013 if (rhs_code
== TRUNC_MOD_EXPR
)
2017 /* We divided. Now finish by:
2020 append_pattern_def_seq (stmt_vinfo
, pattern_stmt
);
2022 t1
= vect_recog_temp_ssa_var (itype
, NULL
);
2024 = gimple_build_assign_with_ops (MULT_EXPR
, t1
, q
, oprnd1
);
2025 append_pattern_def_seq (stmt_vinfo
, def_stmt
);
2027 r
= vect_recog_temp_ssa_var (itype
, NULL
);
2029 = gimple_build_assign_with_ops (MINUS_EXPR
, r
, oprnd0
, t1
);
2032 /* Pattern detected. */
2033 if (dump_enabled_p ())
2035 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2036 "vect_recog_divmod_pattern: detected: ");
2037 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2040 stmts
->safe_push (last_stmt
);
2043 *type_out
= vectype
;
2044 return pattern_stmt
;
2047 /* Function vect_recog_mixed_size_cond_pattern
2049 Try to find the following pattern:
2054 S1 a_T = x_t CMP y_t ? b_T : c_T;
2056 where type 'TYPE' is an integral type which has different size
2057 from 'type'. b_T and c_T are either constants (and if 'TYPE' is wider
2058 than 'type', the constants need to fit into an integer type
2059 with the same width as 'type') or results of conversion from 'type'.
2063 * LAST_STMT: A stmt from which the pattern search begins.
2067 * TYPE_IN: The type of the input arguments to the pattern.
2069 * TYPE_OUT: The type of the output of this pattern.
2071 * Return value: A new stmt that will be used to replace the pattern.
2072 Additionally a def_stmt is added.
2074 a_it = x_t CMP y_t ? b_it : c_it;
2075 a_T = (TYPE) a_it; */
2078 vect_recog_mixed_size_cond_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
2081 gimple last_stmt
= (*stmts
)[0];
2082 tree cond_expr
, then_clause
, else_clause
;
2083 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
), def_stmt_info
;
2084 tree type
, vectype
, comp_vectype
, itype
= NULL_TREE
, vecitype
;
2085 enum machine_mode cmpmode
;
2086 gimple pattern_stmt
, def_stmt
;
2087 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2088 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
2089 tree orig_type0
= NULL_TREE
, orig_type1
= NULL_TREE
;
2090 gimple def_stmt0
= NULL
, def_stmt1
= NULL
;
2092 tree comp_scalar_type
;
2094 if (!is_gimple_assign (last_stmt
)
2095 || gimple_assign_rhs_code (last_stmt
) != COND_EXPR
2096 || STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_internal_def
)
2099 cond_expr
= gimple_assign_rhs1 (last_stmt
);
2100 then_clause
= gimple_assign_rhs2 (last_stmt
);
2101 else_clause
= gimple_assign_rhs3 (last_stmt
);
2103 if (!COMPARISON_CLASS_P (cond_expr
))
2106 comp_scalar_type
= TREE_TYPE (TREE_OPERAND (cond_expr
, 0));
2107 comp_vectype
= get_vectype_for_scalar_type (comp_scalar_type
);
2108 if (comp_vectype
== NULL_TREE
)
2111 type
= gimple_expr_type (last_stmt
);
2112 if (types_compatible_p (type
, comp_scalar_type
)
2113 || ((TREE_CODE (then_clause
) != INTEGER_CST
2114 || TREE_CODE (else_clause
) != INTEGER_CST
)
2115 && !INTEGRAL_TYPE_P (comp_scalar_type
))
2116 || !INTEGRAL_TYPE_P (type
))
2119 if ((TREE_CODE (then_clause
) != INTEGER_CST
2120 && !type_conversion_p (then_clause
, last_stmt
, false, &orig_type0
,
2121 &def_stmt0
, &promotion
))
2122 || (TREE_CODE (else_clause
) != INTEGER_CST
2123 && !type_conversion_p (else_clause
, last_stmt
, false, &orig_type1
,
2124 &def_stmt1
, &promotion
)))
2127 if (orig_type0
&& orig_type1
2128 && !types_compatible_p (orig_type0
, orig_type1
))
2133 if (!types_compatible_p (orig_type0
, comp_scalar_type
))
2135 then_clause
= gimple_assign_rhs1 (def_stmt0
);
2141 if (!types_compatible_p (orig_type1
, comp_scalar_type
))
2143 else_clause
= gimple_assign_rhs1 (def_stmt1
);
2147 cmpmode
= GET_MODE_INNER (TYPE_MODE (comp_vectype
));
2149 if (GET_MODE_BITSIZE (TYPE_MODE (type
)) == GET_MODE_BITSIZE (cmpmode
))
2152 vectype
= get_vectype_for_scalar_type (type
);
2153 if (vectype
== NULL_TREE
)
2156 if (expand_vec_cond_expr_p (vectype
, comp_vectype
))
2159 if (itype
== NULL_TREE
)
2160 itype
= build_nonstandard_integer_type (GET_MODE_BITSIZE (cmpmode
),
2161 TYPE_UNSIGNED (type
));
2163 if (itype
== NULL_TREE
2164 || GET_MODE_BITSIZE (TYPE_MODE (itype
)) != GET_MODE_BITSIZE (cmpmode
))
2167 vecitype
= get_vectype_for_scalar_type (itype
);
2168 if (vecitype
== NULL_TREE
)
2171 if (!expand_vec_cond_expr_p (vecitype
, comp_vectype
))
2174 if (GET_MODE_BITSIZE (TYPE_MODE (type
)) > GET_MODE_BITSIZE (cmpmode
))
2176 if ((TREE_CODE (then_clause
) == INTEGER_CST
2177 && !int_fits_type_p (then_clause
, itype
))
2178 || (TREE_CODE (else_clause
) == INTEGER_CST
2179 && !int_fits_type_p (else_clause
, itype
)))
2184 = gimple_build_assign_with_ops (COND_EXPR
,
2185 vect_recog_temp_ssa_var (itype
, NULL
),
2186 unshare_expr (cond_expr
),
2187 fold_convert (itype
, then_clause
),
2188 fold_convert (itype
, else_clause
));
2190 = gimple_build_assign_with_ops (NOP_EXPR
,
2191 vect_recog_temp_ssa_var (type
, NULL
),
2192 gimple_assign_lhs (def_stmt
), NULL_TREE
);
2194 new_pattern_def_seq (stmt_vinfo
, def_stmt
);
2195 def_stmt_info
= new_stmt_vec_info (def_stmt
, loop_vinfo
, bb_vinfo
);
2196 set_vinfo_for_stmt (def_stmt
, def_stmt_info
);
2197 STMT_VINFO_VECTYPE (def_stmt_info
) = vecitype
;
2198 *type_in
= vecitype
;
2199 *type_out
= vectype
;
2201 if (dump_enabled_p ())
2202 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2203 "vect_recog_mixed_size_cond_pattern: detected: ");
2205 return pattern_stmt
;
2209 /* Helper function of vect_recog_bool_pattern. Called recursively, return
2210 true if bool VAR can be optimized that way. */
2213 check_bool_pattern (tree var
, loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2216 enum vect_def_type dt
;
2218 enum tree_code rhs_code
;
2220 if (!vect_is_simple_use (var
, NULL
, loop_vinfo
, bb_vinfo
, &def_stmt
, &def
,
2224 if (dt
!= vect_internal_def
)
2227 if (!is_gimple_assign (def_stmt
))
2230 if (!has_single_use (def
))
2233 rhs1
= gimple_assign_rhs1 (def_stmt
);
2234 rhs_code
= gimple_assign_rhs_code (def_stmt
);
2238 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2241 if ((TYPE_PRECISION (TREE_TYPE (rhs1
)) != 1
2242 || !TYPE_UNSIGNED (TREE_TYPE (rhs1
)))
2243 && TREE_CODE (TREE_TYPE (rhs1
)) != BOOLEAN_TYPE
)
2245 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2248 return check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
);
2253 if (!check_bool_pattern (rhs1
, loop_vinfo
, bb_vinfo
))
2255 return check_bool_pattern (gimple_assign_rhs2 (def_stmt
), loop_vinfo
,
2259 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
2261 tree vecitype
, comp_vectype
;
2263 /* If the comparison can throw, then is_gimple_condexpr will be
2264 false and we can't make a COND_EXPR/VEC_COND_EXPR out of it. */
2265 if (stmt_could_throw_p (def_stmt
))
2268 comp_vectype
= get_vectype_for_scalar_type (TREE_TYPE (rhs1
));
2269 if (comp_vectype
== NULL_TREE
)
2272 if (TREE_CODE (TREE_TYPE (rhs1
)) != INTEGER_TYPE
)
2274 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (rhs1
));
2276 = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode
), 1);
2277 vecitype
= get_vectype_for_scalar_type (itype
);
2278 if (vecitype
== NULL_TREE
)
2282 vecitype
= comp_vectype
;
2283 return expand_vec_cond_expr_p (vecitype
, comp_vectype
);
2290 /* Helper function of adjust_bool_pattern. Add a cast to TYPE to a previous
2291 stmt (SSA_NAME_DEF_STMT of VAR) by moving the COND_EXPR from RELATED_STMT
2292 to PATTERN_DEF_SEQ and adding a cast as RELATED_STMT. */
2295 adjust_bool_pattern_cast (tree type
, tree var
)
2297 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (SSA_NAME_DEF_STMT (var
));
2298 gimple cast_stmt
, pattern_stmt
;
2300 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo
));
2301 pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_vinfo
);
2302 new_pattern_def_seq (stmt_vinfo
, pattern_stmt
);
2304 = gimple_build_assign_with_ops (NOP_EXPR
,
2305 vect_recog_temp_ssa_var (type
, NULL
),
2306 gimple_assign_lhs (pattern_stmt
),
2308 STMT_VINFO_RELATED_STMT (stmt_vinfo
) = cast_stmt
;
2309 return gimple_assign_lhs (cast_stmt
);
2313 /* Helper function of vect_recog_bool_pattern. Do the actual transformations,
2314 recursively. VAR is an SSA_NAME that should be transformed from bool
2315 to a wider integer type, OUT_TYPE is the desired final integer type of
2316 the whole pattern, TRUEVAL should be NULL unless optimizing
2317 BIT_AND_EXPR into a COND_EXPR with one integer from one of the operands
2318 in the then_clause, STMTS is where statements with added pattern stmts
2319 should be pushed to. */
2322 adjust_bool_pattern (tree var
, tree out_type
, tree trueval
,
2325 gimple stmt
= SSA_NAME_DEF_STMT (var
);
2326 enum tree_code rhs_code
, def_rhs_code
;
2327 tree itype
, cond_expr
, rhs1
, rhs2
, irhs1
, irhs2
;
2329 gimple pattern_stmt
, def_stmt
;
2331 rhs1
= gimple_assign_rhs1 (stmt
);
2332 rhs2
= gimple_assign_rhs2 (stmt
);
2333 rhs_code
= gimple_assign_rhs_code (stmt
);
2334 loc
= gimple_location (stmt
);
2339 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2340 itype
= TREE_TYPE (irhs1
);
2342 = gimple_build_assign_with_ops (SSA_NAME
,
2343 vect_recog_temp_ssa_var (itype
, NULL
),
2348 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2349 itype
= TREE_TYPE (irhs1
);
2351 = gimple_build_assign_with_ops (BIT_XOR_EXPR
,
2352 vect_recog_temp_ssa_var (itype
, NULL
),
2353 irhs1
, build_int_cst (itype
, 1));
2357 /* Try to optimize x = y & (a < b ? 1 : 0); into
2358 x = (a < b ? y : 0);
2364 S1 a_b = x1 CMP1 y1;
2365 S2 b_b = x2 CMP2 y2;
2367 S4 d_T = (TYPE) c_b;
2369 we would normally emit:
2371 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2372 S2' b_T = x2 CMP2 y2 ? 1 : 0;
2373 S3' c_T = a_T & b_T;
2376 but we can save one stmt by using the
2377 result of one of the COND_EXPRs in the other COND_EXPR and leave
2378 BIT_AND_EXPR stmt out:
2380 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2381 S3' c_T = x2 CMP2 y2 ? a_T : 0;
2384 At least when VEC_COND_EXPR is implemented using masks
2385 cond ? 1 : 0 is as expensive as cond ? var : 0, in both cases it
2386 computes the comparison masks and ands it, in one case with
2387 all ones vector, in the other case with a vector register.
2388 Don't do this for BIT_IOR_EXPR, because cond ? 1 : var; is
2389 often more expensive. */
2390 def_stmt
= SSA_NAME_DEF_STMT (rhs2
);
2391 def_rhs_code
= gimple_assign_rhs_code (def_stmt
);
2392 if (TREE_CODE_CLASS (def_rhs_code
) == tcc_comparison
)
2394 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2395 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2396 if (TYPE_PRECISION (TREE_TYPE (irhs1
))
2397 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1
))))
2400 stmt_vec_info stmt_def_vinfo
= vinfo_for_stmt (def_stmt
);
2401 irhs2
= adjust_bool_pattern (rhs2
, out_type
, irhs1
, stmts
);
2402 tstmt
= stmts
->pop ();
2403 gcc_assert (tstmt
== def_stmt
);
2404 stmts
->quick_push (stmt
);
2405 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
))
2406 = STMT_VINFO_RELATED_STMT (stmt_def_vinfo
);
2407 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_def_vinfo
));
2408 STMT_VINFO_RELATED_STMT (stmt_def_vinfo
) = NULL
;
2412 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2415 def_stmt
= SSA_NAME_DEF_STMT (rhs1
);
2416 def_rhs_code
= gimple_assign_rhs_code (def_stmt
);
2417 if (TREE_CODE_CLASS (def_rhs_code
) == tcc_comparison
)
2419 tree def_rhs1
= gimple_assign_rhs1 (def_stmt
);
2420 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2421 if (TYPE_PRECISION (TREE_TYPE (irhs2
))
2422 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (def_rhs1
))))
2425 stmt_vec_info stmt_def_vinfo
= vinfo_for_stmt (def_stmt
);
2426 irhs1
= adjust_bool_pattern (rhs1
, out_type
, irhs2
, stmts
);
2427 tstmt
= stmts
->pop ();
2428 gcc_assert (tstmt
== def_stmt
);
2429 stmts
->quick_push (stmt
);
2430 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
))
2431 = STMT_VINFO_RELATED_STMT (stmt_def_vinfo
);
2432 gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_def_vinfo
));
2433 STMT_VINFO_RELATED_STMT (stmt_def_vinfo
) = NULL
;
2437 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2443 irhs1
= adjust_bool_pattern (rhs1
, out_type
, NULL_TREE
, stmts
);
2444 irhs2
= adjust_bool_pattern (rhs2
, out_type
, NULL_TREE
, stmts
);
2446 if (TYPE_PRECISION (TREE_TYPE (irhs1
))
2447 != TYPE_PRECISION (TREE_TYPE (irhs2
)))
2449 int prec1
= TYPE_PRECISION (TREE_TYPE (irhs1
));
2450 int prec2
= TYPE_PRECISION (TREE_TYPE (irhs2
));
2451 int out_prec
= TYPE_PRECISION (out_type
);
2452 if (absu_hwi (out_prec
- prec1
) < absu_hwi (out_prec
- prec2
))
2453 irhs2
= adjust_bool_pattern_cast (TREE_TYPE (irhs1
), rhs2
);
2454 else if (absu_hwi (out_prec
- prec1
) > absu_hwi (out_prec
- prec2
))
2455 irhs1
= adjust_bool_pattern_cast (TREE_TYPE (irhs2
), rhs1
);
2458 irhs1
= adjust_bool_pattern_cast (out_type
, rhs1
);
2459 irhs2
= adjust_bool_pattern_cast (out_type
, rhs2
);
2462 itype
= TREE_TYPE (irhs1
);
2464 = gimple_build_assign_with_ops (rhs_code
,
2465 vect_recog_temp_ssa_var (itype
, NULL
),
2470 gcc_assert (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
);
2471 if (TREE_CODE (TREE_TYPE (rhs1
)) != INTEGER_TYPE
2472 || !TYPE_UNSIGNED (TREE_TYPE (rhs1
))
2473 || (TYPE_PRECISION (TREE_TYPE (rhs1
))
2474 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1
)))))
2476 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (rhs1
));
2478 = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode
), 1);
2481 itype
= TREE_TYPE (rhs1
);
2482 cond_expr
= build2_loc (loc
, rhs_code
, itype
, rhs1
, rhs2
);
2483 if (trueval
== NULL_TREE
)
2484 trueval
= build_int_cst (itype
, 1);
2486 gcc_checking_assert (useless_type_conversion_p (itype
,
2487 TREE_TYPE (trueval
)));
2489 = gimple_build_assign_with_ops (COND_EXPR
,
2490 vect_recog_temp_ssa_var (itype
, NULL
),
2492 build_int_cst (itype
, 0));
2496 stmts
->safe_push (stmt
);
2497 gimple_set_location (pattern_stmt
, loc
);
2498 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
)) = pattern_stmt
;
2499 return gimple_assign_lhs (pattern_stmt
);
2503 /* Function vect_recog_bool_pattern
2505 Try to find pattern like following:
2507 bool a_b, b_b, c_b, d_b, e_b;
2510 S1 a_b = x1 CMP1 y1;
2511 S2 b_b = x2 CMP2 y2;
2513 S4 d_b = x3 CMP3 y3;
2515 S6 f_T = (TYPE) e_b;
2517 where type 'TYPE' is an integral type.
2521 * LAST_STMT: A stmt at the end from which the pattern
2522 search begins, i.e. cast of a bool to
2527 * TYPE_IN: The type of the input arguments to the pattern.
2529 * TYPE_OUT: The type of the output of this pattern.
2531 * Return value: A new stmt that will be used to replace the pattern.
2533 Assuming size of TYPE is the same as size of all comparisons
2534 (otherwise some casts would be added where needed), the above
2535 sequence we create related pattern stmts:
2536 S1' a_T = x1 CMP1 y1 ? 1 : 0;
2537 S3' c_T = x2 CMP2 y2 ? a_T : 0;
2538 S4' d_T = x3 CMP3 y3 ? 1 : 0;
2539 S5' e_T = c_T | d_T;
2542 Instead of the above S3' we could emit:
2543 S2' b_T = x2 CMP2 y2 ? 1 : 0;
2544 S3' c_T = a_T | b_T;
2545 but the above is more efficient. */
2548 vect_recog_bool_pattern (vec
<gimple
> *stmts
, tree
*type_in
,
2551 gimple last_stmt
= stmts
->pop ();
2552 enum tree_code rhs_code
;
2553 tree var
, lhs
, rhs
, vectype
;
2554 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (last_stmt
);
2555 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2556 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_vinfo
);
2557 gimple pattern_stmt
;
2559 if (!is_gimple_assign (last_stmt
))
2562 var
= gimple_assign_rhs1 (last_stmt
);
2563 lhs
= gimple_assign_lhs (last_stmt
);
2565 if ((TYPE_PRECISION (TREE_TYPE (var
)) != 1
2566 || !TYPE_UNSIGNED (TREE_TYPE (var
)))
2567 && TREE_CODE (TREE_TYPE (var
)) != BOOLEAN_TYPE
)
2570 rhs_code
= gimple_assign_rhs_code (last_stmt
);
2571 if (CONVERT_EXPR_CODE_P (rhs_code
))
2573 if (TREE_CODE (TREE_TYPE (lhs
)) != INTEGER_TYPE
2574 || TYPE_PRECISION (TREE_TYPE (lhs
)) == 1)
2576 vectype
= get_vectype_for_scalar_type (TREE_TYPE (lhs
));
2577 if (vectype
== NULL_TREE
)
2580 if (!check_bool_pattern (var
, loop_vinfo
, bb_vinfo
))
2583 rhs
= adjust_bool_pattern (var
, TREE_TYPE (lhs
), NULL_TREE
, stmts
);
2584 lhs
= vect_recog_temp_ssa_var (TREE_TYPE (lhs
), NULL
);
2585 if (useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2587 = gimple_build_assign_with_ops (SSA_NAME
, lhs
, rhs
, NULL_TREE
);
2590 = gimple_build_assign_with_ops (NOP_EXPR
, lhs
, rhs
, NULL_TREE
);
2591 *type_out
= vectype
;
2593 stmts
->safe_push (last_stmt
);
2594 if (dump_enabled_p ())
2595 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2596 "vect_recog_bool_pattern: detected: ");
2598 return pattern_stmt
;
2600 else if (rhs_code
== SSA_NAME
2601 && STMT_VINFO_DATA_REF (stmt_vinfo
))
2603 stmt_vec_info pattern_stmt_info
;
2604 vectype
= STMT_VINFO_VECTYPE (stmt_vinfo
);
2605 gcc_assert (vectype
!= NULL_TREE
);
2606 if (!VECTOR_MODE_P (TYPE_MODE (vectype
)))
2608 if (!check_bool_pattern (var
, loop_vinfo
, bb_vinfo
))
2611 rhs
= adjust_bool_pattern (var
, TREE_TYPE (vectype
), NULL_TREE
, stmts
);
2612 lhs
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (vectype
), lhs
);
2613 if (!useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (rhs
)))
2615 tree rhs2
= vect_recog_temp_ssa_var (TREE_TYPE (lhs
), NULL
);
2617 = gimple_build_assign_with_ops (NOP_EXPR
, rhs2
, rhs
, NULL_TREE
);
2618 new_pattern_def_seq (stmt_vinfo
, cast_stmt
);
2622 = gimple_build_assign_with_ops (SSA_NAME
, lhs
, rhs
, NULL_TREE
);
2623 pattern_stmt_info
= new_stmt_vec_info (pattern_stmt
, loop_vinfo
,
2625 set_vinfo_for_stmt (pattern_stmt
, pattern_stmt_info
);
2626 STMT_VINFO_DATA_REF (pattern_stmt_info
)
2627 = STMT_VINFO_DATA_REF (stmt_vinfo
);
2628 STMT_VINFO_DR_BASE_ADDRESS (pattern_stmt_info
)
2629 = STMT_VINFO_DR_BASE_ADDRESS (stmt_vinfo
);
2630 STMT_VINFO_DR_INIT (pattern_stmt_info
) = STMT_VINFO_DR_INIT (stmt_vinfo
);
2631 STMT_VINFO_DR_OFFSET (pattern_stmt_info
)
2632 = STMT_VINFO_DR_OFFSET (stmt_vinfo
);
2633 STMT_VINFO_DR_STEP (pattern_stmt_info
) = STMT_VINFO_DR_STEP (stmt_vinfo
);
2634 STMT_VINFO_DR_ALIGNED_TO (pattern_stmt_info
)
2635 = STMT_VINFO_DR_ALIGNED_TO (stmt_vinfo
);
2636 DR_STMT (STMT_VINFO_DATA_REF (stmt_vinfo
)) = pattern_stmt
;
2637 *type_out
= vectype
;
2639 stmts
->safe_push (last_stmt
);
2640 if (dump_enabled_p ())
2641 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2642 "vect_recog_bool_pattern: detected: ");
2643 return pattern_stmt
;
2650 /* Mark statements that are involved in a pattern. */
2653 vect_mark_pattern_stmts (gimple orig_stmt
, gimple pattern_stmt
,
2654 tree pattern_vectype
)
2656 stmt_vec_info pattern_stmt_info
, def_stmt_info
;
2657 stmt_vec_info orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
2658 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (orig_stmt_info
);
2659 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (orig_stmt_info
);
2662 pattern_stmt_info
= vinfo_for_stmt (pattern_stmt
);
2663 if (pattern_stmt_info
== NULL
)
2665 pattern_stmt_info
= new_stmt_vec_info (pattern_stmt
, loop_vinfo
,
2667 set_vinfo_for_stmt (pattern_stmt
, pattern_stmt_info
);
2669 gimple_set_bb (pattern_stmt
, gimple_bb (orig_stmt
));
2671 STMT_VINFO_RELATED_STMT (pattern_stmt_info
) = orig_stmt
;
2672 STMT_VINFO_DEF_TYPE (pattern_stmt_info
)
2673 = STMT_VINFO_DEF_TYPE (orig_stmt_info
);
2674 STMT_VINFO_VECTYPE (pattern_stmt_info
) = pattern_vectype
;
2675 STMT_VINFO_IN_PATTERN_P (orig_stmt_info
) = true;
2676 STMT_VINFO_RELATED_STMT (orig_stmt_info
) = pattern_stmt
;
2677 STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
)
2678 = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info
);
2679 if (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
))
2681 gimple_stmt_iterator si
;
2682 for (si
= gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (pattern_stmt_info
));
2683 !gsi_end_p (si
); gsi_next (&si
))
2685 def_stmt
= gsi_stmt (si
);
2686 def_stmt_info
= vinfo_for_stmt (def_stmt
);
2687 if (def_stmt_info
== NULL
)
2689 def_stmt_info
= new_stmt_vec_info (def_stmt
, loop_vinfo
,
2691 set_vinfo_for_stmt (def_stmt
, def_stmt_info
);
2693 gimple_set_bb (def_stmt
, gimple_bb (orig_stmt
));
2694 STMT_VINFO_RELATED_STMT (def_stmt_info
) = orig_stmt
;
2695 STMT_VINFO_DEF_TYPE (def_stmt_info
)
2696 = STMT_VINFO_DEF_TYPE (orig_stmt_info
);
2697 if (STMT_VINFO_VECTYPE (def_stmt_info
) == NULL_TREE
)
2698 STMT_VINFO_VECTYPE (def_stmt_info
) = pattern_vectype
;
2703 /* Function vect_pattern_recog_1
2706 PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
2707 computation pattern.
2708 STMT: A stmt from which the pattern search should start.
2710 If PATTERN_RECOG_FUNC successfully detected the pattern, it creates an
2711 expression that computes the same functionality and can be used to
2712 replace the sequence of stmts that are involved in the pattern.
2715 This function checks if the expression returned by PATTERN_RECOG_FUNC is
2716 supported in vector form by the target. We use 'TYPE_IN' to obtain the
2717 relevant vector type. If 'TYPE_IN' is already a vector type, then this
2718 indicates that target support had already been checked by PATTERN_RECOG_FUNC.
2719 If 'TYPE_OUT' is also returned by PATTERN_RECOG_FUNC, we check that it fits
2720 to the available target pattern.
2722 This function also does some bookkeeping, as explained in the documentation
2723 for vect_recog_pattern. */
2726 vect_pattern_recog_1 (vect_recog_func_ptr vect_recog_func
,
2727 gimple_stmt_iterator si
,
2728 vec
<gimple
> *stmts_to_replace
)
2730 gimple stmt
= gsi_stmt (si
), pattern_stmt
;
2731 stmt_vec_info stmt_info
;
2732 loop_vec_info loop_vinfo
;
2733 tree pattern_vectype
;
2734 tree type_in
, type_out
;
2735 enum tree_code code
;
2739 stmts_to_replace
->truncate (0);
2740 stmts_to_replace
->quick_push (stmt
);
2741 pattern_stmt
= (* vect_recog_func
) (stmts_to_replace
, &type_in
, &type_out
);
2745 stmt
= stmts_to_replace
->last ();
2746 stmt_info
= vinfo_for_stmt (stmt
);
2747 loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2749 if (VECTOR_MODE_P (TYPE_MODE (type_in
)))
2751 /* No need to check target support (already checked by the pattern
2752 recognition function). */
2753 pattern_vectype
= type_out
? type_out
: type_in
;
2757 enum machine_mode vec_mode
;
2758 enum insn_code icode
;
2761 /* Check target support */
2762 type_in
= get_vectype_for_scalar_type (type_in
);
2766 type_out
= get_vectype_for_scalar_type (type_out
);
2771 pattern_vectype
= type_out
;
2773 if (is_gimple_assign (pattern_stmt
))
2774 code
= gimple_assign_rhs_code (pattern_stmt
);
2777 gcc_assert (is_gimple_call (pattern_stmt
));
2781 optab
= optab_for_tree_code (code
, type_in
, optab_default
);
2782 vec_mode
= TYPE_MODE (type_in
);
2784 || (icode
= optab_handler (optab
, vec_mode
)) == CODE_FOR_nothing
2785 || (insn_data
[icode
].operand
[0].mode
!= TYPE_MODE (type_out
)))
2789 /* Found a vectorizable pattern. */
2790 if (dump_enabled_p ())
2792 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2793 "pattern recognized: ");
2794 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2797 /* Mark the stmts that are involved in the pattern. */
2798 vect_mark_pattern_stmts (stmt
, pattern_stmt
, pattern_vectype
);
2800 /* Patterns cannot be vectorized using SLP, because they change the order of
2803 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTIONS (loop_vinfo
), i
, next
)
2805 LOOP_VINFO_REDUCTIONS (loop_vinfo
).ordered_remove (i
);
2807 /* It is possible that additional pattern stmts are created and inserted in
2808 STMTS_TO_REPLACE. We create a stmt_info for each of them, and mark the
2809 relevant statements. */
2810 for (i
= 0; stmts_to_replace
->iterate (i
, &stmt
)
2811 && (unsigned) i
< (stmts_to_replace
->length () - 1);
2814 stmt_info
= vinfo_for_stmt (stmt
);
2815 pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
2816 if (dump_enabled_p ())
2818 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, vect_location
,
2819 "additional pattern stmt: ");
2820 dump_gimple_stmt (MSG_OPTIMIZED_LOCATIONS
, TDF_SLIM
, pattern_stmt
, 0);
2823 vect_mark_pattern_stmts (stmt
, pattern_stmt
, NULL_TREE
);
2828 /* Function vect_pattern_recog
2831 LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
2834 Output - for each computation idiom that is detected we create a new stmt
2835 that provides the same functionality and that can be vectorized. We
2836 also record some information in the struct_stmt_info of the relevant
2837 stmts, as explained below:
2839 At the entry to this function we have the following stmts, with the
2840 following initial value in the STMT_VINFO fields:
2842 stmt in_pattern_p related_stmt vec_stmt
2843 S1: a_i = .... - - -
2844 S2: a_2 = ..use(a_i).. - - -
2845 S3: a_1 = ..use(a_2).. - - -
2846 S4: a_0 = ..use(a_1).. - - -
2847 S5: ... = ..use(a_0).. - - -
2849 Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
2850 represented by a single stmt. We then:
2851 - create a new stmt S6 equivalent to the pattern (the stmt is not
2852 inserted into the code)
2853 - fill in the STMT_VINFO fields as follows:
2855 in_pattern_p related_stmt vec_stmt
2856 S1: a_i = .... - - -
2857 S2: a_2 = ..use(a_i).. - - -
2858 S3: a_1 = ..use(a_2).. - - -
2859 S4: a_0 = ..use(a_1).. true S6 -
2860 '---> S6: a_new = .... - S4 -
2861 S5: ... = ..use(a_0).. - - -
2863 (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
2864 to each other through the RELATED_STMT field).
2866 S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
2867 of S4 because it will replace all its uses. Stmts {S1,S2,S3} will
2868 remain irrelevant unless used by stmts other than S4.
2870 If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
2871 (because they are marked as irrelevant). It will vectorize S6, and record
2872 a pointer to the new vector stmt VS6 from S6 (as usual).
2873 S4 will be skipped, and S5 will be vectorized as usual:
2875 in_pattern_p related_stmt vec_stmt
2876 S1: a_i = .... - - -
2877 S2: a_2 = ..use(a_i).. - - -
2878 S3: a_1 = ..use(a_2).. - - -
2879 > VS6: va_new = .... - - -
2880 S4: a_0 = ..use(a_1).. true S6 VS6
2881 '---> S6: a_new = .... - S4 VS6
2882 > VS5: ... = ..vuse(va_new).. - - -
2883 S5: ... = ..use(a_0).. - - -
2885 DCE could then get rid of {S1,S2,S3,S4,S5} (if their defs are not used
2886 elsewhere), and we'll end up with:
2889 VS5: ... = ..vuse(va_new)..
2891 In case of more than one pattern statements, e.g., widen-mult with
2895 S2 a_T = (TYPE) a_t;
2896 '--> S3: a_it = (interm_type) a_t;
2897 S4 prod_T = a_T * CONST;
2898 '--> S5: prod_T' = a_it w* CONST;
2900 there may be other users of a_T outside the pattern. In that case S2 will
2901 be marked as relevant (as well as S3), and both S2 and S3 will be analyzed
2902 and vectorized. The vector stmt VS2 will be recorded in S2, and VS3 will
2903 be recorded in S3. */
2906 vect_pattern_recog (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2911 gimple_stmt_iterator si
;
2913 vect_recog_func_ptr vect_recog_func
;
2914 vec
<gimple
> stmts_to_replace
;
2915 stmts_to_replace
.create (1);
2918 if (dump_enabled_p ())
2919 dump_printf_loc (MSG_NOTE
, vect_location
,
2920 "=== vect_pattern_recog ===");
2924 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2925 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
2926 nbbs
= loop
->num_nodes
;
2930 bbs
= &BB_VINFO_BB (bb_vinfo
);
2934 /* Scan through the loop stmts, applying the pattern recognition
2935 functions starting at each stmt visited: */
2936 for (i
= 0; i
< nbbs
; i
++)
2938 basic_block bb
= bbs
[i
];
2939 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2941 if (bb_vinfo
&& (stmt
= gsi_stmt (si
))
2942 && vinfo_for_stmt (stmt
)
2943 && !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt
)))
2946 /* Scan over all generic vect_recog_xxx_pattern functions. */
2947 for (j
= 0; j
< NUM_PATTERNS
; j
++)
2949 vect_recog_func
= vect_vect_recog_func_ptrs
[j
];
2950 vect_pattern_recog_1 (vect_recog_func
, si
,
2956 stmts_to_replace
.release ();