Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / tree-vect-patterns.c
blobc94513cd8cd54d805726b9729b260a98668dae4e
1 /* Analysis Utilities for Loop Vectorization.
2 Copyright (C) 2006, 2007, 2008, 2009 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
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "target.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "tree-dump.h"
32 #include "cfgloop.h"
33 #include "expr.h"
34 #include "optabs.h"
35 #include "params.h"
36 #include "tree-chrec.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-data-ref.h"
39 #include "tree-vectorizer.h"
40 #include "recog.h"
41 #include "toplev.h"
43 /* Function prototypes */
44 static void vect_pattern_recog_1
45 (gimple (* ) (gimple, tree *, tree *), gimple_stmt_iterator);
46 static bool widened_name_p (tree, gimple, tree *, gimple *);
48 /* Pattern recognition functions */
49 static gimple vect_recog_widen_sum_pattern (gimple, tree *, tree *);
50 static gimple vect_recog_widen_mult_pattern (gimple, tree *, tree *);
51 static gimple vect_recog_dot_prod_pattern (gimple, tree *, tree *);
52 static gimple vect_recog_pow_pattern (gimple, tree *, tree *);
53 static vect_recog_func_ptr vect_vect_recog_func_ptrs[NUM_PATTERNS] = {
54 vect_recog_widen_mult_pattern,
55 vect_recog_widen_sum_pattern,
56 vect_recog_dot_prod_pattern,
57 vect_recog_pow_pattern};
60 /* Function widened_name_p
62 Check whether NAME, an ssa-name used in USE_STMT,
63 is a result of a type-promotion, such that:
64 DEF_STMT: NAME = NOP (name0)
65 where the type of name0 (HALF_TYPE) is smaller than the type of NAME.
68 static bool
69 widened_name_p (tree name, gimple use_stmt, tree *half_type, gimple *def_stmt)
71 tree dummy;
72 gimple dummy_gimple;
73 loop_vec_info loop_vinfo;
74 stmt_vec_info stmt_vinfo;
75 tree type = TREE_TYPE (name);
76 tree oprnd0;
77 enum vect_def_type dt;
78 tree def;
80 stmt_vinfo = vinfo_for_stmt (use_stmt);
81 loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
83 if (!vect_is_simple_use (name, loop_vinfo, NULL, def_stmt, &def, &dt))
84 return false;
86 if (dt != vect_internal_def
87 && dt != vect_external_def && dt != vect_constant_def)
88 return false;
90 if (! *def_stmt)
91 return false;
93 if (!is_gimple_assign (*def_stmt))
94 return false;
96 if (gimple_assign_rhs_code (*def_stmt) != NOP_EXPR)
97 return false;
99 oprnd0 = gimple_assign_rhs1 (*def_stmt);
101 *half_type = TREE_TYPE (oprnd0);
102 if (!INTEGRAL_TYPE_P (type) || !INTEGRAL_TYPE_P (*half_type)
103 || (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (*half_type))
104 || (TYPE_PRECISION (type) < (TYPE_PRECISION (*half_type) * 2)))
105 return false;
107 if (!vect_is_simple_use (oprnd0, loop_vinfo, NULL, &dummy_gimple, &dummy,
108 &dt))
109 return false;
111 return true;
114 /* Helper to return a new temporary for pattern of TYPE for STMT. If STMT
115 is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */
117 static tree
118 vect_recog_temp_ssa_var (tree type, gimple stmt)
120 tree var = create_tmp_var (type, "patt");
122 add_referenced_var (var);
123 var = make_ssa_name (var, stmt);
124 return var;
127 /* Function vect_recog_dot_prod_pattern
129 Try to find the following pattern:
131 type x_t, y_t;
132 TYPE1 prod;
133 TYPE2 sum = init;
134 loop:
135 sum_0 = phi <init, sum_1>
136 S1 x_t = ...
137 S2 y_t = ...
138 S3 x_T = (TYPE1) x_t;
139 S4 y_T = (TYPE1) y_t;
140 S5 prod = x_T * y_T;
141 [S6 prod = (TYPE2) prod; #optional]
142 S7 sum_1 = prod + sum_0;
144 where 'TYPE1' is exactly double the size of type 'type', and 'TYPE2' is the
145 same size of 'TYPE1' or bigger. This is a special case of a reduction
146 computation.
148 Input:
150 * LAST_STMT: A stmt from which the pattern search begins. In the example,
151 when this function is called with S7, the pattern {S3,S4,S5,S6,S7} will be
152 detected.
154 Output:
156 * TYPE_IN: The type of the input arguments to the pattern.
158 * TYPE_OUT: The type of the output of this pattern.
160 * Return value: A new stmt that will be used to replace the sequence of
161 stmts that constitute the pattern. In this case it will be:
162 WIDEN_DOT_PRODUCT <x_t, y_t, sum_0>
164 Note: The dot-prod idiom is a widening reduction pattern that is
165 vectorized without preserving all the intermediate results. It
166 produces only N/2 (widened) results (by summing up pairs of
167 intermediate results) rather than all N results. Therefore, we
168 cannot allow this pattern when we want to get all the results and in
169 the correct order (as is the case when this computation is in an
170 inner-loop nested in an outer-loop that us being vectorized). */
172 static gimple
173 vect_recog_dot_prod_pattern (gimple last_stmt, tree *type_in, tree *type_out)
175 gimple stmt;
176 tree oprnd0, oprnd1;
177 tree oprnd00, oprnd01;
178 stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
179 tree type, half_type;
180 gimple pattern_stmt;
181 tree prod_type;
182 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
183 struct loop *loop = LOOP_VINFO_LOOP (loop_info);
184 tree var, rhs;
186 if (!is_gimple_assign (last_stmt))
187 return NULL;
189 type = gimple_expr_type (last_stmt);
191 /* Look for the following pattern
192 DX = (TYPE1) X;
193 DY = (TYPE1) Y;
194 DPROD = DX * DY;
195 DDPROD = (TYPE2) DPROD;
196 sum_1 = DDPROD + sum_0;
197 In which
198 - DX is double the size of X
199 - DY is double the size of Y
200 - DX, DY, DPROD all have the same type
201 - sum is the same size of DPROD or bigger
202 - sum has been recognized as a reduction variable.
204 This is equivalent to:
205 DPROD = X w* Y; #widen mult
206 sum_1 = DPROD w+ sum_0; #widen summation
208 DPROD = X w* Y; #widen mult
209 sum_1 = DPROD + sum_0; #summation
212 /* Starting from LAST_STMT, follow the defs of its uses in search
213 of the above pattern. */
215 if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR)
216 return NULL;
218 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
220 /* Has been detected as widening-summation? */
222 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
223 type = gimple_expr_type (stmt);
224 if (gimple_assign_rhs_code (stmt) != WIDEN_SUM_EXPR)
225 return NULL;
226 oprnd0 = gimple_assign_rhs1 (stmt);
227 oprnd1 = gimple_assign_rhs2 (stmt);
228 half_type = TREE_TYPE (oprnd0);
230 else
232 gimple def_stmt;
234 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
235 return NULL;
236 oprnd0 = gimple_assign_rhs1 (last_stmt);
237 oprnd1 = gimple_assign_rhs2 (last_stmt);
238 if (!types_compatible_p (TREE_TYPE (oprnd0), type)
239 || !types_compatible_p (TREE_TYPE (oprnd1), type))
240 return NULL;
241 stmt = last_stmt;
243 if (widened_name_p (oprnd0, stmt, &half_type, &def_stmt))
245 stmt = def_stmt;
246 oprnd0 = gimple_assign_rhs1 (stmt);
248 else
249 half_type = type;
252 /* So far so good. Since last_stmt was detected as a (summation) reduction,
253 we know that oprnd1 is the reduction variable (defined by a loop-header
254 phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
255 Left to check that oprnd0 is defined by a (widen_)mult_expr */
257 prod_type = half_type;
258 stmt = SSA_NAME_DEF_STMT (oprnd0);
259 /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi
260 inside the loop (in case we are analyzing an outer-loop). */
261 if (!is_gimple_assign (stmt))
262 return NULL;
263 stmt_vinfo = vinfo_for_stmt (stmt);
264 gcc_assert (stmt_vinfo);
265 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_internal_def)
266 return NULL;
267 if (gimple_assign_rhs_code (stmt) != MULT_EXPR)
268 return NULL;
269 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
271 /* Has been detected as a widening multiplication? */
273 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
274 if (gimple_assign_rhs_code (stmt) != WIDEN_MULT_EXPR)
275 return NULL;
276 stmt_vinfo = vinfo_for_stmt (stmt);
277 gcc_assert (stmt_vinfo);
278 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_internal_def);
279 oprnd00 = gimple_assign_rhs1 (stmt);
280 oprnd01 = gimple_assign_rhs2 (stmt);
282 else
284 tree half_type0, half_type1;
285 gimple def_stmt;
286 tree oprnd0, oprnd1;
288 oprnd0 = gimple_assign_rhs1 (stmt);
289 oprnd1 = gimple_assign_rhs2 (stmt);
290 if (!types_compatible_p (TREE_TYPE (oprnd0), prod_type)
291 || !types_compatible_p (TREE_TYPE (oprnd1), prod_type))
292 return NULL;
293 if (!widened_name_p (oprnd0, stmt, &half_type0, &def_stmt))
294 return NULL;
295 oprnd00 = gimple_assign_rhs1 (def_stmt);
296 if (!widened_name_p (oprnd1, stmt, &half_type1, &def_stmt))
297 return NULL;
298 oprnd01 = gimple_assign_rhs1 (def_stmt);
299 if (!types_compatible_p (half_type0, half_type1))
300 return NULL;
301 if (TYPE_PRECISION (prod_type) != TYPE_PRECISION (half_type0) * 2)
302 return NULL;
305 half_type = TREE_TYPE (oprnd00);
306 *type_in = half_type;
307 *type_out = type;
309 /* Pattern detected. Create a stmt to be used to replace the pattern: */
310 var = vect_recog_temp_ssa_var (type, NULL);
311 rhs = build3 (DOT_PROD_EXPR, type, oprnd00, oprnd01, oprnd1),
312 pattern_stmt = gimple_build_assign (var, rhs);
314 if (vect_print_dump_info (REPORT_DETAILS))
316 fprintf (vect_dump, "vect_recog_dot_prod_pattern: detected: ");
317 print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
320 /* We don't allow changing the order of the computation in the inner-loop
321 when doing outer-loop vectorization. */
322 gcc_assert (!nested_in_vect_loop_p (loop, last_stmt));
324 return pattern_stmt;
327 /* Function vect_recog_widen_mult_pattern
329 Try to find the following pattern:
331 type a_t, b_t;
332 TYPE a_T, b_T, prod_T;
334 S1 a_t = ;
335 S2 b_t = ;
336 S3 a_T = (TYPE) a_t;
337 S4 b_T = (TYPE) b_t;
338 S5 prod_T = a_T * b_T;
340 where type 'TYPE' is at least double the size of type 'type'.
342 Input:
344 * LAST_STMT: A stmt from which the pattern search begins. In the example,
345 when this function is called with S5, the pattern {S3,S4,S5} is be detected.
347 Output:
349 * TYPE_IN: The type of the input arguments to the pattern.
351 * TYPE_OUT: The type of the output of this pattern.
353 * Return value: A new stmt that will be used to replace the sequence of
354 stmts that constitute the pattern. In this case it will be:
355 WIDEN_MULT <a_t, b_t>
358 static gimple
359 vect_recog_widen_mult_pattern (gimple last_stmt,
360 tree *type_in,
361 tree *type_out)
363 gimple def_stmt0, def_stmt1;
364 tree oprnd0, oprnd1;
365 tree type, half_type0, half_type1;
366 gimple pattern_stmt;
367 tree vectype;
368 tree dummy;
369 tree var;
370 enum tree_code dummy_code;
371 int dummy_int;
372 VEC (tree, heap) *dummy_vec;
374 if (!is_gimple_assign (last_stmt))
375 return NULL;
377 type = gimple_expr_type (last_stmt);
379 /* Starting from LAST_STMT, follow the defs of its uses in search
380 of the above pattern. */
382 if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR)
383 return NULL;
385 oprnd0 = gimple_assign_rhs1 (last_stmt);
386 oprnd1 = gimple_assign_rhs2 (last_stmt);
387 if (!types_compatible_p (TREE_TYPE (oprnd0), type)
388 || !types_compatible_p (TREE_TYPE (oprnd1), type))
389 return NULL;
391 /* Check argument 0 */
392 if (!widened_name_p (oprnd0, last_stmt, &half_type0, &def_stmt0))
393 return NULL;
394 oprnd0 = gimple_assign_rhs1 (def_stmt0);
396 /* Check argument 1 */
397 if (!widened_name_p (oprnd1, last_stmt, &half_type1, &def_stmt1))
398 return NULL;
399 oprnd1 = gimple_assign_rhs1 (def_stmt1);
401 if (!types_compatible_p (half_type0, half_type1))
402 return NULL;
404 /* Pattern detected. */
405 if (vect_print_dump_info (REPORT_DETAILS))
406 fprintf (vect_dump, "vect_recog_widen_mult_pattern: detected: ");
408 /* Check target support */
409 vectype = get_vectype_for_scalar_type (half_type0);
410 if (!vectype
411 || !supportable_widening_operation (WIDEN_MULT_EXPR, last_stmt, vectype,
412 &dummy, &dummy, &dummy_code,
413 &dummy_code, &dummy_int, &dummy_vec))
414 return NULL;
416 *type_in = vectype;
417 *type_out = NULL_TREE;
419 /* Pattern supported. Create a stmt to be used to replace the pattern: */
420 var = vect_recog_temp_ssa_var (type, NULL);
421 pattern_stmt = gimple_build_assign_with_ops (WIDEN_MULT_EXPR, var, oprnd0,
422 oprnd1);
423 SSA_NAME_DEF_STMT (var) = pattern_stmt;
425 if (vect_print_dump_info (REPORT_DETAILS))
426 print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
428 return pattern_stmt;
432 /* Function vect_recog_pow_pattern
434 Try to find the following pattern:
436 x = POW (y, N);
438 with POW being one of pow, powf, powi, powif and N being
439 either 2 or 0.5.
441 Input:
443 * LAST_STMT: A stmt from which the pattern search begins.
445 Output:
447 * TYPE_IN: The type of the input arguments to the pattern.
449 * TYPE_OUT: The type of the output of this pattern.
451 * Return value: A new stmt that will be used to replace the sequence of
452 stmts that constitute the pattern. In this case it will be:
453 x = x * x
455 x = sqrt (x)
458 static gimple
459 vect_recog_pow_pattern (gimple last_stmt, tree *type_in, tree *type_out)
461 tree type;
462 tree fn, base, exp = NULL;
463 gimple stmt;
464 tree var;
466 if (!is_gimple_call (last_stmt) || gimple_call_lhs (last_stmt) == NULL)
467 return NULL;
469 type = gimple_expr_type (last_stmt);
471 fn = gimple_call_fndecl (last_stmt);
472 switch (DECL_FUNCTION_CODE (fn))
474 case BUILT_IN_POWIF:
475 case BUILT_IN_POWI:
476 case BUILT_IN_POWF:
477 case BUILT_IN_POW:
478 base = gimple_call_arg (last_stmt, 0);
479 exp = gimple_call_arg (last_stmt, 1);
480 if (TREE_CODE (exp) != REAL_CST
481 && TREE_CODE (exp) != INTEGER_CST)
482 return NULL;
483 break;
485 default:
486 return NULL;
489 /* We now have a pow or powi builtin function call with a constant
490 exponent. */
492 *type_out = NULL_TREE;
494 /* Catch squaring. */
495 if ((host_integerp (exp, 0)
496 && tree_low_cst (exp, 0) == 2)
497 || (TREE_CODE (exp) == REAL_CST
498 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp), dconst2)))
500 *type_in = TREE_TYPE (base);
502 var = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
503 stmt = gimple_build_assign_with_ops (MULT_EXPR, var, base, base);
504 SSA_NAME_DEF_STMT (var) = stmt;
505 return stmt;
508 /* Catch square root. */
509 if (TREE_CODE (exp) == REAL_CST
510 && REAL_VALUES_EQUAL (TREE_REAL_CST (exp), dconsthalf))
512 tree newfn = mathfn_built_in (TREE_TYPE (base), BUILT_IN_SQRT);
513 *type_in = get_vectype_for_scalar_type (TREE_TYPE (base));
514 if (*type_in)
516 gimple stmt = gimple_build_call (newfn, 1, base);
517 if (vectorizable_function (stmt, *type_in, *type_in)
518 != NULL_TREE)
520 var = vect_recog_temp_ssa_var (TREE_TYPE (base), stmt);
521 gimple_call_set_lhs (stmt, var);
522 return stmt;
527 return NULL;
531 /* Function vect_recog_widen_sum_pattern
533 Try to find the following pattern:
535 type x_t;
536 TYPE x_T, sum = init;
537 loop:
538 sum_0 = phi <init, sum_1>
539 S1 x_t = *p;
540 S2 x_T = (TYPE) x_t;
541 S3 sum_1 = x_T + sum_0;
543 where type 'TYPE' is at least double the size of type 'type', i.e - we're
544 summing elements of type 'type' into an accumulator of type 'TYPE'. This is
545 a special case of a reduction computation.
547 Input:
549 * LAST_STMT: A stmt from which the pattern search begins. In the example,
550 when this function is called with S3, the pattern {S2,S3} will be detected.
552 Output:
554 * TYPE_IN: The type of the input arguments to the pattern.
556 * TYPE_OUT: The type of the output of this pattern.
558 * Return value: A new stmt that will be used to replace the sequence of
559 stmts that constitute the pattern. In this case it will be:
560 WIDEN_SUM <x_t, sum_0>
562 Note: The widening-sum idiom is a widening reduction pattern that is
563 vectorized without preserving all the intermediate results. It
564 produces only N/2 (widened) results (by summing up pairs of
565 intermediate results) rather than all N results. Therefore, we
566 cannot allow this pattern when we want to get all the results and in
567 the correct order (as is the case when this computation is in an
568 inner-loop nested in an outer-loop that us being vectorized). */
570 static gimple
571 vect_recog_widen_sum_pattern (gimple last_stmt, tree *type_in, tree *type_out)
573 gimple stmt;
574 tree oprnd0, oprnd1;
575 stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
576 tree type, half_type;
577 gimple pattern_stmt;
578 loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
579 struct loop *loop = LOOP_VINFO_LOOP (loop_info);
580 tree var;
582 if (!is_gimple_assign (last_stmt))
583 return NULL;
585 type = gimple_expr_type (last_stmt);
587 /* Look for the following pattern
588 DX = (TYPE) X;
589 sum_1 = DX + sum_0;
590 In which DX is at least double the size of X, and sum_1 has been
591 recognized as a reduction variable.
594 /* Starting from LAST_STMT, follow the defs of its uses in search
595 of the above pattern. */
597 if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR)
598 return NULL;
600 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
601 return NULL;
603 oprnd0 = gimple_assign_rhs1 (last_stmt);
604 oprnd1 = gimple_assign_rhs2 (last_stmt);
605 if (!types_compatible_p (TREE_TYPE (oprnd0), type)
606 || !types_compatible_p (TREE_TYPE (oprnd1), type))
607 return NULL;
609 /* So far so good. Since last_stmt was detected as a (summation) reduction,
610 we know that oprnd1 is the reduction variable (defined by a loop-header
611 phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
612 Left to check that oprnd0 is defined by a cast from type 'type' to type
613 'TYPE'. */
615 if (!widened_name_p (oprnd0, last_stmt, &half_type, &stmt))
616 return NULL;
618 oprnd0 = gimple_assign_rhs1 (stmt);
619 *type_in = half_type;
620 *type_out = type;
622 /* Pattern detected. Create a stmt to be used to replace the pattern: */
623 var = vect_recog_temp_ssa_var (type, NULL);
624 pattern_stmt = gimple_build_assign_with_ops (WIDEN_SUM_EXPR, var,
625 oprnd0, oprnd1);
626 SSA_NAME_DEF_STMT (var) = pattern_stmt;
628 if (vect_print_dump_info (REPORT_DETAILS))
630 fprintf (vect_dump, "vect_recog_widen_sum_pattern: detected: ");
631 print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
634 /* We don't allow changing the order of the computation in the inner-loop
635 when doing outer-loop vectorization. */
636 gcc_assert (!nested_in_vect_loop_p (loop, last_stmt));
638 return pattern_stmt;
642 /* Function vect_pattern_recog_1
644 Input:
645 PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
646 computation pattern.
647 STMT: A stmt from which the pattern search should start.
649 If PATTERN_RECOG_FUNC successfully detected the pattern, it creates an
650 expression that computes the same functionality and can be used to
651 replace the sequence of stmts that are involved in the pattern.
653 Output:
654 This function checks if the expression returned by PATTERN_RECOG_FUNC is
655 supported in vector form by the target. We use 'TYPE_IN' to obtain the
656 relevant vector type. If 'TYPE_IN' is already a vector type, then this
657 indicates that target support had already been checked by PATTERN_RECOG_FUNC.
658 If 'TYPE_OUT' is also returned by PATTERN_RECOG_FUNC, we check that it fits
659 to the available target pattern.
661 This function also does some bookkeeping, as explained in the documentation
662 for vect_recog_pattern. */
664 static void
665 vect_pattern_recog_1 (
666 gimple (* vect_recog_func) (gimple, tree *, tree *),
667 gimple_stmt_iterator si)
669 gimple stmt = gsi_stmt (si), pattern_stmt;
670 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
671 stmt_vec_info pattern_stmt_info;
672 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
673 tree pattern_vectype;
674 tree type_in, type_out;
675 enum tree_code code;
677 pattern_stmt = (* vect_recog_func) (stmt, &type_in, &type_out);
678 if (!pattern_stmt)
679 return;
681 if (VECTOR_MODE_P (TYPE_MODE (type_in)))
683 /* No need to check target support (already checked by the pattern
684 recognition function). */
685 pattern_vectype = type_in;
687 else
689 enum machine_mode vec_mode;
690 enum insn_code icode;
691 optab optab;
693 /* Check target support */
694 pattern_vectype = get_vectype_for_scalar_type (type_in);
695 if (!pattern_vectype)
696 return;
698 if (is_gimple_assign (pattern_stmt))
699 code = gimple_assign_rhs_code (pattern_stmt);
700 else
702 gcc_assert (is_gimple_call (pattern_stmt));
703 code = CALL_EXPR;
706 optab = optab_for_tree_code (code, pattern_vectype, optab_default);
707 vec_mode = TYPE_MODE (pattern_vectype);
708 if (!optab
709 || (icode = optab_handler (optab, vec_mode)->insn_code) ==
710 CODE_FOR_nothing
711 || (type_out
712 && (!get_vectype_for_scalar_type (type_out)
713 || (insn_data[icode].operand[0].mode !=
714 TYPE_MODE (get_vectype_for_scalar_type (type_out))))))
715 return;
718 /* Found a vectorizable pattern. */
719 if (vect_print_dump_info (REPORT_DETAILS))
721 fprintf (vect_dump, "pattern recognized: ");
722 print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
725 /* Mark the stmts that are involved in the pattern. */
726 gsi_insert_before (&si, pattern_stmt, GSI_SAME_STMT);
727 set_vinfo_for_stmt (pattern_stmt,
728 new_stmt_vec_info (pattern_stmt, loop_vinfo, NULL));
729 pattern_stmt_info = vinfo_for_stmt (pattern_stmt);
731 STMT_VINFO_RELATED_STMT (pattern_stmt_info) = stmt;
732 STMT_VINFO_DEF_TYPE (pattern_stmt_info) = STMT_VINFO_DEF_TYPE (stmt_info);
733 STMT_VINFO_VECTYPE (pattern_stmt_info) = pattern_vectype;
734 STMT_VINFO_IN_PATTERN_P (stmt_info) = true;
735 STMT_VINFO_RELATED_STMT (stmt_info) = pattern_stmt;
737 return;
741 /* Function vect_pattern_recog
743 Input:
744 LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
745 computation idioms.
747 Output - for each computation idiom that is detected we insert a new stmt
748 that provides the same functionality and that can be vectorized. We
749 also record some information in the struct_stmt_info of the relevant
750 stmts, as explained below:
752 At the entry to this function we have the following stmts, with the
753 following initial value in the STMT_VINFO fields:
755 stmt in_pattern_p related_stmt vec_stmt
756 S1: a_i = .... - - -
757 S2: a_2 = ..use(a_i).. - - -
758 S3: a_1 = ..use(a_2).. - - -
759 S4: a_0 = ..use(a_1).. - - -
760 S5: ... = ..use(a_0).. - - -
762 Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
763 represented by a single stmt. We then:
764 - create a new stmt S6 that will replace the pattern.
765 - insert the new stmt S6 before the last stmt in the pattern
766 - fill in the STMT_VINFO fields as follows:
768 in_pattern_p related_stmt vec_stmt
769 S1: a_i = .... - - -
770 S2: a_2 = ..use(a_i).. - - -
771 S3: a_1 = ..use(a_2).. - - -
772 > S6: a_new = .... - S4 -
773 S4: a_0 = ..use(a_1).. true S6 -
774 S5: ... = ..use(a_0).. - - -
776 (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
777 to each other through the RELATED_STMT field).
779 S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
780 of S4 because it will replace all its uses. Stmts {S1,S2,S3} will
781 remain irrelevant unless used by stmts other than S4.
783 If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
784 (because they are marked as irrelevant). It will vectorize S6, and record
785 a pointer to the new vector stmt VS6 both from S6 (as usual), and also
786 from S4. We do that so that when we get to vectorizing stmts that use the
787 def of S4 (like S5 that uses a_0), we'll know where to take the relevant
788 vector-def from. S4 will be skipped, and S5 will be vectorized as usual:
790 in_pattern_p related_stmt vec_stmt
791 S1: a_i = .... - - -
792 S2: a_2 = ..use(a_i).. - - -
793 S3: a_1 = ..use(a_2).. - - -
794 > VS6: va_new = .... - - -
795 S6: a_new = .... - S4 VS6
796 S4: a_0 = ..use(a_1).. true S6 VS6
797 > VS5: ... = ..vuse(va_new).. - - -
798 S5: ... = ..use(a_0).. - - -
800 DCE could then get rid of {S1,S2,S3,S4,S5,S6} (if their defs are not used
801 elsewhere), and we'll end up with:
803 VS6: va_new = ....
804 VS5: ... = ..vuse(va_new)..
806 If vectorization does not succeed, DCE will clean S6 away (its def is
807 not used), and we'll end up with the original sequence.
810 void
811 vect_pattern_recog (loop_vec_info loop_vinfo)
813 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
814 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
815 unsigned int nbbs = loop->num_nodes;
816 gimple_stmt_iterator si;
817 gimple stmt;
818 unsigned int i, j;
819 gimple (* vect_recog_func_ptr) (gimple, tree *, tree *);
821 if (vect_print_dump_info (REPORT_DETAILS))
822 fprintf (vect_dump, "=== vect_pattern_recog ===");
824 /* Scan through the loop stmts, applying the pattern recognition
825 functions starting at each stmt visited: */
826 for (i = 0; i < nbbs; i++)
828 basic_block bb = bbs[i];
829 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
831 stmt = gsi_stmt (si);
833 /* Scan over all generic vect_recog_xxx_pattern functions. */
834 for (j = 0; j < NUM_PATTERNS; j++)
836 vect_recog_func_ptr = vect_vect_recog_func_ptrs[j];
837 vect_pattern_recog_1 (vect_recog_func_ptr, si);