[V5] VECT: Add tree_code into "creat_iv" and allow it can handle MINUS_EXPR IV.

Message ID 20230511051244.1068441-1-juzhe.zhong@rivai.ai
State Accepted
Headers
Series [V5] VECT: Add tree_code into "creat_iv" and allow it can handle MINUS_EXPR IV. |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

juzhe.zhong@rivai.ai May 11, 2023, 5:12 a.m. UTC
  From: Juzhe-Zhong <juzhe.zhong@rivai.ai>

This is patch is a seperate patch preparing for supporting decrement IV.

gcc/ChangeLog:

        * cfgloopmanip.cc (create_empty_loop_on_edge): Add PLUS_EXPR.
        * gimple-loop-interchange.cc (tree_loop_interchange::map_inductions_to_loop): Ditto.
        * tree-ssa-loop-ivcanon.cc (create_canonical_iv): Ditto.
        * tree-ssa-loop-ivopts.cc (create_new_iv): Ditto.
        * tree-ssa-loop-manip.cc (create_iv): Ditto.
        (tree_transform_and_unroll_loop): Ditto.
        (canonicalize_loop_ivs): Ditto.
        * tree-ssa-loop-manip.h (create_iv): Ditto.
        * tree-vect-data-refs.cc (vect_create_data_ref_ptr): Ditto.
        * tree-vect-loop-manip.cc (vect_set_loop_controls_directly): Ditto.
        (vect_set_loop_condition_normal): Ditto.
        * tree-vect-loop.cc (vect_create_epilog_for_reduction): Ditto.
        * tree-vect-stmts.cc (vectorizable_store): Ditto.
        (vectorizable_load): Ditto.

---
 gcc/cfgloopmanip.cc            |  2 +-
 gcc/gimple-loop-interchange.cc |  2 +-
 gcc/tree-ssa-loop-ivcanon.cc   |  2 +-
 gcc/tree-ssa-loop-ivopts.cc    |  2 +-
 gcc/tree-ssa-loop-manip.cc     | 18 +++++++++---------
 gcc/tree-ssa-loop-manip.h      |  4 ++--
 gcc/tree-vect-data-refs.cc     |  8 ++++----
 gcc/tree-vect-loop-manip.cc    |  7 ++++---
 gcc/tree-vect-loop.cc          |  2 +-
 gcc/tree-vect-stmts.cc         |  4 ++--
 10 files changed, 26 insertions(+), 25 deletions(-)
  

Comments

Richard Sandiford May 11, 2023, 8:14 a.m. UTC | #1
juzhe.zhong@rivai.ai writes:
> From: Juzhe-Zhong <juzhe.zhong@rivai.ai>
>
> This is patch is a seperate patch preparing for supporting decrement IV.
>
> gcc/ChangeLog:
>
>         * cfgloopmanip.cc (create_empty_loop_on_edge): Add PLUS_EXPR.
>         * gimple-loop-interchange.cc (tree_loop_interchange::map_inductions_to_loop): Ditto.
>         * tree-ssa-loop-ivcanon.cc (create_canonical_iv): Ditto.
>         * tree-ssa-loop-ivopts.cc (create_new_iv): Ditto.
>         * tree-ssa-loop-manip.cc (create_iv): Ditto.
>         (tree_transform_and_unroll_loop): Ditto.
>         (canonicalize_loop_ivs): Ditto.
>         * tree-ssa-loop-manip.h (create_iv): Ditto.
>         * tree-vect-data-refs.cc (vect_create_data_ref_ptr): Ditto.
>         * tree-vect-loop-manip.cc (vect_set_loop_controls_directly): Ditto.
>         (vect_set_loop_condition_normal): Ditto.
>         * tree-vect-loop.cc (vect_create_epilog_for_reduction): Ditto.
>         * tree-vect-stmts.cc (vectorizable_store): Ditto.
>         (vectorizable_load): Ditto.
>
> ---
>  gcc/cfgloopmanip.cc            |  2 +-
>  gcc/gimple-loop-interchange.cc |  2 +-
>  gcc/tree-ssa-loop-ivcanon.cc   |  2 +-
>  gcc/tree-ssa-loop-ivopts.cc    |  2 +-
>  gcc/tree-ssa-loop-manip.cc     | 18 +++++++++---------
>  gcc/tree-ssa-loop-manip.h      |  4 ++--
>  gcc/tree-vect-data-refs.cc     |  8 ++++----
>  gcc/tree-vect-loop-manip.cc    |  7 ++++---
>  gcc/tree-vect-loop.cc          |  2 +-
>  gcc/tree-vect-stmts.cc         |  4 ++--
>  10 files changed, 26 insertions(+), 25 deletions(-)
>
> diff --git a/gcc/cfgloopmanip.cc b/gcc/cfgloopmanip.cc
> index 0e3ad8ed742..6e09dcbb0b1 100644
> --- a/gcc/cfgloopmanip.cc
> +++ b/gcc/cfgloopmanip.cc
> @@ -826,7 +826,7 @@ create_empty_loop_on_edge (edge entry_edge,
>      }
>  
>    gsi = gsi_last_bb (loop_header);
> -  create_iv (initial_value, stride, iv, loop, &gsi, false,
> +  create_iv (initial_value, PLUS_EXPR, stride, iv, loop, &gsi, false,
>  	     iv_before, iv_after);
>  
>    /* Insert loop exit condition.  */
> diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
> index 1b77bfd46b2..e5590374e59 100644
> --- a/gcc/gimple-loop-interchange.cc
> +++ b/gcc/gimple-loop-interchange.cc
> @@ -1185,7 +1185,7 @@ tree_loop_interchange::map_inductions_to_loop (loop_cand &src, loop_cand &tgt)
>  	  tree var_before, var_after;
>  	  tree base = unshare_expr (iv->init_expr);
>  	  tree step = unshare_expr (iv->step);
> -	  create_iv (base, step, SSA_NAME_VAR (iv->var),
> +	  create_iv (base, PLUS_EXPR, step, SSA_NAME_VAR (iv->var),
>  		     tgt.m_loop, &incr_pos, false, &var_before, &var_after);
>  	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_before));
>  	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_after));
> diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
> index f678de41cb0..6a962a9f503 100644
> --- a/gcc/tree-ssa-loop-ivcanon.cc
> +++ b/gcc/tree-ssa-loop-ivcanon.cc
> @@ -113,7 +113,7 @@ create_canonical_iv (class loop *loop, edge exit, tree niter,
>  		       niter,
>  		       build_int_cst (type, 1));
>    incr_at = gsi_last_bb (in->src);
> -  create_iv (niter,
> +  create_iv (niter, PLUS_EXPR,
>  	     build_int_cst (type, -1),
>  	     NULL_TREE, loop,
>  	     &incr_at, false, var_before, &var);
> diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
> index 324703054b5..6fbd2d59318 100644
> --- a/gcc/tree-ssa-loop-ivopts.cc
> +++ b/gcc/tree-ssa-loop-ivopts.cc
> @@ -7267,7 +7267,7 @@ create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
>  
>    base = unshare_expr (cand->iv->base);
>  
> -  create_iv (base, unshare_expr (cand->iv->step),
> +  create_iv (base, PLUS_EXPR, unshare_expr (cand->iv->step),
>  	     cand->var_before, data->current_loop,
>  	     &incr_pos, after, &cand->var_before, &cand->var_after);
>  }
> diff --git a/gcc/tree-ssa-loop-manip.cc b/gcc/tree-ssa-loop-manip.cc
> index 598e2189f6c..4a333ddf9e6 100644
> --- a/gcc/tree-ssa-loop-manip.cc
> +++ b/gcc/tree-ssa-loop-manip.cc
> @@ -57,16 +57,16 @@ static bitmap_obstack loop_renamer_obstack;
>     VAR_AFTER (unless they are NULL).  */
>  
>  void
> -create_iv (tree base, tree step, tree var, class loop *loop,
> -	   gimple_stmt_iterator *incr_pos, bool after,
> -	   tree *var_before, tree *var_after)
> +create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop,
> +	   gimple_stmt_iterator *incr_pos, bool after, tree *var_before,
> +	   tree *var_after)

Looks like you've lost the hunk that updates the comment.  The one
from the previous patch:

> @@ -47,7 +47,9 @@ along with GCC; see the file COPYING3.  If not see
>     so that we can free them all at once.  */
> static bitmap_obstack loop_renamer_obstack;
> -/* Creates an induction variable with value BASE + STEP * iteration in LOOP.
> +/* Creates an induction variable with value BASE (+/-) STEP * iteration in LOOP.
> +   If CODE is PLUS_EXPR, the induction variable is BASE + STEP * iteration.
> +   If CODE is MINUS_EXPR, the induction variable is BASE - STEP * iteration.
>     It is expected that neither BASE nor STEP are shared with other expressions
>     (unless the sharing rules allow this).  Use VAR as a base var_decl for it
>     (if NULL, a new temporary will be created).  The increment will occur at

is fine, with CODE changed to INCR_OP.  (Fortunately each line still fits
within 80 chars. :) )

OK from my point of view with that change (no need to repost), but please
let Richi have a chance to comment too.

Thanks,
Richard


>  {
>    gassign *stmt;
>    gphi *phi;
>    tree initial, step1;
>    gimple_seq stmts;
>    tree vb, va;
> -  enum tree_code incr_op = PLUS_EXPR;
> +  gcc_assert (incr_op == PLUS_EXPR || incr_op == MINUS_EXPR);
>    edge pe = loop_preheader_edge (loop);
>  
>    if (var != NULL_TREE)
> @@ -93,7 +93,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
>  	  step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
>  	  if (tree_int_cst_lt (step1, step))
>  	    {
> -	      incr_op = MINUS_EXPR;
> +	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
>  	      step = step1;
>  	    }
>  	}
> @@ -104,7 +104,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
>  	  if (!tree_expr_nonnegative_warnv_p (step, &ovf)
>  	      && may_negate_without_overflow_p (step))
>  	    {
> -	      incr_op = MINUS_EXPR;
> +	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
>  	      step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
>  	    }
>  	}
> @@ -1365,7 +1365,7 @@ tree_transform_and_unroll_loop (class loop *loop, unsigned factor,
>        tree ctr_before, ctr_after;
>        gimple_stmt_iterator bsi = gsi_last_nondebug_bb (new_exit->src);
>        exit_if = as_a <gcond *> (gsi_stmt (bsi));
> -      create_iv (exit_base, exit_step, NULL_TREE, loop,
> +      create_iv (exit_base, PLUS_EXPR, exit_step, NULL_TREE, loop,
>  		 &bsi, false, &ctr_before, &ctr_after);
>        gimple_cond_set_code (exit_if, exit_cmp);
>        gimple_cond_set_lhs (exit_if, ctr_after);
> @@ -1580,8 +1580,8 @@ canonicalize_loop_ivs (class loop *loop, tree *nit, bool bump_in_latch)
>      gsi = gsi_last_bb (loop->latch);
>    else
>      gsi = gsi_last_nondebug_bb (loop->header);
> -  create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
> -	     loop, &gsi, bump_in_latch, &var_before, NULL);
> +  create_iv (build_int_cst_type (type, 0), PLUS_EXPR, build_int_cst (type, 1),
> +	     NULL_TREE, loop, &gsi, bump_in_latch, &var_before, NULL);
>  
>    rewrite_all_phi_nodes_with_iv (loop, var_before);
>  
> diff --git a/gcc/tree-ssa-loop-manip.h b/gcc/tree-ssa-loop-manip.h
> index d49273a3987..bda09f51d56 100644
> --- a/gcc/tree-ssa-loop-manip.h
> +++ b/gcc/tree-ssa-loop-manip.h
> @@ -22,8 +22,8 @@ along with GCC; see the file COPYING3.  If not see
>  
>  typedef void (*transform_callback)(class loop *, void *);
>  
> -extern void create_iv (tree, tree, tree, class loop *, gimple_stmt_iterator *,
> -		       bool, tree *, tree *);
> +extern void create_iv (tree, tree_code, tree, tree, class loop *,
> +		       gimple_stmt_iterator *, bool, tree *, tree *);
>  extern void rewrite_into_loop_closed_ssa (bitmap, unsigned);
>  extern void verify_loop_closed_ssa (bool, class loop * = NULL);
>  
> diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
> index 6721ab6efc4..5c9103b16e5 100644
> --- a/gcc/tree-vect-data-refs.cc
> +++ b/gcc/tree-vect-data-refs.cc
> @@ -5099,7 +5099,7 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
>  
>        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
>  
> -      create_iv (aggr_ptr_init,
> +      create_iv (aggr_ptr_init, PLUS_EXPR,
>  		 fold_convert (aggr_ptr_type, iv_step),
>  		 aggr_ptr, loop, &incr_gsi, insert_after,
>  		 &indx_before_incr, &indx_after_incr);
> @@ -5129,9 +5129,9 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
>      {
>        standard_iv_increment_position (containing_loop, &incr_gsi,
>  				      &insert_after);
> -      create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
> -		 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
> -		 &indx_after_incr);
> +      create_iv (aptr, PLUS_EXPR, fold_convert (aggr_ptr_type, DR_STEP (dr)),
> +		 aggr_ptr, containing_loop, &incr_gsi, insert_after,
> +		 &indx_before_incr, &indx_after_incr);
>        incr = gsi_stmt (incr_gsi);
>  
>        /* Copy the points-to information if it exists. */
> diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
> index 44bd5f2c805..ff6159e08d5 100644
> --- a/gcc/tree-vect-loop-manip.cc
> +++ b/gcc/tree-vect-loop-manip.cc
> @@ -468,8 +468,9 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
>    gimple_stmt_iterator incr_gsi;
>    bool insert_after;
>    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -  create_iv (build_int_cst (iv_type, 0), nitems_step, NULL_TREE, loop,
> -	     &incr_gsi, insert_after, &index_before_incr, &index_after_incr);
> +  create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
> +	     loop, &incr_gsi, insert_after, &index_before_incr,
> +	     &index_after_incr);
>  
>    tree zero_index = build_int_cst (compare_type, 0);
>    tree test_index, test_limit, first_limit;
> @@ -893,7 +894,7 @@ vect_set_loop_condition_normal (class loop *loop, tree niters, tree step,
>      }
>  
>    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -  create_iv (init, step, NULL_TREE, loop,
> +  create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
>               &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr);
>    indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
>  					      true, NULL_TREE, true,
> diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> index 6ea0f21fd13..ed0166fedab 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -5567,7 +5567,7 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
>        gimple_stmt_iterator incr_gsi;
>        bool insert_after;
>        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -      create_iv (series_vect, vec_step, NULL_TREE, loop, &incr_gsi,
> +      create_iv (series_vect, PLUS_EXPR, vec_step, NULL_TREE, loop, &incr_gsi,
>  		 insert_after, &indx_before_incr, &indx_after_incr);
>  
>        /* Next create a new phi node vector (NEW_PHI_TREE) which starts
> diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> index 61a2da4ecee..7313191b0db 100644
> --- a/gcc/tree-vect-stmts.cc
> +++ b/gcc/tree-vect-stmts.cc
> @@ -8286,7 +8286,7 @@ vectorizable_store (vec_info *vinfo,
>  
>        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
>        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> -      create_iv (stride_base, ivstep, NULL,
> +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
>  		 loop, &incr_gsi, insert_after,
>  		 &offvar, NULL);
>        incr = gsi_stmt (incr_gsi);
> @@ -9457,7 +9457,7 @@ vectorizable_load (vec_info *vinfo,
>  
>        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
>        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> -      create_iv (stride_base, ivstep, NULL,
> +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
>  		 loop, &incr_gsi, insert_after,
>  		 &offvar, NULL);
  
juzhe.zhong@rivai.ai May 11, 2023, 8:22 a.m. UTC | #2
OK, thanks for Richard Sandiford.

Waiting for Richard Biener comment before commit.



juzhe.zhong@rivai.ai
 
From: Richard Sandiford
Date: 2023-05-11 16:14
To: juzhe.zhong
CC: gcc-patches; rguenther
Subject: Re: [PATCH V5] VECT: Add tree_code into "creat_iv" and allow it can handle MINUS_EXPR IV.
juzhe.zhong@rivai.ai writes:
> From: Juzhe-Zhong <juzhe.zhong@rivai.ai>
>
> This is patch is a seperate patch preparing for supporting decrement IV.
>
> gcc/ChangeLog:
>
>         * cfgloopmanip.cc (create_empty_loop_on_edge): Add PLUS_EXPR.
>         * gimple-loop-interchange.cc (tree_loop_interchange::map_inductions_to_loop): Ditto.
>         * tree-ssa-loop-ivcanon.cc (create_canonical_iv): Ditto.
>         * tree-ssa-loop-ivopts.cc (create_new_iv): Ditto.
>         * tree-ssa-loop-manip.cc (create_iv): Ditto.
>         (tree_transform_and_unroll_loop): Ditto.
>         (canonicalize_loop_ivs): Ditto.
>         * tree-ssa-loop-manip.h (create_iv): Ditto.
>         * tree-vect-data-refs.cc (vect_create_data_ref_ptr): Ditto.
>         * tree-vect-loop-manip.cc (vect_set_loop_controls_directly): Ditto.
>         (vect_set_loop_condition_normal): Ditto.
>         * tree-vect-loop.cc (vect_create_epilog_for_reduction): Ditto.
>         * tree-vect-stmts.cc (vectorizable_store): Ditto.
>         (vectorizable_load): Ditto.
>
> ---
>  gcc/cfgloopmanip.cc            |  2 +-
>  gcc/gimple-loop-interchange.cc |  2 +-
>  gcc/tree-ssa-loop-ivcanon.cc   |  2 +-
>  gcc/tree-ssa-loop-ivopts.cc    |  2 +-
>  gcc/tree-ssa-loop-manip.cc     | 18 +++++++++---------
>  gcc/tree-ssa-loop-manip.h      |  4 ++--
>  gcc/tree-vect-data-refs.cc     |  8 ++++----
>  gcc/tree-vect-loop-manip.cc    |  7 ++++---
>  gcc/tree-vect-loop.cc          |  2 +-
>  gcc/tree-vect-stmts.cc         |  4 ++--
>  10 files changed, 26 insertions(+), 25 deletions(-)
>
> diff --git a/gcc/cfgloopmanip.cc b/gcc/cfgloopmanip.cc
> index 0e3ad8ed742..6e09dcbb0b1 100644
> --- a/gcc/cfgloopmanip.cc
> +++ b/gcc/cfgloopmanip.cc
> @@ -826,7 +826,7 @@ create_empty_loop_on_edge (edge entry_edge,
>      }
>  
>    gsi = gsi_last_bb (loop_header);
> -  create_iv (initial_value, stride, iv, loop, &gsi, false,
> +  create_iv (initial_value, PLUS_EXPR, stride, iv, loop, &gsi, false,
>       iv_before, iv_after);
>  
>    /* Insert loop exit condition.  */
> diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
> index 1b77bfd46b2..e5590374e59 100644
> --- a/gcc/gimple-loop-interchange.cc
> +++ b/gcc/gimple-loop-interchange.cc
> @@ -1185,7 +1185,7 @@ tree_loop_interchange::map_inductions_to_loop (loop_cand &src, loop_cand &tgt)
>    tree var_before, var_after;
>    tree base = unshare_expr (iv->init_expr);
>    tree step = unshare_expr (iv->step);
> -   create_iv (base, step, SSA_NAME_VAR (iv->var),
> +   create_iv (base, PLUS_EXPR, step, SSA_NAME_VAR (iv->var),
>       tgt.m_loop, &incr_pos, false, &var_before, &var_after);
>    bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_before));
>    bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_after));
> diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
> index f678de41cb0..6a962a9f503 100644
> --- a/gcc/tree-ssa-loop-ivcanon.cc
> +++ b/gcc/tree-ssa-loop-ivcanon.cc
> @@ -113,7 +113,7 @@ create_canonical_iv (class loop *loop, edge exit, tree niter,
>         niter,
>         build_int_cst (type, 1));
>    incr_at = gsi_last_bb (in->src);
> -  create_iv (niter,
> +  create_iv (niter, PLUS_EXPR,
>       build_int_cst (type, -1),
>       NULL_TREE, loop,
>       &incr_at, false, var_before, &var);
> diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
> index 324703054b5..6fbd2d59318 100644
> --- a/gcc/tree-ssa-loop-ivopts.cc
> +++ b/gcc/tree-ssa-loop-ivopts.cc
> @@ -7267,7 +7267,7 @@ create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
>  
>    base = unshare_expr (cand->iv->base);
>  
> -  create_iv (base, unshare_expr (cand->iv->step),
> +  create_iv (base, PLUS_EXPR, unshare_expr (cand->iv->step),
>       cand->var_before, data->current_loop,
>       &incr_pos, after, &cand->var_before, &cand->var_after);
>  }
> diff --git a/gcc/tree-ssa-loop-manip.cc b/gcc/tree-ssa-loop-manip.cc
> index 598e2189f6c..4a333ddf9e6 100644
> --- a/gcc/tree-ssa-loop-manip.cc
> +++ b/gcc/tree-ssa-loop-manip.cc
> @@ -57,16 +57,16 @@ static bitmap_obstack loop_renamer_obstack;
>     VAR_AFTER (unless they are NULL).  */
>  
>  void
> -create_iv (tree base, tree step, tree var, class loop *loop,
> -    gimple_stmt_iterator *incr_pos, bool after,
> -    tree *var_before, tree *var_after)
> +create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop,
> +    gimple_stmt_iterator *incr_pos, bool after, tree *var_before,
> +    tree *var_after)
 
Looks like you've lost the hunk that updates the comment.  The one
from the previous patch:
 
> @@ -47,7 +47,9 @@ along with GCC; see the file COPYING3.  If not see
>     so that we can free them all at once.  */
> static bitmap_obstack loop_renamer_obstack;
> -/* Creates an induction variable with value BASE + STEP * iteration in LOOP.
> +/* Creates an induction variable with value BASE (+/-) STEP * iteration in LOOP.
> +   If CODE is PLUS_EXPR, the induction variable is BASE + STEP * iteration.
> +   If CODE is MINUS_EXPR, the induction variable is BASE - STEP * iteration.
>     It is expected that neither BASE nor STEP are shared with other expressions
>     (unless the sharing rules allow this).  Use VAR as a base var_decl for it
>     (if NULL, a new temporary will be created).  The increment will occur at
 
is fine, with CODE changed to INCR_OP.  (Fortunately each line still fits
within 80 chars. :) )
 
OK from my point of view with that change (no need to repost), but please
let Richi have a chance to comment too.
 
Thanks,
Richard
 
 
>  {
>    gassign *stmt;
>    gphi *phi;
>    tree initial, step1;
>    gimple_seq stmts;
>    tree vb, va;
> -  enum tree_code incr_op = PLUS_EXPR;
> +  gcc_assert (incr_op == PLUS_EXPR || incr_op == MINUS_EXPR);
>    edge pe = loop_preheader_edge (loop);
>  
>    if (var != NULL_TREE)
> @@ -93,7 +93,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
>    step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
>    if (tree_int_cst_lt (step1, step))
>      {
> -       incr_op = MINUS_EXPR;
> +       incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
>        step = step1;
>      }
>  }
> @@ -104,7 +104,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
>    if (!tree_expr_nonnegative_warnv_p (step, &ovf)
>        && may_negate_without_overflow_p (step))
>      {
> -       incr_op = MINUS_EXPR;
> +       incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
>        step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
>      }
>  }
> @@ -1365,7 +1365,7 @@ tree_transform_and_unroll_loop (class loop *loop, unsigned factor,
>        tree ctr_before, ctr_after;
>        gimple_stmt_iterator bsi = gsi_last_nondebug_bb (new_exit->src);
>        exit_if = as_a <gcond *> (gsi_stmt (bsi));
> -      create_iv (exit_base, exit_step, NULL_TREE, loop,
> +      create_iv (exit_base, PLUS_EXPR, exit_step, NULL_TREE, loop,
>  &bsi, false, &ctr_before, &ctr_after);
>        gimple_cond_set_code (exit_if, exit_cmp);
>        gimple_cond_set_lhs (exit_if, ctr_after);
> @@ -1580,8 +1580,8 @@ canonicalize_loop_ivs (class loop *loop, tree *nit, bool bump_in_latch)
>      gsi = gsi_last_bb (loop->latch);
>    else
>      gsi = gsi_last_nondebug_bb (loop->header);
> -  create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
> -      loop, &gsi, bump_in_latch, &var_before, NULL);
> +  create_iv (build_int_cst_type (type, 0), PLUS_EXPR, build_int_cst (type, 1),
> +      NULL_TREE, loop, &gsi, bump_in_latch, &var_before, NULL);
>  
>    rewrite_all_phi_nodes_with_iv (loop, var_before);
>  
> diff --git a/gcc/tree-ssa-loop-manip.h b/gcc/tree-ssa-loop-manip.h
> index d49273a3987..bda09f51d56 100644
> --- a/gcc/tree-ssa-loop-manip.h
> +++ b/gcc/tree-ssa-loop-manip.h
> @@ -22,8 +22,8 @@ along with GCC; see the file COPYING3.  If not see
>  
>  typedef void (*transform_callback)(class loop *, void *);
>  
> -extern void create_iv (tree, tree, tree, class loop *, gimple_stmt_iterator *,
> -        bool, tree *, tree *);
> +extern void create_iv (tree, tree_code, tree, tree, class loop *,
> +        gimple_stmt_iterator *, bool, tree *, tree *);
>  extern void rewrite_into_loop_closed_ssa (bitmap, unsigned);
>  extern void verify_loop_closed_ssa (bool, class loop * = NULL);
>  
> diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
> index 6721ab6efc4..5c9103b16e5 100644
> --- a/gcc/tree-vect-data-refs.cc
> +++ b/gcc/tree-vect-data-refs.cc
> @@ -5099,7 +5099,7 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
>  
>        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
>  
> -      create_iv (aggr_ptr_init,
> +      create_iv (aggr_ptr_init, PLUS_EXPR,
>  fold_convert (aggr_ptr_type, iv_step),
>  aggr_ptr, loop, &incr_gsi, insert_after,
>  &indx_before_incr, &indx_after_incr);
> @@ -5129,9 +5129,9 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
>      {
>        standard_iv_increment_position (containing_loop, &incr_gsi,
>        &insert_after);
> -      create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
> - containing_loop, &incr_gsi, insert_after, &indx_before_incr,
> - &indx_after_incr);
> +      create_iv (aptr, PLUS_EXPR, fold_convert (aggr_ptr_type, DR_STEP (dr)),
> + aggr_ptr, containing_loop, &incr_gsi, insert_after,
> + &indx_before_incr, &indx_after_incr);
>        incr = gsi_stmt (incr_gsi);
>  
>        /* Copy the points-to information if it exists. */
> diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
> index 44bd5f2c805..ff6159e08d5 100644
> --- a/gcc/tree-vect-loop-manip.cc
> +++ b/gcc/tree-vect-loop-manip.cc
> @@ -468,8 +468,9 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
>    gimple_stmt_iterator incr_gsi;
>    bool insert_after;
>    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -  create_iv (build_int_cst (iv_type, 0), nitems_step, NULL_TREE, loop,
> -      &incr_gsi, insert_after, &index_before_incr, &index_after_incr);
> +  create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
> +      loop, &incr_gsi, insert_after, &index_before_incr,
> +      &index_after_incr);
>  
>    tree zero_index = build_int_cst (compare_type, 0);
>    tree test_index, test_limit, first_limit;
> @@ -893,7 +894,7 @@ vect_set_loop_condition_normal (class loop *loop, tree niters, tree step,
>      }
>  
>    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -  create_iv (init, step, NULL_TREE, loop,
> +  create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
>               &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr);
>    indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
>        true, NULL_TREE, true,
> diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> index 6ea0f21fd13..ed0166fedab 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -5567,7 +5567,7 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
>        gimple_stmt_iterator incr_gsi;
>        bool insert_after;
>        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> -      create_iv (series_vect, vec_step, NULL_TREE, loop, &incr_gsi,
> +      create_iv (series_vect, PLUS_EXPR, vec_step, NULL_TREE, loop, &incr_gsi,
>  insert_after, &indx_before_incr, &indx_after_incr);
>  
>        /* Next create a new phi node vector (NEW_PHI_TREE) which starts
> diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> index 61a2da4ecee..7313191b0db 100644
> --- a/gcc/tree-vect-stmts.cc
> +++ b/gcc/tree-vect-stmts.cc
> @@ -8286,7 +8286,7 @@ vectorizable_store (vec_info *vinfo,
>  
>        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
>        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> -      create_iv (stride_base, ivstep, NULL,
> +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
>  loop, &incr_gsi, insert_after,
>  &offvar, NULL);
>        incr = gsi_stmt (incr_gsi);
> @@ -9457,7 +9457,7 @@ vectorizable_load (vec_info *vinfo,
>  
>        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
>        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> -      create_iv (stride_base, ivstep, NULL,
> +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
>  loop, &incr_gsi, insert_after,
>  &offvar, NULL);
  
Richard Biener May 11, 2023, 9:13 a.m. UTC | #3
On Thu, 11 May 2023, Richard Sandiford wrote:

> juzhe.zhong@rivai.ai writes:
> > From: Juzhe-Zhong <juzhe.zhong@rivai.ai>
> >
> > This is patch is a seperate patch preparing for supporting decrement IV.
> >
> > gcc/ChangeLog:
> >
> >         * cfgloopmanip.cc (create_empty_loop_on_edge): Add PLUS_EXPR.
> >         * gimple-loop-interchange.cc (tree_loop_interchange::map_inductions_to_loop): Ditto.
> >         * tree-ssa-loop-ivcanon.cc (create_canonical_iv): Ditto.
> >         * tree-ssa-loop-ivopts.cc (create_new_iv): Ditto.
> >         * tree-ssa-loop-manip.cc (create_iv): Ditto.
> >         (tree_transform_and_unroll_loop): Ditto.
> >         (canonicalize_loop_ivs): Ditto.
> >         * tree-ssa-loop-manip.h (create_iv): Ditto.
> >         * tree-vect-data-refs.cc (vect_create_data_ref_ptr): Ditto.
> >         * tree-vect-loop-manip.cc (vect_set_loop_controls_directly): Ditto.
> >         (vect_set_loop_condition_normal): Ditto.
> >         * tree-vect-loop.cc (vect_create_epilog_for_reduction): Ditto.
> >         * tree-vect-stmts.cc (vectorizable_store): Ditto.
> >         (vectorizable_load): Ditto.
> >
> > ---
> >  gcc/cfgloopmanip.cc            |  2 +-
> >  gcc/gimple-loop-interchange.cc |  2 +-
> >  gcc/tree-ssa-loop-ivcanon.cc   |  2 +-
> >  gcc/tree-ssa-loop-ivopts.cc    |  2 +-
> >  gcc/tree-ssa-loop-manip.cc     | 18 +++++++++---------
> >  gcc/tree-ssa-loop-manip.h      |  4 ++--
> >  gcc/tree-vect-data-refs.cc     |  8 ++++----
> >  gcc/tree-vect-loop-manip.cc    |  7 ++++---
> >  gcc/tree-vect-loop.cc          |  2 +-
> >  gcc/tree-vect-stmts.cc         |  4 ++--
> >  10 files changed, 26 insertions(+), 25 deletions(-)
> >
> > diff --git a/gcc/cfgloopmanip.cc b/gcc/cfgloopmanip.cc
> > index 0e3ad8ed742..6e09dcbb0b1 100644
> > --- a/gcc/cfgloopmanip.cc
> > +++ b/gcc/cfgloopmanip.cc
> > @@ -826,7 +826,7 @@ create_empty_loop_on_edge (edge entry_edge,
> >      }
> >  
> >    gsi = gsi_last_bb (loop_header);
> > -  create_iv (initial_value, stride, iv, loop, &gsi, false,
> > +  create_iv (initial_value, PLUS_EXPR, stride, iv, loop, &gsi, false,
> >  	     iv_before, iv_after);
> >  
> >    /* Insert loop exit condition.  */
> > diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
> > index 1b77bfd46b2..e5590374e59 100644
> > --- a/gcc/gimple-loop-interchange.cc
> > +++ b/gcc/gimple-loop-interchange.cc
> > @@ -1185,7 +1185,7 @@ tree_loop_interchange::map_inductions_to_loop (loop_cand &src, loop_cand &tgt)
> >  	  tree var_before, var_after;
> >  	  tree base = unshare_expr (iv->init_expr);
> >  	  tree step = unshare_expr (iv->step);
> > -	  create_iv (base, step, SSA_NAME_VAR (iv->var),
> > +	  create_iv (base, PLUS_EXPR, step, SSA_NAME_VAR (iv->var),
> >  		     tgt.m_loop, &incr_pos, false, &var_before, &var_after);
> >  	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_before));
> >  	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_after));
> > diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
> > index f678de41cb0..6a962a9f503 100644
> > --- a/gcc/tree-ssa-loop-ivcanon.cc
> > +++ b/gcc/tree-ssa-loop-ivcanon.cc
> > @@ -113,7 +113,7 @@ create_canonical_iv (class loop *loop, edge exit, tree niter,
> >  		       niter,
> >  		       build_int_cst (type, 1));
> >    incr_at = gsi_last_bb (in->src);
> > -  create_iv (niter,
> > +  create_iv (niter, PLUS_EXPR,
> >  	     build_int_cst (type, -1),
> >  	     NULL_TREE, loop,
> >  	     &incr_at, false, var_before, &var);
> > diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
> > index 324703054b5..6fbd2d59318 100644
> > --- a/gcc/tree-ssa-loop-ivopts.cc
> > +++ b/gcc/tree-ssa-loop-ivopts.cc
> > @@ -7267,7 +7267,7 @@ create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
> >  
> >    base = unshare_expr (cand->iv->base);
> >  
> > -  create_iv (base, unshare_expr (cand->iv->step),
> > +  create_iv (base, PLUS_EXPR, unshare_expr (cand->iv->step),
> >  	     cand->var_before, data->current_loop,
> >  	     &incr_pos, after, &cand->var_before, &cand->var_after);
> >  }
> > diff --git a/gcc/tree-ssa-loop-manip.cc b/gcc/tree-ssa-loop-manip.cc
> > index 598e2189f6c..4a333ddf9e6 100644
> > --- a/gcc/tree-ssa-loop-manip.cc
> > +++ b/gcc/tree-ssa-loop-manip.cc
> > @@ -57,16 +57,16 @@ static bitmap_obstack loop_renamer_obstack;
> >     VAR_AFTER (unless they are NULL).  */
> >  
> >  void
> > -create_iv (tree base, tree step, tree var, class loop *loop,
> > -	   gimple_stmt_iterator *incr_pos, bool after,
> > -	   tree *var_before, tree *var_after)
> > +create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop,
> > +	   gimple_stmt_iterator *incr_pos, bool after, tree *var_before,
> > +	   tree *var_after)
> 
> Looks like you've lost the hunk that updates the comment.  The one
> from the previous patch:
> 
> > @@ -47,7 +47,9 @@ along with GCC; see the file COPYING3.  If not see
> >     so that we can free them all at once.  */
> > static bitmap_obstack loop_renamer_obstack;
> > -/* Creates an induction variable with value BASE + STEP * iteration in LOOP.
> > +/* Creates an induction variable with value BASE (+/-) STEP * iteration in LOOP.
> > +   If CODE is PLUS_EXPR, the induction variable is BASE + STEP * iteration.
> > +   If CODE is MINUS_EXPR, the induction variable is BASE - STEP * iteration.
> >     It is expected that neither BASE nor STEP are shared with other expressions
> >     (unless the sharing rules allow this).  Use VAR as a base var_decl for it
> >     (if NULL, a new temporary will be created).  The increment will occur at
> 
> is fine, with CODE changed to INCR_OP.  (Fortunately each line still fits
> within 80 chars. :) )
> 
> OK from my point of view with that change (no need to repost), but please
> let Richi have a chance to comment too.

LGTM as well.

Thanks,
Richard.

> Thanks,
> Richard
> 
> 
> >  {
> >    gassign *stmt;
> >    gphi *phi;
> >    tree initial, step1;
> >    gimple_seq stmts;
> >    tree vb, va;
> > -  enum tree_code incr_op = PLUS_EXPR;
> > +  gcc_assert (incr_op == PLUS_EXPR || incr_op == MINUS_EXPR);
> >    edge pe = loop_preheader_edge (loop);
> >  
> >    if (var != NULL_TREE)
> > @@ -93,7 +93,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
> >  	  step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
> >  	  if (tree_int_cst_lt (step1, step))
> >  	    {
> > -	      incr_op = MINUS_EXPR;
> > +	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
> >  	      step = step1;
> >  	    }
> >  	}
> > @@ -104,7 +104,7 @@ create_iv (tree base, tree step, tree var, class loop *loop,
> >  	  if (!tree_expr_nonnegative_warnv_p (step, &ovf)
> >  	      && may_negate_without_overflow_p (step))
> >  	    {
> > -	      incr_op = MINUS_EXPR;
> > +	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
> >  	      step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
> >  	    }
> >  	}
> > @@ -1365,7 +1365,7 @@ tree_transform_and_unroll_loop (class loop *loop, unsigned factor,
> >        tree ctr_before, ctr_after;
> >        gimple_stmt_iterator bsi = gsi_last_nondebug_bb (new_exit->src);
> >        exit_if = as_a <gcond *> (gsi_stmt (bsi));
> > -      create_iv (exit_base, exit_step, NULL_TREE, loop,
> > +      create_iv (exit_base, PLUS_EXPR, exit_step, NULL_TREE, loop,
> >  		 &bsi, false, &ctr_before, &ctr_after);
> >        gimple_cond_set_code (exit_if, exit_cmp);
> >        gimple_cond_set_lhs (exit_if, ctr_after);
> > @@ -1580,8 +1580,8 @@ canonicalize_loop_ivs (class loop *loop, tree *nit, bool bump_in_latch)
> >      gsi = gsi_last_bb (loop->latch);
> >    else
> >      gsi = gsi_last_nondebug_bb (loop->header);
> > -  create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
> > -	     loop, &gsi, bump_in_latch, &var_before, NULL);
> > +  create_iv (build_int_cst_type (type, 0), PLUS_EXPR, build_int_cst (type, 1),
> > +	     NULL_TREE, loop, &gsi, bump_in_latch, &var_before, NULL);
> >  
> >    rewrite_all_phi_nodes_with_iv (loop, var_before);
> >  
> > diff --git a/gcc/tree-ssa-loop-manip.h b/gcc/tree-ssa-loop-manip.h
> > index d49273a3987..bda09f51d56 100644
> > --- a/gcc/tree-ssa-loop-manip.h
> > +++ b/gcc/tree-ssa-loop-manip.h
> > @@ -22,8 +22,8 @@ along with GCC; see the file COPYING3.  If not see
> >  
> >  typedef void (*transform_callback)(class loop *, void *);
> >  
> > -extern void create_iv (tree, tree, tree, class loop *, gimple_stmt_iterator *,
> > -		       bool, tree *, tree *);
> > +extern void create_iv (tree, tree_code, tree, tree, class loop *,
> > +		       gimple_stmt_iterator *, bool, tree *, tree *);
> >  extern void rewrite_into_loop_closed_ssa (bitmap, unsigned);
> >  extern void verify_loop_closed_ssa (bool, class loop * = NULL);
> >  
> > diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
> > index 6721ab6efc4..5c9103b16e5 100644
> > --- a/gcc/tree-vect-data-refs.cc
> > +++ b/gcc/tree-vect-data-refs.cc
> > @@ -5099,7 +5099,7 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
> >  
> >        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> >  
> > -      create_iv (aggr_ptr_init,
> > +      create_iv (aggr_ptr_init, PLUS_EXPR,
> >  		 fold_convert (aggr_ptr_type, iv_step),
> >  		 aggr_ptr, loop, &incr_gsi, insert_after,
> >  		 &indx_before_incr, &indx_after_incr);
> > @@ -5129,9 +5129,9 @@ vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
> >      {
> >        standard_iv_increment_position (containing_loop, &incr_gsi,
> >  				      &insert_after);
> > -      create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
> > -		 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
> > -		 &indx_after_incr);
> > +      create_iv (aptr, PLUS_EXPR, fold_convert (aggr_ptr_type, DR_STEP (dr)),
> > +		 aggr_ptr, containing_loop, &incr_gsi, insert_after,
> > +		 &indx_before_incr, &indx_after_incr);
> >        incr = gsi_stmt (incr_gsi);
> >  
> >        /* Copy the points-to information if it exists. */
> > diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
> > index 44bd5f2c805..ff6159e08d5 100644
> > --- a/gcc/tree-vect-loop-manip.cc
> > +++ b/gcc/tree-vect-loop-manip.cc
> > @@ -468,8 +468,9 @@ vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
> >    gimple_stmt_iterator incr_gsi;
> >    bool insert_after;
> >    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> > -  create_iv (build_int_cst (iv_type, 0), nitems_step, NULL_TREE, loop,
> > -	     &incr_gsi, insert_after, &index_before_incr, &index_after_incr);
> > +  create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
> > +	     loop, &incr_gsi, insert_after, &index_before_incr,
> > +	     &index_after_incr);
> >  
> >    tree zero_index = build_int_cst (compare_type, 0);
> >    tree test_index, test_limit, first_limit;
> > @@ -893,7 +894,7 @@ vect_set_loop_condition_normal (class loop *loop, tree niters, tree step,
> >      }
> >  
> >    standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> > -  create_iv (init, step, NULL_TREE, loop,
> > +  create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
> >               &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr);
> >    indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
> >  					      true, NULL_TREE, true,
> > diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> > index 6ea0f21fd13..ed0166fedab 100644
> > --- a/gcc/tree-vect-loop.cc
> > +++ b/gcc/tree-vect-loop.cc
> > @@ -5567,7 +5567,7 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
> >        gimple_stmt_iterator incr_gsi;
> >        bool insert_after;
> >        standard_iv_increment_position (loop, &incr_gsi, &insert_after);
> > -      create_iv (series_vect, vec_step, NULL_TREE, loop, &incr_gsi,
> > +      create_iv (series_vect, PLUS_EXPR, vec_step, NULL_TREE, loop, &incr_gsi,
> >  		 insert_after, &indx_before_incr, &indx_after_incr);
> >  
> >        /* Next create a new phi node vector (NEW_PHI_TREE) which starts
> > diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> > index 61a2da4ecee..7313191b0db 100644
> > --- a/gcc/tree-vect-stmts.cc
> > +++ b/gcc/tree-vect-stmts.cc
> > @@ -8286,7 +8286,7 @@ vectorizable_store (vec_info *vinfo,
> >  
> >        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
> >        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> > -      create_iv (stride_base, ivstep, NULL,
> > +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
> >  		 loop, &incr_gsi, insert_after,
> >  		 &offvar, NULL);
> >        incr = gsi_stmt (incr_gsi);
> > @@ -9457,7 +9457,7 @@ vectorizable_load (vec_info *vinfo,
> >  
> >        stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
> >        ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
> > -      create_iv (stride_base, ivstep, NULL,
> > +      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
> >  		 loop, &incr_gsi, insert_after,
> >  		 &offvar, NULL);
>
  

Patch

diff --git a/gcc/cfgloopmanip.cc b/gcc/cfgloopmanip.cc
index 0e3ad8ed742..6e09dcbb0b1 100644
--- a/gcc/cfgloopmanip.cc
+++ b/gcc/cfgloopmanip.cc
@@ -826,7 +826,7 @@  create_empty_loop_on_edge (edge entry_edge,
     }
 
   gsi = gsi_last_bb (loop_header);
-  create_iv (initial_value, stride, iv, loop, &gsi, false,
+  create_iv (initial_value, PLUS_EXPR, stride, iv, loop, &gsi, false,
 	     iv_before, iv_after);
 
   /* Insert loop exit condition.  */
diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
index 1b77bfd46b2..e5590374e59 100644
--- a/gcc/gimple-loop-interchange.cc
+++ b/gcc/gimple-loop-interchange.cc
@@ -1185,7 +1185,7 @@  tree_loop_interchange::map_inductions_to_loop (loop_cand &src, loop_cand &tgt)
 	  tree var_before, var_after;
 	  tree base = unshare_expr (iv->init_expr);
 	  tree step = unshare_expr (iv->step);
-	  create_iv (base, step, SSA_NAME_VAR (iv->var),
+	  create_iv (base, PLUS_EXPR, step, SSA_NAME_VAR (iv->var),
 		     tgt.m_loop, &incr_pos, false, &var_before, &var_after);
 	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_before));
 	  bitmap_set_bit (m_dce_seeds, SSA_NAME_VERSION (var_after));
diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
index f678de41cb0..6a962a9f503 100644
--- a/gcc/tree-ssa-loop-ivcanon.cc
+++ b/gcc/tree-ssa-loop-ivcanon.cc
@@ -113,7 +113,7 @@  create_canonical_iv (class loop *loop, edge exit, tree niter,
 		       niter,
 		       build_int_cst (type, 1));
   incr_at = gsi_last_bb (in->src);
-  create_iv (niter,
+  create_iv (niter, PLUS_EXPR,
 	     build_int_cst (type, -1),
 	     NULL_TREE, loop,
 	     &incr_at, false, var_before, &var);
diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
index 324703054b5..6fbd2d59318 100644
--- a/gcc/tree-ssa-loop-ivopts.cc
+++ b/gcc/tree-ssa-loop-ivopts.cc
@@ -7267,7 +7267,7 @@  create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
 
   base = unshare_expr (cand->iv->base);
 
-  create_iv (base, unshare_expr (cand->iv->step),
+  create_iv (base, PLUS_EXPR, unshare_expr (cand->iv->step),
 	     cand->var_before, data->current_loop,
 	     &incr_pos, after, &cand->var_before, &cand->var_after);
 }
diff --git a/gcc/tree-ssa-loop-manip.cc b/gcc/tree-ssa-loop-manip.cc
index 598e2189f6c..4a333ddf9e6 100644
--- a/gcc/tree-ssa-loop-manip.cc
+++ b/gcc/tree-ssa-loop-manip.cc
@@ -57,16 +57,16 @@  static bitmap_obstack loop_renamer_obstack;
    VAR_AFTER (unless they are NULL).  */
 
 void
-create_iv (tree base, tree step, tree var, class loop *loop,
-	   gimple_stmt_iterator *incr_pos, bool after,
-	   tree *var_before, tree *var_after)
+create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop,
+	   gimple_stmt_iterator *incr_pos, bool after, tree *var_before,
+	   tree *var_after)
 {
   gassign *stmt;
   gphi *phi;
   tree initial, step1;
   gimple_seq stmts;
   tree vb, va;
-  enum tree_code incr_op = PLUS_EXPR;
+  gcc_assert (incr_op == PLUS_EXPR || incr_op == MINUS_EXPR);
   edge pe = loop_preheader_edge (loop);
 
   if (var != NULL_TREE)
@@ -93,7 +93,7 @@  create_iv (tree base, tree step, tree var, class loop *loop,
 	  step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
 	  if (tree_int_cst_lt (step1, step))
 	    {
-	      incr_op = MINUS_EXPR;
+	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
 	      step = step1;
 	    }
 	}
@@ -104,7 +104,7 @@  create_iv (tree base, tree step, tree var, class loop *loop,
 	  if (!tree_expr_nonnegative_warnv_p (step, &ovf)
 	      && may_negate_without_overflow_p (step))
 	    {
-	      incr_op = MINUS_EXPR;
+	      incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
 	      step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
 	    }
 	}
@@ -1365,7 +1365,7 @@  tree_transform_and_unroll_loop (class loop *loop, unsigned factor,
       tree ctr_before, ctr_after;
       gimple_stmt_iterator bsi = gsi_last_nondebug_bb (new_exit->src);
       exit_if = as_a <gcond *> (gsi_stmt (bsi));
-      create_iv (exit_base, exit_step, NULL_TREE, loop,
+      create_iv (exit_base, PLUS_EXPR, exit_step, NULL_TREE, loop,
 		 &bsi, false, &ctr_before, &ctr_after);
       gimple_cond_set_code (exit_if, exit_cmp);
       gimple_cond_set_lhs (exit_if, ctr_after);
@@ -1580,8 +1580,8 @@  canonicalize_loop_ivs (class loop *loop, tree *nit, bool bump_in_latch)
     gsi = gsi_last_bb (loop->latch);
   else
     gsi = gsi_last_nondebug_bb (loop->header);
-  create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
-	     loop, &gsi, bump_in_latch, &var_before, NULL);
+  create_iv (build_int_cst_type (type, 0), PLUS_EXPR, build_int_cst (type, 1),
+	     NULL_TREE, loop, &gsi, bump_in_latch, &var_before, NULL);
 
   rewrite_all_phi_nodes_with_iv (loop, var_before);
 
diff --git a/gcc/tree-ssa-loop-manip.h b/gcc/tree-ssa-loop-manip.h
index d49273a3987..bda09f51d56 100644
--- a/gcc/tree-ssa-loop-manip.h
+++ b/gcc/tree-ssa-loop-manip.h
@@ -22,8 +22,8 @@  along with GCC; see the file COPYING3.  If not see
 
 typedef void (*transform_callback)(class loop *, void *);
 
-extern void create_iv (tree, tree, tree, class loop *, gimple_stmt_iterator *,
-		       bool, tree *, tree *);
+extern void create_iv (tree, tree_code, tree, tree, class loop *,
+		       gimple_stmt_iterator *, bool, tree *, tree *);
 extern void rewrite_into_loop_closed_ssa (bitmap, unsigned);
 extern void verify_loop_closed_ssa (bool, class loop * = NULL);
 
diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
index 6721ab6efc4..5c9103b16e5 100644
--- a/gcc/tree-vect-data-refs.cc
+++ b/gcc/tree-vect-data-refs.cc
@@ -5099,7 +5099,7 @@  vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
 
       standard_iv_increment_position (loop, &incr_gsi, &insert_after);
 
-      create_iv (aggr_ptr_init,
+      create_iv (aggr_ptr_init, PLUS_EXPR,
 		 fold_convert (aggr_ptr_type, iv_step),
 		 aggr_ptr, loop, &incr_gsi, insert_after,
 		 &indx_before_incr, &indx_after_incr);
@@ -5129,9 +5129,9 @@  vect_create_data_ref_ptr (vec_info *vinfo, stmt_vec_info stmt_info,
     {
       standard_iv_increment_position (containing_loop, &incr_gsi,
 				      &insert_after);
-      create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
-		 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
-		 &indx_after_incr);
+      create_iv (aptr, PLUS_EXPR, fold_convert (aggr_ptr_type, DR_STEP (dr)),
+		 aggr_ptr, containing_loop, &incr_gsi, insert_after,
+		 &indx_before_incr, &indx_after_incr);
       incr = gsi_stmt (incr_gsi);
 
       /* Copy the points-to information if it exists. */
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index 44bd5f2c805..ff6159e08d5 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -468,8 +468,9 @@  vect_set_loop_controls_directly (class loop *loop, loop_vec_info loop_vinfo,
   gimple_stmt_iterator incr_gsi;
   bool insert_after;
   standard_iv_increment_position (loop, &incr_gsi, &insert_after);
-  create_iv (build_int_cst (iv_type, 0), nitems_step, NULL_TREE, loop,
-	     &incr_gsi, insert_after, &index_before_incr, &index_after_incr);
+  create_iv (build_int_cst (iv_type, 0), PLUS_EXPR, nitems_step, NULL_TREE,
+	     loop, &incr_gsi, insert_after, &index_before_incr,
+	     &index_after_incr);
 
   tree zero_index = build_int_cst (compare_type, 0);
   tree test_index, test_limit, first_limit;
@@ -893,7 +894,7 @@  vect_set_loop_condition_normal (class loop *loop, tree niters, tree step,
     }
 
   standard_iv_increment_position (loop, &incr_gsi, &insert_after);
-  create_iv (init, step, NULL_TREE, loop,
+  create_iv (init, PLUS_EXPR, step, NULL_TREE, loop,
              &incr_gsi, insert_after, &indx_before_incr, &indx_after_incr);
   indx_after_incr = force_gimple_operand_gsi (&loop_cond_gsi, indx_after_incr,
 					      true, NULL_TREE, true,
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 6ea0f21fd13..ed0166fedab 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -5567,7 +5567,7 @@  vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
       gimple_stmt_iterator incr_gsi;
       bool insert_after;
       standard_iv_increment_position (loop, &incr_gsi, &insert_after);
-      create_iv (series_vect, vec_step, NULL_TREE, loop, &incr_gsi,
+      create_iv (series_vect, PLUS_EXPR, vec_step, NULL_TREE, loop, &incr_gsi,
 		 insert_after, &indx_before_incr, &indx_after_incr);
 
       /* Next create a new phi node vector (NEW_PHI_TREE) which starts
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 61a2da4ecee..7313191b0db 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -8286,7 +8286,7 @@  vectorizable_store (vec_info *vinfo,
 
       stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
       ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
-      create_iv (stride_base, ivstep, NULL,
+      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
 		 loop, &incr_gsi, insert_after,
 		 &offvar, NULL);
       incr = gsi_stmt (incr_gsi);
@@ -9457,7 +9457,7 @@  vectorizable_load (vec_info *vinfo,
 
       stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
       ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
-      create_iv (stride_base, ivstep, NULL,
+      create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
 		 loop, &incr_gsi, insert_after,
 		 &offvar, NULL);