OSDN Git Service

* target.h (struct calls): Add function_value_regno_p field.
[pf3gnuchains/gcc-fork.git] / gcc / tree-vect-patterns.c
1 /* Analysis Utilities for Loop Vectorization.
2    Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3    Contributed by Dorit Nuzman <dorit@il.ibm.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "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 "gimple-pretty-print.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "cfgloop.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "params.h"
37 #include "tree-data-ref.h"
38 #include "tree-vectorizer.h"
39 #include "recog.h"
40 #include "toplev.h"
41
42 /* Function prototypes */
43 static void vect_pattern_recog_1
44   (gimple (* ) (gimple, tree *, tree *), gimple_stmt_iterator);
45 static bool widened_name_p (tree, gimple, tree *, gimple *);
46
47 /* Pattern recognition functions  */
48 static gimple vect_recog_widen_sum_pattern (gimple, tree *, tree *);
49 static gimple vect_recog_widen_mult_pattern (gimple, tree *, tree *);
50 static gimple vect_recog_dot_prod_pattern (gimple, tree *, tree *);
51 static gimple vect_recog_pow_pattern (gimple, tree *, tree *);
52 static vect_recog_func_ptr vect_vect_recog_func_ptrs[NUM_PATTERNS] = {
53         vect_recog_widen_mult_pattern,
54         vect_recog_widen_sum_pattern,
55         vect_recog_dot_prod_pattern,
56         vect_recog_pow_pattern};
57
58
59 /* Function widened_name_p
60
61    Check whether NAME, an ssa-name used in USE_STMT,
62    is a result of a type-promotion, such that:
63      DEF_STMT: NAME = NOP (name0)
64    where the type of name0 (HALF_TYPE) is smaller than the type of NAME.
65 */
66
67 static bool
68 widened_name_p (tree name, gimple use_stmt, tree *half_type, gimple *def_stmt)
69 {
70   tree dummy;
71   gimple dummy_gimple;
72   loop_vec_info loop_vinfo;
73   stmt_vec_info stmt_vinfo;
74   tree type = TREE_TYPE (name);
75   tree oprnd0;
76   enum vect_def_type dt;
77   tree def;
78
79   stmt_vinfo = vinfo_for_stmt (use_stmt);
80   loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
81
82   if (!vect_is_simple_use (name, loop_vinfo, NULL, def_stmt, &def, &dt))
83     return false;
84
85   if (dt != vect_internal_def
86       && dt != vect_external_def && dt != vect_constant_def)
87     return false;
88
89   if (! *def_stmt)
90     return false;
91
92   if (!is_gimple_assign (*def_stmt))
93     return false;
94
95   if (gimple_assign_rhs_code (*def_stmt) != NOP_EXPR)
96     return false;
97
98   oprnd0 = gimple_assign_rhs1 (*def_stmt);
99
100   *half_type = TREE_TYPE (oprnd0);
101   if (!INTEGRAL_TYPE_P (type) || !INTEGRAL_TYPE_P (*half_type)
102       || (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (*half_type))
103       || (TYPE_PRECISION (type) < (TYPE_PRECISION (*half_type) * 2)))
104     return false;
105
106   if (!vect_is_simple_use (oprnd0, loop_vinfo, NULL, &dummy_gimple, &dummy,
107                            &dt))
108     return false;
109
110   return true;
111 }
112
113 /* Helper to return a new temporary for pattern of TYPE for STMT.  If STMT
114    is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */
115
116 static tree
117 vect_recog_temp_ssa_var (tree type, gimple stmt)
118 {
119   tree var = create_tmp_var (type, "patt");
120
121   add_referenced_var (var);
122   var = make_ssa_name (var, stmt);
123   return var;
124 }
125
126 /* Function vect_recog_dot_prod_pattern
127
128    Try to find the following pattern:
129
130      type x_t, y_t;
131      TYPE1 prod;
132      TYPE2 sum = init;
133    loop:
134      sum_0 = phi <init, sum_1>
135      S1  x_t = ...
136      S2  y_t = ...
137      S3  x_T = (TYPE1) x_t;
138      S4  y_T = (TYPE1) y_t;
139      S5  prod = x_T * y_T;
140      [S6  prod = (TYPE2) prod;  #optional]
141      S7  sum_1 = prod + sum_0;
142
143    where 'TYPE1' is exactly double the size of type 'type', and 'TYPE2' is the
144    same size of 'TYPE1' or bigger. This is a special case of a reduction
145    computation.
146
147    Input:
148
149    * LAST_STMT: A stmt from which the pattern search begins. In the example,
150    when this function is called with S7, the pattern {S3,S4,S5,S6,S7} will be
151    detected.
152
153    Output:
154
155    * TYPE_IN: The type of the input arguments to the pattern.
156
157    * TYPE_OUT: The type of the output  of this pattern.
158
159    * Return value: A new stmt that will be used to replace the sequence of
160    stmts that constitute the pattern. In this case it will be:
161         WIDEN_DOT_PRODUCT <x_t, y_t, sum_0>
162
163    Note: The dot-prod idiom is a widening reduction pattern that is
164          vectorized without preserving all the intermediate results. It
165          produces only N/2 (widened) results (by summing up pairs of
166          intermediate results) rather than all N results.  Therefore, we
167          cannot allow this pattern when we want to get all the results and in
168          the correct order (as is the case when this computation is in an
169          inner-loop nested in an outer-loop that us being vectorized).  */
170
171 static gimple
172 vect_recog_dot_prod_pattern (gimple last_stmt, tree *type_in, tree *type_out)
173 {
174   gimple stmt;
175   tree oprnd0, oprnd1;
176   tree oprnd00, oprnd01;
177   stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
178   tree type, half_type;
179   gimple pattern_stmt;
180   tree prod_type;
181   loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
182   struct loop *loop = LOOP_VINFO_LOOP (loop_info);
183   tree var, rhs;
184
185   if (!is_gimple_assign (last_stmt))
186     return NULL;
187
188   type = gimple_expr_type (last_stmt);
189
190   /* Look for the following pattern
191           DX = (TYPE1) X;
192           DY = (TYPE1) Y;
193           DPROD = DX * DY;
194           DDPROD = (TYPE2) DPROD;
195           sum_1 = DDPROD + sum_0;
196      In which
197      - DX is double the size of X
198      - DY is double the size of Y
199      - DX, DY, DPROD all have the same type
200      - sum is the same size of DPROD or bigger
201      - sum has been recognized as a reduction variable.
202
203      This is equivalent to:
204        DPROD = X w* Y;          #widen mult
205        sum_1 = DPROD w+ sum_0;  #widen summation
206      or
207        DPROD = X w* Y;          #widen mult
208        sum_1 = DPROD + sum_0;   #summation
209    */
210
211   /* Starting from LAST_STMT, follow the defs of its uses in search
212      of the above pattern.  */
213
214   if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR)
215     return NULL;
216
217   if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
218     {
219       /* Has been detected as widening-summation?  */
220
221       stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
222       type = gimple_expr_type (stmt);
223       if (gimple_assign_rhs_code (stmt) != WIDEN_SUM_EXPR)
224         return NULL;
225       oprnd0 = gimple_assign_rhs1 (stmt);
226       oprnd1 = gimple_assign_rhs2 (stmt);
227       half_type = TREE_TYPE (oprnd0);
228     }
229   else
230     {
231       gimple def_stmt;
232
233       if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
234         return NULL;
235       oprnd0 = gimple_assign_rhs1 (last_stmt);
236       oprnd1 = gimple_assign_rhs2 (last_stmt);
237       if (!types_compatible_p (TREE_TYPE (oprnd0), type)
238           || !types_compatible_p (TREE_TYPE (oprnd1), type))
239         return NULL;
240       stmt = last_stmt;
241
242       if (widened_name_p (oprnd0, stmt, &half_type, &def_stmt))
243         {
244           stmt = def_stmt;
245           oprnd0 = gimple_assign_rhs1 (stmt);
246         }
247       else
248         half_type = type;
249     }
250
251   /* So far so good. Since last_stmt was detected as a (summation) reduction,
252      we know that oprnd1 is the reduction variable (defined by a loop-header
253      phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
254      Left to check that oprnd0 is defined by a (widen_)mult_expr  */
255
256   prod_type = half_type;
257   stmt = SSA_NAME_DEF_STMT (oprnd0);
258   /* FORNOW.  Can continue analyzing the def-use chain when this stmt in a phi
259      inside the loop (in case we are analyzing an outer-loop).  */
260   if (!is_gimple_assign (stmt))
261     return NULL;
262   stmt_vinfo = vinfo_for_stmt (stmt);
263   gcc_assert (stmt_vinfo);
264   if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_internal_def)
265     return NULL;
266   if (gimple_assign_rhs_code (stmt) != MULT_EXPR)
267     return NULL;
268   if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
269     {
270       /* Has been detected as a widening multiplication?  */
271
272       stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
273       if (gimple_assign_rhs_code (stmt) != WIDEN_MULT_EXPR)
274         return NULL;
275       stmt_vinfo = vinfo_for_stmt (stmt);
276       gcc_assert (stmt_vinfo);
277       gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_internal_def);
278       oprnd00 = gimple_assign_rhs1 (stmt);
279       oprnd01 = gimple_assign_rhs2 (stmt);
280     }
281   else
282     {
283       tree half_type0, half_type1;
284       gimple def_stmt;
285       tree oprnd0, oprnd1;
286
287       oprnd0 = gimple_assign_rhs1 (stmt);
288       oprnd1 = gimple_assign_rhs2 (stmt);
289       if (!types_compatible_p (TREE_TYPE (oprnd0), prod_type)
290           || !types_compatible_p (TREE_TYPE (oprnd1), prod_type))
291         return NULL;
292       if (!widened_name_p (oprnd0, stmt, &half_type0, &def_stmt))
293         return NULL;
294       oprnd00 = gimple_assign_rhs1 (def_stmt);
295       if (!widened_name_p (oprnd1, stmt, &half_type1, &def_stmt))
296         return NULL;
297       oprnd01 = gimple_assign_rhs1 (def_stmt);
298       if (!types_compatible_p (half_type0, half_type1))
299         return NULL;
300       if (TYPE_PRECISION (prod_type) != TYPE_PRECISION (half_type0) * 2)
301         return NULL;
302     }
303
304   half_type = TREE_TYPE (oprnd00);
305   *type_in = half_type;
306   *type_out = type;
307
308   /* Pattern detected. Create a stmt to be used to replace the pattern: */
309   var = vect_recog_temp_ssa_var (type, NULL);
310   rhs = build3 (DOT_PROD_EXPR, type, oprnd00, oprnd01, oprnd1),
311   pattern_stmt = gimple_build_assign (var, rhs);
312
313   if (vect_print_dump_info (REPORT_DETAILS))
314     {
315       fprintf (vect_dump, "vect_recog_dot_prod_pattern: detected: ");
316       print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
317     }
318
319   /* We don't allow changing the order of the computation in the inner-loop
320      when doing outer-loop vectorization.  */
321   gcc_assert (!nested_in_vect_loop_p (loop, last_stmt));
322
323   return pattern_stmt;
324 }
325
326 /* Function vect_recog_widen_mult_pattern
327
328    Try to find the following pattern:
329
330      type a_t, b_t;
331      TYPE a_T, b_T, prod_T;
332
333      S1  a_t = ;
334      S2  b_t = ;
335      S3  a_T = (TYPE) a_t;
336      S4  b_T = (TYPE) b_t;
337      S5  prod_T = a_T * b_T;
338
339    where type 'TYPE' is at least double the size of type 'type'.
340
341    Input:
342
343    * LAST_STMT: A stmt from which the pattern search begins. In the example,
344    when this function is called with S5, the pattern {S3,S4,S5} is be detected.
345
346    Output:
347
348    * TYPE_IN: The type of the input arguments to the pattern.
349
350    * TYPE_OUT: The type of the output  of this pattern.
351
352    * Return value: A new stmt that will be used to replace the sequence of
353    stmts that constitute the pattern. In this case it will be:
354         WIDEN_MULT <a_t, b_t>
355 */
356
357 static gimple
358 vect_recog_widen_mult_pattern (gimple last_stmt,
359                                tree *type_in,
360                                tree *type_out)
361 {
362   gimple def_stmt0, def_stmt1;
363   tree oprnd0, oprnd1;
364   tree type, half_type0, half_type1;
365   gimple pattern_stmt;
366   tree vectype, vectype_out;
367   tree dummy;
368   tree var;
369   enum tree_code dummy_code;
370   int dummy_int;
371   VEC (tree, heap) *dummy_vec;
372
373   if (!is_gimple_assign (last_stmt))
374     return NULL;
375
376   type = gimple_expr_type (last_stmt);
377
378   /* Starting from LAST_STMT, follow the defs of its uses in search
379      of the above pattern.  */
380
381   if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR)
382     return NULL;
383
384   oprnd0 = gimple_assign_rhs1 (last_stmt);
385   oprnd1 = gimple_assign_rhs2 (last_stmt);
386   if (!types_compatible_p (TREE_TYPE (oprnd0), type)
387       || !types_compatible_p (TREE_TYPE (oprnd1), type))
388     return NULL;
389
390   /* Check argument 0 */
391   if (!widened_name_p (oprnd0, last_stmt, &half_type0, &def_stmt0))
392     return NULL;
393   oprnd0 = gimple_assign_rhs1 (def_stmt0);
394
395   /* Check argument 1 */
396   if (!widened_name_p (oprnd1, last_stmt, &half_type1, &def_stmt1))
397     return NULL;
398   oprnd1 = gimple_assign_rhs1 (def_stmt1);
399
400   if (!types_compatible_p (half_type0, half_type1))
401     return NULL;
402
403   /* Pattern detected.  */
404   if (vect_print_dump_info (REPORT_DETAILS))
405     fprintf (vect_dump, "vect_recog_widen_mult_pattern: detected: ");
406
407   /* Check target support  */
408   vectype = get_vectype_for_scalar_type (half_type0);
409   vectype_out = get_vectype_for_scalar_type (type);
410   if (!vectype
411       || !supportable_widening_operation (WIDEN_MULT_EXPR, last_stmt,
412                                           vectype_out, vectype,
413                                           &dummy, &dummy, &dummy_code,
414                                           &dummy_code, &dummy_int, &dummy_vec))
415     return NULL;
416
417   *type_in = vectype;
418   *type_out = vectype_out;
419
420   /* Pattern supported. Create a stmt to be used to replace the pattern: */
421   var = vect_recog_temp_ssa_var (type, NULL);
422   pattern_stmt = gimple_build_assign_with_ops (WIDEN_MULT_EXPR, var, oprnd0,
423                                                oprnd1);
424   SSA_NAME_DEF_STMT (var) = pattern_stmt;
425
426   if (vect_print_dump_info (REPORT_DETAILS))
427     print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
428
429   return pattern_stmt;
430 }
431
432
433 /* Function vect_recog_pow_pattern
434
435    Try to find the following pattern:
436
437      x = POW (y, N);
438
439    with POW being one of pow, powf, powi, powif and N being
440    either 2 or 0.5.
441
442    Input:
443
444    * LAST_STMT: A stmt from which the pattern search begins.
445
446    Output:
447
448    * TYPE_IN: The type of the input arguments to the pattern.
449
450    * TYPE_OUT: The type of the output of this pattern.
451
452    * Return value: A new stmt that will be used to replace the sequence of
453    stmts that constitute the pattern. In this case it will be:
454         x = x * x
455    or
456         x = sqrt (x)
457 */
458
459 static gimple
460 vect_recog_pow_pattern (gimple last_stmt, tree *type_in, tree *type_out)
461 {
462   tree fn, base, exp = NULL;
463   gimple stmt;
464   tree var;
465
466   if (!is_gimple_call (last_stmt) || gimple_call_lhs (last_stmt) == NULL)
467     return NULL;
468
469   fn = gimple_call_fndecl (last_stmt);
470   switch (DECL_FUNCTION_CODE (fn))
471     {
472     case BUILT_IN_POWIF:
473     case BUILT_IN_POWI:
474     case BUILT_IN_POWF:
475     case BUILT_IN_POW:
476       base = gimple_call_arg (last_stmt, 0);
477       exp = gimple_call_arg (last_stmt, 1);
478       if (TREE_CODE (exp) != REAL_CST
479           && TREE_CODE (exp) != INTEGER_CST)
480         return NULL;
481       break;
482
483     default:
484       return NULL;
485     }
486
487   /* We now have a pow or powi builtin function call with a constant
488      exponent.  */
489
490   *type_out = NULL_TREE;
491
492   /* Catch squaring.  */
493   if ((host_integerp (exp, 0)
494        && tree_low_cst (exp, 0) == 2)
495       || (TREE_CODE (exp) == REAL_CST
496           && REAL_VALUES_EQUAL (TREE_REAL_CST (exp), dconst2)))
497     {
498       *type_in = TREE_TYPE (base);
499
500       var = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
501       stmt = gimple_build_assign_with_ops (MULT_EXPR, var, base, base);
502       SSA_NAME_DEF_STMT (var) = stmt;
503       return stmt;
504     }
505
506   /* Catch square root.  */
507   if (TREE_CODE (exp) == REAL_CST
508       && REAL_VALUES_EQUAL (TREE_REAL_CST (exp), dconsthalf))
509     {
510       tree newfn = mathfn_built_in (TREE_TYPE (base), BUILT_IN_SQRT);
511       *type_in = get_vectype_for_scalar_type (TREE_TYPE (base));
512       if (*type_in)
513         {
514           gimple stmt = gimple_build_call (newfn, 1, base);
515           if (vectorizable_function (stmt, *type_in, *type_in)
516               != NULL_TREE)
517             {
518               var = vect_recog_temp_ssa_var (TREE_TYPE (base), stmt);
519               gimple_call_set_lhs (stmt, var);
520               return stmt;
521             }
522         }
523     }
524
525   return NULL;
526 }
527
528
529 /* Function vect_recog_widen_sum_pattern
530
531    Try to find the following pattern:
532
533      type x_t;
534      TYPE x_T, sum = init;
535    loop:
536      sum_0 = phi <init, sum_1>
537      S1  x_t = *p;
538      S2  x_T = (TYPE) x_t;
539      S3  sum_1 = x_T + sum_0;
540
541    where type 'TYPE' is at least double the size of type 'type', i.e - we're
542    summing elements of type 'type' into an accumulator of type 'TYPE'. This is
543    a special case of a reduction computation.
544
545    Input:
546
547    * LAST_STMT: A stmt from which the pattern search begins. In the example,
548    when this function is called with S3, the pattern {S2,S3} will be detected.
549
550    Output:
551
552    * TYPE_IN: The type of the input arguments to the pattern.
553
554    * TYPE_OUT: The type of the output of this pattern.
555
556    * Return value: A new stmt that will be used to replace the sequence of
557    stmts that constitute the pattern. In this case it will be:
558         WIDEN_SUM <x_t, sum_0>
559
560    Note: The widening-sum idiom is a widening reduction pattern that is
561          vectorized without preserving all the intermediate results. It
562          produces only N/2 (widened) results (by summing up pairs of
563          intermediate results) rather than all N results.  Therefore, we
564          cannot allow this pattern when we want to get all the results and in
565          the correct order (as is the case when this computation is in an
566          inner-loop nested in an outer-loop that us being vectorized).  */
567
568 static gimple
569 vect_recog_widen_sum_pattern (gimple last_stmt, tree *type_in, tree *type_out)
570 {
571   gimple stmt;
572   tree oprnd0, oprnd1;
573   stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
574   tree type, half_type;
575   gimple pattern_stmt;
576   loop_vec_info loop_info = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
577   struct loop *loop = LOOP_VINFO_LOOP (loop_info);
578   tree var;
579
580   if (!is_gimple_assign (last_stmt))
581     return NULL;
582
583   type = gimple_expr_type (last_stmt);
584
585   /* Look for the following pattern
586           DX = (TYPE) X;
587           sum_1 = DX + sum_0;
588      In which DX is at least double the size of X, and sum_1 has been
589      recognized as a reduction variable.
590    */
591
592   /* Starting from LAST_STMT, follow the defs of its uses in search
593      of the above pattern.  */
594
595   if (gimple_assign_rhs_code (last_stmt) != PLUS_EXPR)
596     return NULL;
597
598   if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
599     return NULL;
600
601   oprnd0 = gimple_assign_rhs1 (last_stmt);
602   oprnd1 = gimple_assign_rhs2 (last_stmt);
603   if (!types_compatible_p (TREE_TYPE (oprnd0), type)
604       || !types_compatible_p (TREE_TYPE (oprnd1), type))
605     return NULL;
606
607   /* So far so good. Since last_stmt was detected as a (summation) reduction,
608      we know that oprnd1 is the reduction variable (defined by a loop-header
609      phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
610      Left to check that oprnd0 is defined by a cast from type 'type' to type
611      'TYPE'.  */
612
613   if (!widened_name_p (oprnd0, last_stmt, &half_type, &stmt))
614     return NULL;
615
616   oprnd0 = gimple_assign_rhs1 (stmt);
617   *type_in = half_type;
618   *type_out = type;
619
620   /* Pattern detected. Create a stmt to be used to replace the pattern: */
621   var = vect_recog_temp_ssa_var (type, NULL);
622   pattern_stmt = gimple_build_assign_with_ops (WIDEN_SUM_EXPR, var,
623                                                oprnd0, oprnd1);
624   SSA_NAME_DEF_STMT (var) = pattern_stmt;
625
626   if (vect_print_dump_info (REPORT_DETAILS))
627     {
628       fprintf (vect_dump, "vect_recog_widen_sum_pattern: detected: ");
629       print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
630     }
631
632   /* We don't allow changing the order of the computation in the inner-loop
633      when doing outer-loop vectorization.  */
634   gcc_assert (!nested_in_vect_loop_p (loop, last_stmt));
635
636   return pattern_stmt;
637 }
638
639
640 /* Function vect_pattern_recog_1
641
642    Input:
643    PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
644         computation pattern.
645    STMT: A stmt from which the pattern search should start.
646
647    If PATTERN_RECOG_FUNC successfully detected the pattern, it creates an
648    expression that computes the same functionality and can be used to
649    replace the sequence of stmts that are involved in the pattern.
650
651    Output:
652    This function checks if the expression returned by PATTERN_RECOG_FUNC is
653    supported in vector form by the target.  We use 'TYPE_IN' to obtain the
654    relevant vector type. If 'TYPE_IN' is already a vector type, then this
655    indicates that target support had already been checked by PATTERN_RECOG_FUNC.
656    If 'TYPE_OUT' is also returned by PATTERN_RECOG_FUNC, we check that it fits
657    to the available target pattern.
658
659    This function also does some bookkeeping, as explained in the documentation
660    for vect_recog_pattern.  */
661
662 static void
663 vect_pattern_recog_1 (
664         gimple (* vect_recog_func) (gimple, tree *, tree *),
665         gimple_stmt_iterator si)
666 {
667   gimple stmt = gsi_stmt (si), pattern_stmt;
668   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
669   stmt_vec_info pattern_stmt_info;
670   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
671   tree pattern_vectype;
672   tree type_in, type_out;
673   enum tree_code code;
674   int i;
675   gimple next;
676
677   pattern_stmt = (* vect_recog_func) (stmt, &type_in, &type_out);
678   if (!pattern_stmt)
679     return;
680
681   if (VECTOR_MODE_P (TYPE_MODE (type_in)))
682     {
683       /* No need to check target support (already checked by the pattern
684          recognition function).  */
685       if (type_out)
686         gcc_assert (VECTOR_MODE_P (TYPE_MODE (type_out)));
687       pattern_vectype = type_out ? type_out : type_in;
688     }
689   else
690     {
691       enum machine_mode vec_mode;
692       enum insn_code icode;
693       optab optab;
694
695       /* Check target support  */
696       type_in = get_vectype_for_scalar_type (type_in);
697       if (!type_in)
698         return;
699       if (type_out)
700         type_out = get_vectype_for_scalar_type (type_out);
701       else
702         type_out = type_in;
703       pattern_vectype = type_out;
704
705       if (is_gimple_assign (pattern_stmt))
706         code = gimple_assign_rhs_code (pattern_stmt);
707       else
708         {
709           gcc_assert (is_gimple_call (pattern_stmt));
710           code = CALL_EXPR;
711         }
712
713       optab = optab_for_tree_code (code, type_in, optab_default);
714       vec_mode = TYPE_MODE (type_in);
715       if (!optab
716           || (icode = optab_handler (optab, vec_mode)->insn_code) ==
717               CODE_FOR_nothing
718           || (insn_data[icode].operand[0].mode != TYPE_MODE (type_out)))
719         return;
720     }
721
722   /* Found a vectorizable pattern.  */
723   if (vect_print_dump_info (REPORT_DETAILS))
724     {
725       fprintf (vect_dump, "pattern recognized: ");
726       print_gimple_stmt (vect_dump, pattern_stmt, 0, TDF_SLIM);
727     }
728
729   /* Mark the stmts that are involved in the pattern. */
730   gsi_insert_before (&si, pattern_stmt, GSI_SAME_STMT);
731   set_vinfo_for_stmt (pattern_stmt,
732                       new_stmt_vec_info (pattern_stmt, loop_vinfo, NULL));
733   pattern_stmt_info = vinfo_for_stmt (pattern_stmt);
734
735   STMT_VINFO_RELATED_STMT (pattern_stmt_info) = stmt;
736   STMT_VINFO_DEF_TYPE (pattern_stmt_info) = STMT_VINFO_DEF_TYPE (stmt_info);
737   STMT_VINFO_VECTYPE (pattern_stmt_info) = pattern_vectype;
738   STMT_VINFO_IN_PATTERN_P (stmt_info) = true;
739   STMT_VINFO_RELATED_STMT (stmt_info) = pattern_stmt;
740
741   /* Patterns cannot be vectorized using SLP, because they change the order of
742      computation.  */
743   for (i = 0; VEC_iterate (gimple, LOOP_VINFO_REDUCTIONS (loop_vinfo), i,
744                            next);
745        i++)
746     if (next == stmt)
747       VEC_ordered_remove (gimple, LOOP_VINFO_REDUCTIONS (loop_vinfo), i); 
748 }
749
750
751 /* Function vect_pattern_recog
752
753    Input:
754    LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
755         computation idioms.
756
757    Output - for each computation idiom that is detected we insert a new stmt
758         that provides the same functionality and that can be vectorized. We
759         also record some information in the struct_stmt_info of the relevant
760         stmts, as explained below:
761
762    At the entry to this function we have the following stmts, with the
763    following initial value in the STMT_VINFO fields:
764
765          stmt                     in_pattern_p  related_stmt    vec_stmt
766          S1: a_i = ....                 -       -               -
767          S2: a_2 = ..use(a_i)..         -       -               -
768          S3: a_1 = ..use(a_2)..         -       -               -
769          S4: a_0 = ..use(a_1)..         -       -               -
770          S5: ... = ..use(a_0)..         -       -               -
771
772    Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
773    represented by a single stmt. We then:
774    - create a new stmt S6 that will replace the pattern.
775    - insert the new stmt S6 before the last stmt in the pattern
776    - fill in the STMT_VINFO fields as follows:
777
778                                   in_pattern_p  related_stmt    vec_stmt
779          S1: a_i = ....                 -       -               -
780          S2: a_2 = ..use(a_i)..         -       -               -
781          S3: a_1 = ..use(a_2)..         -       -               -
782        > S6: a_new = ....               -       S4              -
783          S4: a_0 = ..use(a_1)..         true    S6              -
784          S5: ... = ..use(a_0)..         -       -               -
785
786    (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
787     to each other through the RELATED_STMT field).
788
789    S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
790    of S4 because it will replace all its uses.  Stmts {S1,S2,S3} will
791    remain irrelevant unless used by stmts other than S4.
792
793    If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
794    (because they are marked as irrelevant). It will vectorize S6, and record
795    a pointer to the new vector stmt VS6 both from S6 (as usual), and also
796    from S4. We do that so that when we get to vectorizing stmts that use the
797    def of S4 (like S5 that uses a_0), we'll know where to take the relevant
798    vector-def from. S4 will be skipped, and S5 will be vectorized as usual:
799
800                                   in_pattern_p  related_stmt    vec_stmt
801          S1: a_i = ....                 -       -               -
802          S2: a_2 = ..use(a_i)..         -       -               -
803          S3: a_1 = ..use(a_2)..         -       -               -
804        > VS6: va_new = ....             -       -               -
805          S6: a_new = ....               -       S4              VS6
806          S4: a_0 = ..use(a_1)..         true    S6              VS6
807        > VS5: ... = ..vuse(va_new)..    -       -               -
808          S5: ... = ..use(a_0)..         -       -               -
809
810    DCE could then get rid of {S1,S2,S3,S4,S5,S6} (if their defs are not used
811    elsewhere), and we'll end up with:
812
813         VS6: va_new = ....
814         VS5: ... = ..vuse(va_new)..
815
816    If vectorization does not succeed, DCE will clean S6 away (its def is
817    not used), and we'll end up with the original sequence.
818 */
819
820 void
821 vect_pattern_recog (loop_vec_info loop_vinfo)
822 {
823   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
824   basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
825   unsigned int nbbs = loop->num_nodes;
826   gimple_stmt_iterator si;
827   unsigned int i, j;
828   gimple (* vect_recog_func_ptr) (gimple, tree *, tree *);
829
830   if (vect_print_dump_info (REPORT_DETAILS))
831     fprintf (vect_dump, "=== vect_pattern_recog ===");
832
833   /* Scan through the loop stmts, applying the pattern recognition
834      functions starting at each stmt visited:  */
835   for (i = 0; i < nbbs; i++)
836     {
837       basic_block bb = bbs[i];
838       for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
839         {
840           /* Scan over all generic vect_recog_xxx_pattern functions.  */
841           for (j = 0; j < NUM_PATTERNS; j++)
842             {
843               vect_recog_func_ptr = vect_vect_recog_func_ptrs[j];
844               vect_pattern_recog_1 (vect_recog_func_ptr, si);
845             }
846         }
847     }
848 }