00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024 #include "coretypes.h"
00025 #include "tm.h"
00026 #include "ggc.h"
00027 #include "tree.h"
00028
00029 #include "target.h"
00030 #include "basic-block.h"
00031 #include "diagnostic.h"
00032 #include "tree-flow.h"
00033 #include "tree-dump.h"
00034 #include "timevar.h"
00035 #include "cfgloop.h"
00036 #include "expr.h"
00037 #include "optabs.h"
00038 #include "params.h"
00039 #include "tree-data-ref.h"
00040 #include "tree-vectorizer.h"
00041 #include "recog.h"
00042 #include "toplev.h"
00043
00044
00045 static void vect_pattern_recog_1
00046 (tree (* ) (tree, tree *, tree *), block_stmt_iterator);
00047 static bool widened_name_p (tree, tree, tree *, tree *);
00048
00049
00050 static tree vect_recog_widen_sum_pattern (tree, tree *, tree *);
00051 static tree vect_recog_widen_mult_pattern (tree, tree *, tree *);
00052 static tree vect_recog_dot_prod_pattern (tree, tree *, tree *);
00053 static vect_recog_func_ptr vect_vect_recog_func_ptrs[NUM_PATTERNS] = {
00054 vect_recog_widen_mult_pattern,
00055 vect_recog_widen_sum_pattern,
00056 vect_recog_dot_prod_pattern};
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 static bool
00068 widened_name_p (tree name, tree use_stmt, tree *half_type, tree *def_stmt)
00069 {
00070 tree dummy;
00071 loop_vec_info loop_vinfo;
00072 stmt_vec_info stmt_vinfo;
00073 tree expr;
00074 tree type = TREE_TYPE (name);
00075 tree oprnd0;
00076 enum vect_def_type dt;
00077 tree def;
00078
00079 stmt_vinfo = vinfo_for_stmt (use_stmt);
00080 loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
00081
00082 if (!vect_is_simple_use (name, loop_vinfo, def_stmt, &def, &dt))
00083 return false;
00084
00085 if (dt != vect_loop_def
00086 && dt != vect_invariant_def && dt != vect_constant_def)
00087 return false;
00088
00089 if (! *def_stmt)
00090 return false;
00091
00092 if (TREE_CODE (*def_stmt) != MODIFY_EXPR)
00093 return false;
00094
00095 expr = TREE_OPERAND (*def_stmt, 1);
00096 if (TREE_CODE (expr) != NOP_EXPR)
00097 return false;
00098
00099 oprnd0 = TREE_OPERAND (expr, 0);
00100
00101 *half_type = TREE_TYPE (oprnd0);
00102 if (!INTEGRAL_TYPE_P (type) || !INTEGRAL_TYPE_P (*half_type)
00103 || (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (*half_type))
00104 || (TYPE_PRECISION (type) < (TYPE_PRECISION (*half_type) * 2)))
00105 return false;
00106
00107 if (!vect_is_simple_use (oprnd0, loop_vinfo, &dummy, &dummy, &dt))
00108 return false;
00109
00110 if (dt != vect_invariant_def && dt != vect_constant_def
00111 && dt != vect_loop_def)
00112 return false;
00113
00114 return true;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 static tree
00157 vect_recog_dot_prod_pattern (tree last_stmt, tree *type_in, tree *type_out)
00158 {
00159 tree stmt, expr;
00160 tree oprnd0, oprnd1;
00161 tree oprnd00, oprnd01;
00162 stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
00163 tree type, half_type;
00164 tree pattern_expr;
00165 tree prod_type;
00166
00167 if (TREE_CODE (last_stmt) != MODIFY_EXPR)
00168 return NULL;
00169
00170 expr = TREE_OPERAND (last_stmt, 1);
00171 type = TREE_TYPE (expr);
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 if (TREE_CODE (expr) != PLUS_EXPR)
00198 return NULL;
00199
00200 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
00201 {
00202
00203
00204 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
00205 expr = TREE_OPERAND (stmt, 1);
00206 type = TREE_TYPE (expr);
00207 if (TREE_CODE (expr) != WIDEN_SUM_EXPR)
00208 return NULL;
00209 oprnd0 = TREE_OPERAND (expr, 0);
00210 oprnd1 = TREE_OPERAND (expr, 1);
00211 half_type = TREE_TYPE (oprnd0);
00212 }
00213 else
00214 {
00215 tree def_stmt;
00216
00217 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
00218 return NULL;
00219 oprnd0 = TREE_OPERAND (expr, 0);
00220 oprnd1 = TREE_OPERAND (expr, 1);
00221 if (TYPE_MAIN_VARIANT (TREE_TYPE (oprnd0)) != TYPE_MAIN_VARIANT (type)
00222 || TYPE_MAIN_VARIANT (TREE_TYPE (oprnd1)) != TYPE_MAIN_VARIANT (type))
00223 return NULL;
00224 stmt = last_stmt;
00225
00226 if (widened_name_p (oprnd0, stmt, &half_type, &def_stmt))
00227 {
00228 stmt = def_stmt;
00229 expr = TREE_OPERAND (stmt, 1);
00230 oprnd0 = TREE_OPERAND (expr, 0);
00231 }
00232 else
00233 half_type = type;
00234 }
00235
00236
00237
00238
00239
00240
00241 prod_type = half_type;
00242 stmt = SSA_NAME_DEF_STMT (oprnd0);
00243 gcc_assert (stmt);
00244 stmt_vinfo = vinfo_for_stmt (stmt);
00245 gcc_assert (stmt_vinfo);
00246 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_loop_def)
00247 return NULL;
00248 expr = TREE_OPERAND (stmt, 1);
00249 if (TREE_CODE (expr) != MULT_EXPR)
00250 return NULL;
00251 if (STMT_VINFO_IN_PATTERN_P (stmt_vinfo))
00252 {
00253
00254
00255 stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
00256 expr = TREE_OPERAND (stmt, 1);
00257 if (TREE_CODE (expr) != WIDEN_MULT_EXPR)
00258 return NULL;
00259 stmt_vinfo = vinfo_for_stmt (stmt);
00260 gcc_assert (stmt_vinfo);
00261 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_loop_def);
00262 oprnd00 = TREE_OPERAND (expr, 0);
00263 oprnd01 = TREE_OPERAND (expr, 1);
00264 }
00265 else
00266 {
00267 tree half_type0, half_type1;
00268 tree def_stmt;
00269 tree oprnd0, oprnd1;
00270
00271 oprnd0 = TREE_OPERAND (expr, 0);
00272 oprnd1 = TREE_OPERAND (expr, 1);
00273 if (TYPE_MAIN_VARIANT (TREE_TYPE (oprnd0))
00274 != TYPE_MAIN_VARIANT (prod_type)
00275 || TYPE_MAIN_VARIANT (TREE_TYPE (oprnd1))
00276 != TYPE_MAIN_VARIANT (prod_type))
00277 return NULL;
00278 if (!widened_name_p (oprnd0, stmt, &half_type0, &def_stmt))
00279 return NULL;
00280 oprnd00 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
00281 if (!widened_name_p (oprnd1, stmt, &half_type1, &def_stmt))
00282 return NULL;
00283 oprnd01 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
00284 if (TYPE_MAIN_VARIANT (half_type0) != TYPE_MAIN_VARIANT (half_type1))
00285 return NULL;
00286 if (TYPE_PRECISION (prod_type) != TYPE_PRECISION (half_type0) * 2)
00287 return NULL;
00288 }
00289
00290 half_type = TREE_TYPE (oprnd00);
00291 *type_in = half_type;
00292 *type_out = type;
00293
00294
00295 pattern_expr = build3 (DOT_PROD_EXPR, type, oprnd00, oprnd01, oprnd1);
00296 if (vect_print_dump_info (REPORT_DETAILS))
00297 {
00298 fprintf (vect_dump, "vect_recog_dot_prod_pattern: detected: ");
00299 print_generic_expr (vect_dump, pattern_expr, TDF_SLIM);
00300 }
00301 return pattern_expr;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 static tree
00337 vect_recog_widen_mult_pattern (tree last_stmt ATTRIBUTE_UNUSED,
00338 tree *type_in ATTRIBUTE_UNUSED,
00339 tree *type_out ATTRIBUTE_UNUSED)
00340 {
00341
00342 return NULL;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 static tree
00379 vect_recog_widen_sum_pattern (tree last_stmt, tree *type_in, tree *type_out)
00380 {
00381 tree stmt, expr;
00382 tree oprnd0, oprnd1;
00383 stmt_vec_info stmt_vinfo = vinfo_for_stmt (last_stmt);
00384 tree type, half_type;
00385 tree pattern_expr;
00386
00387 if (TREE_CODE (last_stmt) != MODIFY_EXPR)
00388 return NULL;
00389
00390 expr = TREE_OPERAND (last_stmt, 1);
00391 type = TREE_TYPE (expr);
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 if (TREE_CODE (expr) != PLUS_EXPR)
00404 return NULL;
00405
00406 if (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def)
00407 return NULL;
00408
00409 oprnd0 = TREE_OPERAND (expr, 0);
00410 oprnd1 = TREE_OPERAND (expr, 1);
00411 if (TYPE_MAIN_VARIANT (TREE_TYPE (oprnd0)) != TYPE_MAIN_VARIANT (type)
00412 || TYPE_MAIN_VARIANT (TREE_TYPE (oprnd1)) != TYPE_MAIN_VARIANT (type))
00413 return NULL;
00414
00415
00416
00417
00418
00419
00420
00421 if (!widened_name_p (oprnd0, last_stmt, &half_type, &stmt))
00422 return NULL;
00423
00424 oprnd0 = TREE_OPERAND (TREE_OPERAND (stmt, 1), 0);
00425 *type_in = half_type;
00426 *type_out = type;
00427
00428
00429 pattern_expr = build2 (WIDEN_SUM_EXPR, type, oprnd0, oprnd1);
00430 if (vect_print_dump_info (REPORT_DETAILS))
00431 {
00432 fprintf (vect_dump, "vect_recog_widen_sum_pattern: detected: ");
00433 print_generic_expr (vect_dump, pattern_expr, TDF_SLIM);
00434 }
00435 return pattern_expr;
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 static void
00462 vect_pattern_recog_1 (
00463 tree (* vect_recog_func) (tree, tree *, tree *),
00464 block_stmt_iterator si)
00465 {
00466 tree stmt = bsi_stmt (si);
00467 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
00468 stmt_vec_info pattern_stmt_info;
00469 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
00470 tree pattern_expr;
00471 tree pattern_vectype;
00472 tree type_in, type_out;
00473 tree pattern_type;
00474 enum tree_code code;
00475 tree var, var_name;
00476 stmt_ann_t ann;
00477
00478 pattern_expr = (* vect_recog_func) (stmt, &type_in, &type_out);
00479 if (!pattern_expr)
00480 return;
00481
00482 if (VECTOR_MODE_P (TYPE_MODE (type_in)))
00483 {
00484
00485
00486 pattern_vectype = type_in;
00487 }
00488 else
00489 {
00490 enum tree_code vec_mode;
00491 enum insn_code icode;
00492 optab optab;
00493
00494
00495 pattern_vectype = get_vectype_for_scalar_type (type_in);
00496 optab = optab_for_tree_code (TREE_CODE (pattern_expr), pattern_vectype);
00497 vec_mode = TYPE_MODE (pattern_vectype);
00498 if (!optab
00499 || (icode = optab->handlers[(int) vec_mode].insn_code) ==
00500 CODE_FOR_nothing
00501 || (type_out
00502 && (insn_data[icode].operand[0].mode !=
00503 TYPE_MODE (get_vectype_for_scalar_type (type_out)))))
00504 return;
00505 }
00506
00507
00508 if (vect_print_dump_info (REPORT_DETAILS))
00509 {
00510 fprintf (vect_dump, "pattern recognized: ");
00511 print_generic_expr (vect_dump, pattern_expr, TDF_SLIM);
00512 }
00513
00514
00515
00516 code = TREE_CODE (pattern_expr);
00517 pattern_type = TREE_TYPE (pattern_expr);
00518 var = create_tmp_var (pattern_type, "patt");
00519 add_referenced_var (var);
00520 var_name = make_ssa_name (var, NULL_TREE);
00521 pattern_expr = build2 (MODIFY_EXPR, void_type_node, var_name, pattern_expr);
00522 SSA_NAME_DEF_STMT (var_name) = pattern_expr;
00523 bsi_insert_before (&si, pattern_expr, BSI_SAME_STMT);
00524 ann = stmt_ann (pattern_expr);
00525 set_stmt_info (ann, new_stmt_vec_info (pattern_expr, loop_vinfo));
00526 pattern_stmt_info = vinfo_for_stmt (pattern_expr);
00527
00528 STMT_VINFO_RELATED_STMT (pattern_stmt_info) = stmt;
00529 STMT_VINFO_DEF_TYPE (pattern_stmt_info) = STMT_VINFO_DEF_TYPE (stmt_info);
00530 STMT_VINFO_VECTYPE (pattern_stmt_info) = pattern_vectype;
00531 STMT_VINFO_IN_PATTERN_P (stmt_info) = true;
00532 STMT_VINFO_RELATED_STMT (stmt_info) = pattern_expr;
00533
00534 return;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 void
00608 vect_pattern_recog (loop_vec_info loop_vinfo)
00609 {
00610 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
00611 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
00612 unsigned int nbbs = loop->num_nodes;
00613 block_stmt_iterator si;
00614 tree stmt;
00615 unsigned int i, j;
00616 tree (* vect_recog_func_ptr) (tree, tree *, tree *);
00617
00618 if (vect_print_dump_info (REPORT_DETAILS))
00619 fprintf (vect_dump, "=== vect_pattern_recog ===");
00620
00621
00622
00623 for (i = 0; i < nbbs; i++)
00624 {
00625 basic_block bb = bbs[i];
00626 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
00627 {
00628 stmt = bsi_stmt (si);
00629
00630
00631 for (j = 0; j < NUM_PATTERNS; j++)
00632 {
00633 vect_recog_func_ptr = vect_vect_recog_func_ptrs[j];
00634 vect_pattern_recog_1 (vect_recog_func_ptr, si);
00635 }
00636 }
00637 }
00638 }