Hi! As the testcase shows, the non-rectangular loop expansion code didn't try to regimplify operands of GIMPLE_CONDs it built in some cases. I have added a helper function which does that and used it in some places that were regimplifying already to simplify those spots, plus added it in a couple of other places where it was needed.
Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2021-11-16 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/103208 * omp-expand.c (expand_omp_build_cond): New function. (expand_omp_for_init_counts, expand_omp_for_init_vars, expand_omp_for_static_nochunk, expand_omp_for_static_chunk): Use it. * c-c++-common/gomp/loop-11.c: New test. --- gcc/omp-expand.c.jj 2021-11-11 14:35:37.631348121 +0100 +++ gcc/omp-expand.c 2021-11-15 20:39:22.666976655 +0100 @@ -1208,6 +1208,28 @@ expand_omp_build_assign (gimple_stmt_ite } } +/* Prepend or append LHS CODE RHS condition before or after *GSI_P. */ + +static gcond * +expand_omp_build_cond (gimple_stmt_iterator *gsi_p, enum tree_code code, + tree lhs, tree rhs, bool after = false) +{ + gcond *cond_stmt = gimple_build_cond (code, lhs, rhs, NULL_TREE, NULL_TREE); + if (after) + gsi_insert_after (gsi_p, cond_stmt, GSI_CONTINUE_LINKING); + else + gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT); + if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), expand_omp_regimplify_p, + NULL, NULL) + || walk_tree (gimple_cond_rhs_ptr (cond_stmt), expand_omp_regimplify_p, + NULL, NULL)) + { + gimple_stmt_iterator gsi = gsi_for_stmt (cond_stmt); + gimple_regimplify_operands (cond_stmt, &gsi); + } + return cond_stmt; +} + /* Expand the OpenMP parallel or task directive starting at REGION. */ static void @@ -1868,17 +1890,8 @@ expand_omp_for_init_counts (struct omp_f n2 = fold_convert (itype, unshare_expr (fd->loops[i].n2)); n2 = force_gimple_operand_gsi (gsi, n2, true, NULL_TREE, true, GSI_SAME_STMT); - cond_stmt = gimple_build_cond (fd->loops[i].cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (gsi, cond_stmt, GSI_SAME_STMT); - if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL) - || walk_tree (gimple_cond_rhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL)) - { - *gsi = gsi_for_stmt (cond_stmt); - gimple_regimplify_operands (cond_stmt, gsi); - } + cond_stmt = expand_omp_build_cond (gsi, fd->loops[i].cond_code, + n1, n2); e = split_block (entry_bb, cond_stmt); basic_block &zero_iter_bb = i < fd->collapse ? zero_iter1_bb : zero_iter2_bb; @@ -2075,18 +2088,16 @@ expand_omp_for_init_counts (struct omp_f n2e = force_gimple_operand_gsi (&gsi2, n2e, true, NULL_TREE, true, GSI_SAME_STMT); gcond *cond_stmt - = gimple_build_cond (fd->loops[i].cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1, n2); e = split_block (bb1, cond_stmt); e->flags = EDGE_TRUE_VALUE; e->probability = profile_probability::likely ().guessed (); basic_block bb2 = e->dest; gsi2 = gsi_after_labels (bb2); - cond_stmt = gimple_build_cond (fd->loops[i].cond_code, n1e, n2e, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1e, n2e); e = split_block (bb2, cond_stmt); e->flags = EDGE_TRUE_VALUE; e->probability = profile_probability::likely ().guessed (); @@ -2137,9 +2148,8 @@ expand_omp_for_init_counts (struct omp_f e->probability = profile_probability::unlikely ().guessed (); gsi2 = gsi_after_labels (bb3); - cond_stmt = gimple_build_cond (fd->loops[i].cond_code, n1e, n2e, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1e, n2e); e = split_block (bb3, cond_stmt); e->flags = EDGE_TRUE_VALUE; e->probability = profile_probability::likely ().guessed (); @@ -2193,9 +2203,8 @@ expand_omp_for_init_counts (struct omp_f true, GSI_SAME_STMT); expand_omp_build_assign (&gsi2, j ? n2o : n1o, tem); - cond_stmt = gimple_build_cond (fd->loops[i].cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + cond_stmt = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1, n2); e = split_block (gsi_bb (gsi2), cond_stmt); e->flags = j ? EDGE_TRUE_VALUE : EDGE_FALSE_VALUE; e->probability = profile_probability::unlikely ().guessed (); @@ -2298,9 +2307,8 @@ expand_omp_for_init_counts (struct omp_f if (i == fd->last_nonrect) { gcond *cond_stmt - = gimple_build_cond (fd->loops[i].cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1, n2); e = split_block (cur_bb, cond_stmt); e->flags = EDGE_TRUE_VALUE; ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE); @@ -2354,10 +2362,7 @@ expand_omp_for_init_counts (struct omp_f ne = split_block (e->dest, last_stmt (e->dest)); gsi2 = gsi_after_labels (ne->dest); - gcond *cond_stmt - = gimple_build_cond (fd->loops[i].cond_code, vs[i], n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, vs[i], n2); edge e3, e4; if (next_bb == entry_bb) { @@ -2558,10 +2563,8 @@ expand_omp_for_init_vars (struct omp_for tree first_inner_iterations = fd->first_inner_iterations; tree factor = fd->factor; gcond *cond_stmt - = gimple_build_cond (NE_EXPR, factor, - build_zero_cst (TREE_TYPE (factor)), - NULL_TREE, NULL_TREE); - gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING); + = expand_omp_build_cond (gsi, NE_EXPR, factor, + build_zero_cst (TREE_TYPE (factor))); edge e = split_block (gsi_bb (*gsi), cond_stmt); basic_block bb0 = e->src; e->flags = EDGE_TRUE_VALUE; @@ -2831,9 +2834,8 @@ expand_omp_for_init_vars (struct omp_for if (j == fd->last_nonrect) { gcond *cond_stmt - = gimple_build_cond (fd->loops[j].cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi2, cond_stmt, GSI_SAME_STMT); + = expand_omp_build_cond (&gsi2, fd->loops[i].cond_code, + n1, n2); e = split_block (cur_bb, cond_stmt); e->flags = EDGE_TRUE_VALUE; edge ne = make_edge (cur_bb, next_bb, EDGE_FALSE_VALUE); @@ -4909,17 +4911,8 @@ expand_omp_for_static_nochunk (struct om n2 = fold_convert (type, unshare_expr (fd->loop.n2)); n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE, true, GSI_SAME_STMT); - gcond *cond_stmt = gimple_build_cond (fd->loop.cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT); - if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL) - || walk_tree (gimple_cond_rhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL)) - { - gsi = gsi_for_stmt (cond_stmt); - gimple_regimplify_operands (cond_stmt, &gsi); - } + gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code, + n1, n2); ep = split_block (entry_bb, cond_stmt); ep->flags = EDGE_TRUE_VALUE; entry_bb = ep->dest; @@ -5713,17 +5706,8 @@ expand_omp_for_static_chunk (struct omp_ n2 = fold_convert (type, unshare_expr (fd->loop.n2)); n2 = force_gimple_operand_gsi (&gsi, n2, true, NULL_TREE, true, GSI_SAME_STMT); - gcond *cond_stmt = gimple_build_cond (fd->loop.cond_code, n1, n2, - NULL_TREE, NULL_TREE); - gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT); - if (walk_tree (gimple_cond_lhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL) - || walk_tree (gimple_cond_rhs_ptr (cond_stmt), - expand_omp_regimplify_p, NULL, NULL)) - { - gsi = gsi_for_stmt (cond_stmt); - gimple_regimplify_operands (cond_stmt, &gsi); - } + gcond *cond_stmt = expand_omp_build_cond (&gsi, fd->loop.cond_code, + n1, n2); se = split_block (entry_bb, cond_stmt); se->flags = EDGE_TRUE_VALUE; entry_bb = se->dest; --- gcc/testsuite/c-c++-common/gomp/loop-11.c.jj 2021-11-15 20:38:29.864704361 +0100 +++ gcc/testsuite/c-c++-common/gomp/loop-11.c 2021-11-15 20:38:25.028771008 +0100 @@ -0,0 +1,5 @@ +/* PR tree-optimization/103208 */ +/* { dg-do compile } */ +/* { dg-additional-options "-O2 -fwrapv" } */ + +#include "loop-8.c" Jakub