This does some more cleanup and refactoring with two fixes, the pure slp compute in vect_analyze_loop_operations was failing to look at pattern stmts and the vect_is_slp_reduction hunk makes reduction detection fail because the pattern state changes in between reduction detection and vectoriztaion (which re-calls the early code).
Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2015-05-22 Richard Biener <rguent...@suse.de> * tree-vectorizer.h (struct _slp_oprnd_info): Add second_pattern member. * tree-vect-loop.c (vect_analyze_loop_operations): Look at patterns when determining whether SLP is pure. (vect_is_slp_reduction): Remove check for pattern stmts. (vect_is_simple_reduction_1): Remove dead code. * tree-vect-slp.c (vect_create_oprnd_info): Initialize second_pattern. (vect_get_and_check_slp_defs): Pass in the stmt number. Allow the first def in a reduction to be not a pattern stmt when the rest of the stmts def are patterns. (vect_build_slp_tree_1): Allow tcc_expression codes like SAD_EXPR and DOT_PROD_EXPR. (vect_build_slp_tree): Adjust. (vect_analyze_slp): Refactor and move BB vect error message ... (vect_slp_analyze_bb_1): ... here. Index: gcc/tree-vect-loop.c =================================================================== --- gcc/tree-vect-loop.c (revision 223529) +++ gcc/tree-vect-loop.c (working copy) @@ -1399,7 +1399,12 @@ vect_analyze_loop_operations (loop_vec_i { gimple stmt = gsi_stmt (si); stmt_vec_info stmt_info = vinfo_for_stmt (stmt); - gcc_assert (stmt_info); + if (STMT_VINFO_IN_PATTERN_P (stmt_info) + && STMT_VINFO_RELATED_STMT (stmt_info)) + { + stmt = STMT_VINFO_RELATED_STMT (stmt_info); + stmt_info = vinfo_for_stmt (stmt); + } if ((STMT_VINFO_RELEVANT_P (stmt_info) || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))) && !PURE_SLP_STMT (stmt_info)) @@ -2031,12 +2036,8 @@ vect_is_slp_reduction (loop_vec_info loo if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))) { - if (vinfo_for_stmt (use_stmt) - && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt))) - { - loop_use_stmt = use_stmt; - nloop_uses++; - } + loop_use_stmt = use_stmt; + nloop_uses++; } else n_out_of_loop_uses++; @@ -2265,9 +2266,7 @@ vect_is_simple_reduction_1 (loop_vec_inf return NULL; } - if (vinfo_for_stmt (use_stmt) - && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt))) - nloop_uses++; + nloop_uses++; if (nloop_uses > 1) { if (dump_enabled_p ()) @@ -2325,9 +2324,7 @@ vect_is_simple_reduction_1 (loop_vec_inf gimple use_stmt = USE_STMT (use_p); if (is_gimple_debug (use_stmt)) continue; - if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)) - && vinfo_for_stmt (use_stmt) - && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt))) + if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))) nloop_uses++; if (nloop_uses > 1) { Index: gcc/tree-vect-slp.c =================================================================== --- gcc/tree-vect-slp.c (revision 223529) +++ gcc/tree-vect-slp.c (working copy) @@ -183,6 +183,7 @@ vect_create_oprnd_info (int nops, int gr oprnd_info->first_dt = vect_uninitialized_def; oprnd_info->first_op_type = NULL_TREE; oprnd_info->first_pattern = false; + oprnd_info->second_pattern = false; oprnds_info.quick_push (oprnd_info); } @@ -242,7 +243,7 @@ vect_get_place_in_interleaving_chain (gi static int vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo, - gimple stmt, bool first, + gimple stmt, unsigned stmt_num, vec<slp_oprnd_info> *oprnds_info) { tree oprnd; @@ -256,6 +257,8 @@ vect_get_and_check_slp_defs (loop_vec_in int first_op_idx = 1; bool commutative = false; bool first_op_cond = false; + bool first = stmt_num == 0; + bool second = stmt_num == 1; if (loop_vinfo) loop = LOOP_VINFO_LOOP (loop_vinfo); @@ -326,7 +329,11 @@ again: && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt))) { pattern = true; - if (!first && !oprnd_info->first_pattern) + if (!first && !oprnd_info->first_pattern + /* Allow different pattern state for the defs of the + first stmt in reduction chains. */ + && (oprnd_info->first_dt != vect_reduction_def + || (!second && !oprnd_info->second_pattern))) { if (i == 0 && !swapped @@ -377,6 +384,9 @@ again: } } + if (second) + oprnd_info->second_pattern = pattern; + if (first) { oprnd_info->first_dt = dt; @@ -892,7 +902,7 @@ vect_build_slp_tree_1 (loop_vec_info loo /* Not memory operation. */ if (TREE_CODE_CLASS (rhs_code) != tcc_binary && TREE_CODE_CLASS (rhs_code) != tcc_unary - && rhs_code != COND_EXPR + && TREE_CODE_CLASS (rhs_code) != tcc_expression && rhs_code != CALL_EXPR) { if (dump_enabled_p ()) @@ -1033,7 +1043,7 @@ vect_build_slp_tree (loop_vec_info loop_ FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (*node), i, stmt) { switch (vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo, - stmt, (i == 0), &oprnds_info)) + stmt, i, &oprnds_info)) { case 0: break; @@ -1910,17 +1920,7 @@ vect_analyze_slp (loop_vec_info loop_vin max_tree_size)) ok = true; - if (bb_vinfo && !ok) - { - if (dump_enabled_p ()) - dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, - "Failed to SLP the basic block.\n"); - - return false; - } - - if (loop_vinfo - && LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).length () > 0) + if (reduc_chains.length () > 0) { /* Find SLP sequences starting from reduction chains. */ FOR_EACH_VEC_ELT (reduc_chains, i, first_element) @@ -1936,7 +1936,7 @@ vect_analyze_slp (loop_vec_info loop_vin } /* Find SLP sequences starting from groups of reductions. */ - if (loop_vinfo && LOOP_VINFO_REDUCTIONS (loop_vinfo).length () > 1 + if (reductions.length () > 1 && vect_analyze_slp_instance (loop_vinfo, bb_vinfo, reductions[0], max_tree_size)) ok = true; @@ -2443,9 +2443,13 @@ vect_slp_analyze_bb_1 (basic_block bb) if (!vect_analyze_slp (NULL, bb_vinfo, n_stmts)) { if (dump_enabled_p ()) - dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, - "not vectorized: failed to find SLP opportunities " - "in basic block.\n"); + { + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "Failed to SLP the basic block.\n"); + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "not vectorized: failed to find SLP opportunities " + "in basic block.\n"); + } destroy_bb_vec_info (bb_vinfo); return NULL; Index: gcc/tree-vectorizer.h =================================================================== --- gcc/tree-vectorizer.h (revision 223529) +++ gcc/tree-vectorizer.h (working copy) @@ -163,6 +163,7 @@ typedef struct _slp_oprnd_info enum vect_def_type first_dt; tree first_op_type; bool first_pattern; + bool second_pattern; } *slp_oprnd_info;