public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RFC: Variable-length VECTOR_CSTs
@ 2017-11-29 12:14 Richard Sandiford
  2017-11-29 15:11 ` David Malcolm
  2017-11-29 15:56 ` RFC: Variable-length VECTOR_CSTs Richard Biener
  0 siblings, 2 replies; 24+ messages in thread
From: Richard Sandiford @ 2017-11-29 12:14 UTC (permalink / raw)
  To: gcc-patches

It was clear from the SVE reviews that people were unhappy with how
"special" the variable-length case was.  One particular concern was
the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
that would in turn lead to different representations of VEC_PERM_EXPRs
with constant permute vectors, and difficulties in invoking
vec_perm_const_ok.

This is an RFC for a VECTOR_CST representation that treats each
specific constant as one instance of an arbitrary-length sequence.
The reprensentation then extends to variable-length vectors in a
natural way.

As discussed on IRC, if a vector contains X*N elements for some
constant N and integer X>0, the main features we need are:

1) the ability to represent a sequence that duplicates N values

   This is useful for SLP invariants.

2) the ability to represent a sequence that starts with N values and
   is followed by zeros

   This is useful for the initial value in a double or SLP reduction

3) the ability to represent N interleaved series

   This is useful for SLP inductions and for VEC_PERM_EXPRs.

For (2), zero isn't necessarily special, since vectors used in an AND
reduction might need to fill with ones.  Also, we might need up to N
different fill values with mixed SLP operations; it isn't necessarily
safe to assume that a single fill value will always be enough.

The same goes for (3): there's no reason in principle why the
steps in an SLP induction should all be the same (although they
do need to be at the moment).  E.g. once we support SLP on:

  for (unsigned int i = 0; i < n; i += 2)
    {
      x[i] += 4 + i;
      x[i + 1] += 11 + i * 3;
    }

we'll need {[4, 14], +, [2, 6]}.

So the idea is to represent vectors as P interleaved patterns of the form:

  [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]

where the STEP is always zero (actually null) for non-integer vectors.
This is effectively projecting a "foreground" value of P elements
onto an arbitrary-length "background" sequenece, where the background
sequence contains P parallel linear series.

E.g. to pick an extreme and unlikely example,

  [42, 99, 2, 20, 3, 30, 4, 40, ...]

has 2 patterns:

  BASE0 = 42, BASE1 = 2, STEP = 1
  BASE0 = 99, BASE1 = 20, STEP = 10

The more useful cases are degenerate versions of this general case.

As far as memory consumption goes: the number of patterns needed for a
fixed-length vector with 2*N elements is always at most N; in the worst
case, we simply interleave the first N elements with the second N elements.
The worst-case increase in footprint is therefore N trees for the steps.
In practice the footprint is usually smaller than it was before, since
most constants do have a pattern.

The patch below implements this for trees.  I have patches to use the
same style of encoding for CONST_VECTOR and vec_perm_indices, but the
tree one is probably easiest to read.

The patch only adds the representation.  Follow-on patches make more
use of it (and usually make things simpler; e.g. integer_zerop is no
longer a looping operation).

Does this look better?

Thanks,
Richard


2017-11-29  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
	* doc/generic.texi (VECTOR_CST): Describe new representation of
	vector constants.
	* tree.def (VECTOR_CST): Update comment to refer to generic.texi.
	* tree-core.h (tree_base): Add a vector_cst field to the u union.
	(tree_vector_pattern): New struct.
	(tree_vector): Replace elts array with a patterns array.
	* tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
	(VECTOR_CST_ELTS): Delete.
	(VECTOR_CST_ELT): Redefine using vector_cst_elt.
	(VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
	(VECTOR_CST_DUPLICATE_P, VECTOR_CST_SERIES_P, VECTOR_CST_BASE)
	(VECTOR_CST_STEP): Likewise.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS,
	VECTOR_CST_DUPLICATE_P and VECTOR_CST_SERIES_P as arguments.
	(build_vector): Declare an overload that takes a vector of
	tree_vector_patterns.
	(vector_cst_int_elt, vector_cst_elt): Declare.
	* tree.c (tree_code_size): Abort if passed VECTOR_CST.
	(tree_size): Update for new VECTOR_CST layout.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS,
	VECTOR_CST_DUPLICATE_P and VECTOR_CST_SERIES_P as arguments.
	(combine_patterns, compress_vector_patterns, vector_overflow_p)
	(vector_duplicate_p, vector_series_p): New functions.
	(build_vector): Add an overload that takes a vector of
	tree_vector_patterns.  Use it for the overload that takes
	a vector of elements.
	(vector_cst_int_elt, vector_cst_elt): New functions.
	(drop_tree_overflow): For VECTOR_CST, drop the overflow flags
	from the VECTOR_CST_BASEs.
	(check_vector_cst, test_vector_cst_patterns): New functions.
	(tree_c_tests): Call it.
	* lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
	VECTOR_CST fields.
	(hash_tree): Likewise.
	* tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
	(streamer_write_tree_header): Likewise.
	* tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
	(streamer_alloc_tree): Likewise.  Update call to make_vector.
	* fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.

gcc/lto/
	* lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.

Index: gcc/doc/generic.texi
===================================================================
*** gcc/doc/generic.texi	2017-11-29 11:07:59.961993930 +0000
--- gcc/doc/generic.texi	2017-11-29 11:08:00.183993912 +0000
*************** These nodes are used to represent comple
*** 1084,1093 ****
  imaginary parts respectively.
  
  @item VECTOR_CST
! These nodes are used to represent vector constants, whose parts are
! constant nodes.  Each individual constant node is either an integer or a
! double constant node.  The first operand is a @code{TREE_LIST} of the
! constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
  
  @item STRING_CST
  These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
--- 1084,1147 ----
  imaginary parts respectively.
  
  @item VECTOR_CST
! These nodes are used to represent vector constants.  Each vector
! constant @var{v} is treated as a specific instance of an arbitrary-length
! sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
! interleaved patterns.  Each pattern @var{i} has three parts:
! 
! @itemize @samp
! @item VECTOR_CST_BASE (@var{v}, @var{i}, 0)
! The vector element value for the first instance of the pattern.
! 
! @item VECTOR_CST_BASE (@var{v}, @var{i}, 1)
! The vector element value for the second instance of the pattern.
! 
! @item VECTOR_CST_STEP (@var{v}, @var{i})
! If the first and second instances of the pattern are @code{INTEGER_CST}s,
! this is the difference between each subsequent instance of the pattern
! and the previous instance.  In other cases it is null, which indicates
! that subsequent instances of the pattern have the same value as the
! second instance.
! 
! If @var{v} only needs two instances of a pattern, and if both instances
! are @code{INTEGER_CST}s, the step is the difference between them.
! 
! The addition of the step to get third and subsequent elements is always
! a wrapping operation: there is no undefined behavior or overflow.
! @end itemize
! 
! For example, the constant:
! @smallexample
! @{ 1, 5, 2, 6, 3, 7, 4, 8 @}
! @end smallexample
! is encoded using the interleaved patterns:
! @smallexample
! @{ 1, 2, 3, @dots{} @}
! @{ 5, 6, 7, @dots{} @}
! @end smallexample
! where:
! @smallexample
! wi::to_wide (VECTOR_CST_BASE (@var{v}, 0, 0)) == 1
! wi::to_wide (VECTOR_CST_BASE (@var{v}, 0, 1)) == 2
! wi::to_wide (VECTOR_CST_STEP (@var{v}, 0)) == 1
! wi::to_wide (VECTOR_CST_BASE (@var{v}, 1, 0)) == 5
! wi::to_wide (VECTOR_CST_BASE (@var{v}, 1, 1)) == 6
! wi::to_wide (VECTOR_CST_STEP (@var{v}, 1)) == 1
! @end smallexample
! 
! @samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply
! contains repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})}
! values.  In this case the two bases in each pattern are equal and
! the steps for integer vectors are zero.
! 
! @samp{VECTOR_CST_SERIES_P (@var{v})} is true if each pattern can
! be seen as a linear series, with @samp{VECTOR_CST_BASE (@var{v}, @var{i}, 0)}
! giving the start value and @samp{VECTOR_CST_STEP (@var{v}, @var{i}))}
! giving the step.
! 
! The utility function @code{vector_cst_elt} gives the value of an
! arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
! the same value as a @code{wide_int}.
  
  @item STRING_CST
  These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
Index: gcc/tree.def
===================================================================
*** gcc/tree.def	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree.def	2017-11-29 11:08:00.187993912 +0000
*************** DEFTREECODE (FIXED_CST, "fixed_cst", tcc
*** 301,307 ****
     whose contents are other constant nodes.  */
  DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
  
! /* Contents are in VECTOR_CST_ELTS field.  */
  DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
  
  /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
--- 301,307 ----
     whose contents are other constant nodes.  */
  DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
  
! /* See generic.texi for details.  */
  DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
  
  /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
Index: gcc/tree-core.h
===================================================================
*** gcc/tree-core.h	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree-core.h	2017-11-29 11:08:00.185993912 +0000
*************** struct GTY(()) tree_base {
*** 976,983 ****
      /* VEC length.  This field is only used with TREE_VEC.  */
      int length;
  
!     /* Number of elements.  This field is only used with VECTOR_CST.  */
!     unsigned int nelts;
  
      /* SSA version number.  This field is only used with SSA_NAME.  */
      unsigned int version;
--- 976,995 ----
      /* VEC length.  This field is only used with TREE_VEC.  */
      int length;
  
!     /* This field is only used with VECTOR_CST.  */
!     struct {
!       /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
!       unsigned int log2_npatterns : 16;
! 
!       /* The value of VECTOR_CST_DUPLICATE_P.  */
!       unsigned int duplicate_p : 1;
! 
!       /* The value of VECTOR_CST_SERIES_P.  */
!       unsigned int series_p : 1;
! 
!       /* For future expansion.  */
!       unsigned int unused : 14;
!     } vector_cst;
  
      /* SSA version number.  This field is only used with SSA_NAME.  */
      unsigned int version;
*************** struct GTY(()) tree_complex {
*** 1331,1339 ****
    tree imag;
  };
  
  struct GTY(()) tree_vector {
    struct tree_typed typed;
!   tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
  };
  
  struct GTY(()) tree_identifier {
--- 1343,1363 ----
    tree imag;
  };
  
+ /* One pattern in a VECTOR_CST.  Instance I of the pattern has the value:
+ 
+       I == 0 ? BASE[0] : BASE[1] + (I - 1) * STEP
+ 
+    The STEP is nonnull iff BASE[0] and BASE[1] are INTEGER_CSTs;
+    in other cases the step is implicitly 0.  */
+ struct GTY(()) tree_vector_pattern {
+   tree base[2];
+   tree step;
+ };
+ 
  struct GTY(()) tree_vector {
    struct tree_typed typed;
!   tree_vector_pattern
!     GTY ((length ("VECTOR_CST_NPATTERNS ((tree) &%h)"))) patterns[1];
  };
  
  struct GTY(()) tree_identifier {
Index: gcc/tree.h
===================================================================
*** gcc/tree.h	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree.h	2017-11-29 11:08:00.188993912 +0000
*************** #define TREE_STRING_POINTER(NODE) \
*** 1012,1021 ****
  #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
  #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
  
! /* In a VECTOR_CST node.  */
! #define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
! #define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
! #define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
  
  /* Define fields and accessors for some special-purpose tree nodes.  */
  
--- 1012,1033 ----
  #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
  #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
  
! /* In a VECTOR_CST node.  See generic.texi for details.  */
! #define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
! #define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
! 
! #define VECTOR_CST_LOG2_NPATTERNS(NODE) \
!   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
! #define VECTOR_CST_NPATTERNS(NODE) \
!   (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
! #define VECTOR_CST_DUPLICATE_P(NODE) \
!   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.duplicate_p)
! #define VECTOR_CST_SERIES_P(NODE) \
!   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.series_p)
! #define VECTOR_CST_BASE(NODE, ELT, SUBELT) \
!   (VECTOR_CST_CHECK (NODE)->vector.patterns[ELT].base[SUBELT])
! #define VECTOR_CST_STEP(NODE, ELT) \
!   (VECTOR_CST_CHECK (NODE)->vector.patterns[ELT].step)
  
  /* Define fields and accessors for some special-purpose tree nodes.  */
  
*************** extern tree force_fit_type (tree, const
*** 4024,4030 ****
  extern tree build_int_cst (tree, HOST_WIDE_INT);
  extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
  extern tree build_int_cst_type (tree, HOST_WIDE_INT);
! extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
  extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
  extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
  extern tree build_vector_from_val (tree, tree);
--- 4036,4043 ----
  extern tree build_int_cst (tree, HOST_WIDE_INT);
  extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
  extern tree build_int_cst_type (tree, HOST_WIDE_INT);
! extern tree make_vector (unsigned, bool, bool CXX_MEM_STAT_INFO);
! extern tree build_vector (tree, vec<tree_vector_pattern> CXX_MEM_STAT_INFO);
  extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
  extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
  extern tree build_vector_from_val (tree, tree);
*************** extern tree first_field (const_tree);
*** 4271,4276 ****
--- 4284,4292 ----
  
  extern bool initializer_zerop (const_tree);
  
+ extern wide_int vector_cst_int_elt (const_tree, unsigned int);
+ extern tree vector_cst_elt (const_tree, unsigned int);
+ 
  /* Given a vector VEC, return its first element if all elements are
     the same.  Otherwise return NULL_TREE.  */
  
Index: gcc/tree.c
===================================================================
*** gcc/tree.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree.c	2017-11-29 11:08:00.187993912 +0000
*************** tree_code_size (enum tree_code code)
*** 839,845 ****
  	case REAL_CST:		return sizeof (tree_real_cst);
  	case FIXED_CST:		return sizeof (tree_fixed_cst);
  	case COMPLEX_CST:	return sizeof (tree_complex);
! 	case VECTOR_CST:	return sizeof (tree_vector);
  	case STRING_CST:	gcc_unreachable ();
  	default:
  	  gcc_checking_assert (code >= NUM_TREE_CODES);
--- 839,845 ----
  	case REAL_CST:		return sizeof (tree_real_cst);
  	case FIXED_CST:		return sizeof (tree_fixed_cst);
  	case COMPLEX_CST:	return sizeof (tree_complex);
! 	case VECTOR_CST:	gcc_unreachable ();
  	case STRING_CST:	gcc_unreachable ();
  	default:
  	  gcc_checking_assert (code >= NUM_TREE_CODES);
*************** tree_size (const_tree node)
*** 899,905 ****
  
      case VECTOR_CST:
        return (sizeof (struct tree_vector)
! 	      + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
  
      case STRING_CST:
        return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
--- 899,906 ----
  
      case VECTOR_CST:
        return (sizeof (struct tree_vector)
! 	      + ((VECTOR_CST_NPATTERNS (node) - 1)
! 		 * sizeof (tree_vector_pattern)));
  
      case STRING_CST:
        return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
*************** cst_and_fits_in_hwi (const_tree x)
*** 1708,1720 ****
  	  && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
  }
  
! /* Build a newly constructed VECTOR_CST node of length LEN.  */
  
  tree
! make_vector (unsigned len MEM_STAT_DECL)
  {
    tree t;
!   unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
  
    record_node_allocation_statistics (VECTOR_CST, length);
  
--- 1709,1726 ----
  	  && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
  }
  
! /* Build a newly constructed VECTOR_CST with the given values of
!    (VECTOR_CST_)LOG2_NPATTERNS, (VECTOR_CST_)DUPLICATE_P amd
!    (VECTOR_CST_)SERIES_P.  */
  
  tree
! make_vector (unsigned log2_npatterns, bool duplicate_p,
! 	     bool series_p MEM_STAT_DECL)
  {
    tree t;
!   unsigned npatterns = 1 << log2_npatterns;
!   unsigned length = (sizeof (struct tree_vector)
! 		     + (npatterns - 1) * sizeof (tree_vector_pattern));
  
    record_node_allocation_statistics (VECTOR_CST, length);
  
*************** make_vector (unsigned len MEM_STAT_DECL)
*** 1722,1764 ****
  
    TREE_SET_CODE (t, VECTOR_CST);
    TREE_CONSTANT (t) = 1;
!   VECTOR_CST_NELTS (t) = len;
  
    return t;
  }
  
! /* Return a new VECTOR_CST node whose type is TYPE and whose values
!    are given by VALS.  */
  
! tree
! build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
  {
!   unsigned int nelts = vals.length ();
!   gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
!   int over = 0;
!   unsigned cnt = 0;
!   tree v = make_vector (nelts);
!   TREE_TYPE (v) = type;
  
!   /* Iterate through elements and check for overflow.  */
!   for (cnt = 0; cnt < nelts; ++cnt)
      {
!       tree value = vals[cnt];
  
!       VECTOR_CST_ELT (v, cnt) = value;
  
!       /* Don't crash if we get an address constant.  */
!       if (!CONSTANT_CLASS_P (value))
! 	continue;
  
!       over |= TREE_OVERFLOW (value);
      }
  
!   TREE_OVERFLOW (v) = over;
    return v;
  }
  
  /* Return a new VECTOR_CST node whose type is TYPE and whose values
     are extracted from V, a vector of CONSTRUCTOR_ELT.  */
  
  tree
--- 1728,1949 ----
  
    TREE_SET_CODE (t, VECTOR_CST);
    TREE_CONSTANT (t) = 1;
!   VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
!   VECTOR_CST_DUPLICATE_P (t) = duplicate_p;
!   VECTOR_CST_SERIES_P (t) = series_p;
  
    return t;
  }
  
! /* Try to represent the interleaving of SRC1 and SRC2 as a single
!    pattern.  Return true on success, adding the pattern to DEST.
!    FIRST_P is true if all elements are represented by the bases,
!    with no values determined by the steps.  */
  
! static bool
! combine_patterns (vec<tree_vector_pattern> *dest,
! 		  const tree_vector_pattern *src1,
! 		  const tree_vector_pattern *src2,
! 		  bool first_p)
  {
!   /* Any new pattern would represent:
! 
!      { src1->base[0], vals[0], vals[1], vals[2], ... }
! 
!      where ... exists only if !FIRST_P.  */
!   tree vals[3] = { src2->base[0], src1->base[1], src2->base[1] };
! 
!   /* See whether any values overflowed.  */
!   tree overflowed = NULL_TREE;
!   for (unsigned int i = 0; i < 3; ++i)
!     if (CONSTANT_CLASS_P (vals[i]) && TREE_OVERFLOW (vals[i]))
!       {
! 	overflowed = vals[i];
! 	break;
!       }
! 
!   bool zero_step1_p = (!src1->step || wi::to_wide (src1->step) == 0);
!   bool zero_step2_p = (!src2->step || wi::to_wide (src1->step) == 0);
  
!   /* Option 1: use VALS[0] as the second base without a step.  */
!   if ((first_p || (zero_step1_p && zero_step2_p))
!       && operand_equal_p (vals[0], vals[1], 0)
!       && operand_equal_p (vals[1], vals[2], 0))
!     {
!       tree_vector_pattern pattern (*src1);
!       /* Set TREE_OVERFLOW on the result if it was set for any of
! 	 the values being merged.  */
!       if (overflowed)
! 	pattern.base[1] = overflowed;
!       if (!zero_step1_p)
! 	pattern.step = build_zero_cst (TREE_TYPE (pattern.step));
!       dest->quick_push (pattern);
!       return true;
!     }
! 
!   /* Option 2: use VALS[0] as the second base and handle the rest
!      via a step.  In this case we must have nonnull steps (which
!      indicates that using a step is valid).  */
!   if (!src1->step || !src2->step)
!     return false;
! 
!   /* Check that VALS has a consistent step.  */
!   wide_int step = wi::to_wide (vals[1]) - wi::to_wide (vals[0]);
!   if (step != wi::to_wide (vals[2]) - wi::to_wide (vals[1]))
!     return false;
! 
!   /* Check that that step is in turn consistent with any existing one.  */
!   if (!first_p)
      {
!       wide_int double_step = wi::lshift (step, 1);
!       if (double_step != wi::to_wide (src1->step)
! 	  || double_step != wi::to_wide (src2->step))
! 	return false;
!     }
  
!   /* The combination is valid.  */
!   tree_vector_pattern pattern (*src1);
!   /* Set TREE_OVERFLOW on the result if it was set for any of
!      the values being merged.  */
!   if (!overflowed || overflowed == vals[0])
!     pattern.base[1] = vals[0];
!   else
!     pattern.base[1] = force_fit_type (TREE_TYPE (vals[0]),
! 				      wi::to_wide (vals[0]), 0, true);
!   pattern.step = wide_int_to_tree (TREE_TYPE (vals[0]), step);
!   dest->quick_push (pattern);
!   return true;
! }
  
! /* PATTERNS represents a constant of type TYPE.  Compress it into the
!    canonical form, returning the new vector of patterns.  Use CUR and NEXT
!    as temporary workspace.  */
! 
! static vec<tree_vector_pattern>
! compress_vector_patterns (vec<tree_vector_pattern> *cur,
! 			  vec<tree_vector_pattern> *next,
! 			  tree type, vec<tree_vector_pattern> patterns)
! {
!   while (patterns.length () > 1)
!     {
!       unsigned int npatterns = patterns.length ();
!       gcc_assert ((npatterns & 1) == 0);
!       unsigned int step = npatterns / 2;
!       cur->truncate (0);
!       cur->reserve (step);
!       bool first_p = npatterns * 2 == TYPE_VECTOR_SUBPARTS (type);
!       for (unsigned int i = 0; i < step; ++i)
! 	if (!combine_patterns (cur, &patterns[i], &patterns[i + step],
! 			       first_p))
! 	  return patterns;
!       patterns = *cur;
!       std::swap (cur, next);
!     }
!   return patterns;
! }
! 
! /* Return true if PATTERNS has an overflow bit set.  */
! 
! static bool
! vector_overflow_p (vec<tree_vector_pattern> patterns)
! {
!   unsigned int i;
!   tree_vector_pattern *pattern;
!   FOR_EACH_VEC_ELT (patterns, i, pattern)
!     for (unsigned int j = 0; j < 2; ++j)
!       if (CONSTANT_CLASS_P (pattern->base[j])
! 	  && TREE_OVERFLOW (pattern->base[j]))
! 	return true;
!   return false;
! }
! 
! /* Return true if PATTERNS represents a duplicate operation.  */
  
! static bool
! vector_duplicate_p (vec<tree_vector_pattern> patterns)
! {
!   unsigned int i;
!   tree_vector_pattern *pattern;
!   FOR_EACH_VEC_ELT (patterns, i, pattern)
!     {
!       if (pattern->step && wi::to_wide (pattern->step) != 0)
! 	return false;
!       if (!operand_equal_p (pattern->base[0], pattern->base[1], 0))
! 	return false;
      }
+   return true;
+ }
+ 
+ /* Return true if PATTERNS represents a vector series.  */
+ 
+ static bool
+ vector_series_p (vec<tree_vector_pattern> patterns)
+ {
+   unsigned int i;
+   tree_vector_pattern *pattern;
+   FOR_EACH_VEC_ELT (patterns, i, pattern)
+     if (!pattern->step
+ 	|| (wi::to_wide (pattern->base[1]) - wi::to_wide (pattern->base[0])
+ 	    != wi::to_wide (pattern->step)))
+       return false;
+   return true;
+ }
+ 
+ /* Build a VECTOR_CST of type TYPE using the patterns in PATTERNS,
+    canonicalizing it first if necessary.  */
+ 
+ tree
+ build_vector (tree type, vec<tree_vector_pattern> patterns MEM_STAT_DECL)
+ {
+   auto_vec<tree_vector_pattern, 16> tmp1, tmp2;
+   patterns = compress_vector_patterns (&tmp1, &tmp2, type, patterns);
+   unsigned int npatterns = patterns.length ();
+ 
+   gcc_assert (pow2p_hwi (npatterns));
+   bool overflow_p = vector_overflow_p (patterns);
+   bool duplicate_p = vector_duplicate_p (patterns);
+   bool series_p = vector_series_p (patterns);
+   tree v = make_vector (exact_log2 (npatterns), duplicate_p, series_p);
+   TREE_TYPE (v) = type;
+   TREE_OVERFLOW (v) = overflow_p;
  
!   memcpy (v->vector.patterns, patterns.address (),
! 	  npatterns * sizeof (tree_vector_pattern));
    return v;
  }
  
  /* Return a new VECTOR_CST node whose type is TYPE and whose values
+    are given by ELTS.  */
+ 
+ tree
+ build_vector (tree type, vec<tree> elts MEM_STAT_DECL)
+ {
+   unsigned int nelts = elts.length ();
+   gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
+ 
+   gcc_assert (pow2p_hwi (nelts));
+   bool single_p = nelts == 1;
+   unsigned int npatterns = single_p ? 1 : nelts / 2;
+   auto_vec<tree_vector_pattern, 16> patterns (npatterns);
+   for (unsigned int i = 0; i < npatterns; ++i)
+     {
+       tree_vector_pattern pattern;
+       pattern.base[0] = elts[i];
+       pattern.base[1] = elts[single_p ? i : i + npatterns];
+       if (INTEGRAL_TYPE_P (TREE_TYPE (type))
+ 	  && TREE_CODE (pattern.base[0]) == INTEGER_CST
+ 	  && TREE_CODE (pattern.base[1]) == INTEGER_CST)
+ 	pattern.step = wide_int_to_tree (TREE_TYPE (type),
+ 					 wi::to_wide (pattern.base[1])
+ 					 - wi::to_wide (pattern.base[0]));
+       else
+ 	pattern.step = NULL_TREE;
+       patterns.quick_push (pattern);
+     }
+   return build_vector (type, patterns);
+ }
+ 
+ /* Return a new VECTOR_CST node whose type is TYPE and whose values
     are extracted from V, a vector of CONSTRUCTOR_ELT.  */
  
  tree
*************** build_opaque_vector_type (tree innertype
*** 10353,10358 ****
--- 10538,10581 ----
    return cand;
  }
  
+ /* Return the value of element I of VECTOR_CST T as a wide_int.  */
+ 
+ wide_int
+ vector_cst_int_elt (const_tree t, unsigned int i)
+ {
+   unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+   if (i < npatterns)
+     return wi::to_wide (VECTOR_CST_BASE (t, i, 0));
+ 
+   unsigned int pattern = i & (npatterns - 1);
+   tree base = VECTOR_CST_BASE (t, pattern, 1);
+   tree step = VECTOR_CST_STEP (t, pattern);
+   if (i < npatterns * 2 || !step || wi::to_wide (step) == 0)
+     return wi::to_wide (base);
+ 
+   unsigned int factor = (i >> VECTOR_CST_LOG2_NPATTERNS (t)) - 1;
+   return wi::to_wide (base) + factor * wi::to_wide (step);
+ }
+ 
+ /* Return the value of element I of VECTOR_CST T.  */
+ 
+ tree
+ vector_cst_elt (const_tree t, unsigned int i)
+ {
+   unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+   if (i < npatterns)
+     return VECTOR_CST_BASE (t, i, 0);
+ 
+   unsigned int pattern = i & (npatterns - 1);
+   tree base = VECTOR_CST_BASE (t, pattern, 1);
+   tree step = VECTOR_CST_STEP (t, pattern);
+   if (i < npatterns * 2 || !step || wi::to_wide (step) == 0)
+     return base;
+ 
+   unsigned int factor = (i >> VECTOR_CST_LOG2_NPATTERNS (t)) - 1;
+   return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
+ 			   wi::to_wide (base) + factor * wi::to_wide (step));
+ }
  
  /* Given an initializer INIT, return TRUE if INIT is zero or some
     aggregate of zeros.  Otherwise return FALSE.  */
*************** drop_tree_overflow (tree t)
*** 12447,12461 ****
        if (TREE_OVERFLOW (TREE_IMAGPART (t)))
  	TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
      }
    if (TREE_CODE (t) == VECTOR_CST)
!     {
!       for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
! 	{
! 	  tree& elt = VECTOR_CST_ELT (t, i);
! 	  if (TREE_OVERFLOW (elt))
! 	    elt = drop_tree_overflow (elt);
  	}
!     }
    return t;
  }
  
--- 12670,12686 ----
        if (TREE_OVERFLOW (TREE_IMAGPART (t)))
  	TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
      }
+ 
    if (TREE_CODE (t) == VECTOR_CST)
!     /* Only the base values can have an overflow bit set.  */
!     for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (t); ++i)
!       for (unsigned int j = 0; j < 2; ++j)
! 	{
! 	  tree *elt = &VECTOR_CST_BASE (t, i, j);
! 	  if (TREE_OVERFLOW (*elt))
! 	    *elt = drop_tree_overflow (*elt);
  	}
! 
    return t;
  }
  
*************** test_labels ()
*** 13954,13959 ****
--- 14179,14350 ----
    ASSERT_FALSE (FORCED_LABEL (label_decl));
  }
  
+ /* Check that VECTOR_CST Y contains the elements in X.  */
+ 
+ static void
+ check_vector_cst (vec<tree> x, tree y)
+ {
+   for (unsigned int i = 0; i < x.length (); ++i)
+     ASSERT_EQ (wi::to_wide (x[i]), wi::to_wide (vector_cst_elt (y, i)));
+ }
+ 
+ /* Test the creation of VECTOR_CSTs.  */
+ 
+ static void
+ test_vector_cst_patterns ()
+ {
+   auto_vec<tree, 8> elements (8);
+   elements.quick_grow (8);
+   tree element_type = build_nonstandard_integer_type (16, true);
+   tree vector_type = build_vector_type (element_type, 8);
+ 
+   /* Test a simple linear series with a base of 0 and a step of 1:
+      { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
+   for (unsigned int i = 0; i < 8; ++i)
+     elements[i] = build_int_cst (element_type, i);
+   tree series_0_1 = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (series_0_1), 1);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_BASE (series_0_1, 0, 0)));
+   ASSERT_TRUE (integer_onep (VECTOR_CST_BASE (series_0_1, 0, 1)));
+   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (series_0_1, 0)));
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (series_0_1));
+   ASSERT_TRUE (VECTOR_CST_SERIES_P (series_0_1));
+   check_vector_cst (elements, series_0_1);
+ 
+   /* Try the same with the first element replaced by 100:
+      { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
+   elements[0] = build_int_cst (element_type, 100);
+   tree jump_series = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (jump_series), 1);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_series, 0, 0)), 100);
+   ASSERT_TRUE (integer_onep (VECTOR_CST_BASE (jump_series, 0, 1)));
+   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (jump_series, 0)));
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (jump_series));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (jump_series));
+   check_vector_cst (elements, jump_series);
+ 
+   /* Try a series that wraps around.
+      { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
+   for (unsigned int i = 1; i < 8; ++i)
+     elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
+   tree wrap_series = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (wrap_series), 1);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (wrap_series, 0, 0)), 100);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (wrap_series, 0, 1)), 65531);
+   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (wrap_series, 0)));
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (wrap_series));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (wrap_series));
+   check_vector_cst (elements, wrap_series);
+ 
+   /* Try a downward series:
+      { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
+   for (unsigned int i = 1; i < 8; ++i)
+     elements[i] = build_int_cst (element_type, 80 - i);
+   tree down_series = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (down_series), 1);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (down_series, 0, 0)), 100);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (down_series, 0, 1)), 79);
+   ASSERT_TRUE (integer_minus_onep (VECTOR_CST_STEP (down_series, 0)));
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (down_series));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (down_series));
+   check_vector_cst (elements, down_series);
+ 
+   /* Try two interleaved series with different bases and steps:
+      { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
+   elements[1] = build_int_cst (element_type, 53);
+   for (unsigned int i = 2; i < 8; i += 2)
+     {
+       elements[i] = build_int_cst (element_type, 70 - i * 2);
+       elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
+     }
+   tree mixed_series = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (mixed_series), 2);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 0, 0)), 100);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 0, 1)), 66);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed_series, 0)), -4);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 1, 0)), 53);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 1, 1)), 206);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed_series, 1)), 6);
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (mixed_series));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (mixed_series));
+   check_vector_cst (elements, mixed_series);
+ 
+   /* Try a duplicated value:
+      { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
+   for (unsigned int i = 1; i < 8; ++i)
+     elements[i] = elements[0];
+   tree dup1 = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (dup1), 1);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup1, 0, 0)), 100);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup1, 0, 1)), 100);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup1, 0)));
+   ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (dup1));
+   ASSERT_TRUE (VECTOR_CST_SERIES_P (dup1));
+   check_vector_cst (elements, dup1);
+ 
+   /* Try an interleaved duplicated value:
+      { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
+   elements[1] = build_int_cst (element_type, 55);
+   for (unsigned int i = 2; i < 8; ++i)
+     elements[i] = elements[i - 2];
+   tree dup2 = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (dup2), 2);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 0, 0)), 100);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 0, 1)), 100);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup2, 0)));
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 1, 0)), 55);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 1, 1)), 55);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup2, 1)));
+   ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (dup2));
+   ASSERT_TRUE (VECTOR_CST_SERIES_P (dup2));
+   check_vector_cst (elements, dup2);
+ 
+   /* Try a duplicated value with 2 exceptions
+      { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
+   elements[0] = build_int_cst (element_type, 41);
+   elements[1] = build_int_cst (element_type, 97);
+   tree jump_dup2 = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (jump_dup2), 2);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 0, 0)), 41);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 0, 1)), 100);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (jump_dup2, 0)));
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 1, 0)), 97);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 1, 1)), 55);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (jump_dup2, 1)));
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (jump_dup2));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (jump_dup2));
+   check_vector_cst (elements, jump_dup2);
+ 
+   /* Try with and without a step
+      { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
+   for (unsigned int i = 3; i < 8; i += 2)
+     elements[i] = build_int_cst (element_type, i * 7);
+   tree mixed2 = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (mixed2), 2);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 0, 0)), 41);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 0, 1)), 100);
+   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (mixed2, 0)));
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 1, 0)), 97);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 1, 1)), 21);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed2, 1)), 14);
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (mixed2));
+   ASSERT_FALSE (VECTOR_CST_SERIES_P (mixed2));
+   check_vector_cst (elements, mixed2);
+ 
+   /* Try a fully-general constant:
+      { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
+   elements[5] = build_int_cst (element_type, 9990);
+   tree general = build_vector (vector_type, elements);
+   ASSERT_EQ (VECTOR_CST_NPATTERNS (general), 4);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 0)), 59);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 1)), 9893);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 2)), 0);
+   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 3)), 28);
+   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (general));
+   ASSERT_TRUE (VECTOR_CST_SERIES_P (general));
+   check_vector_cst (elements, general);
+ }
+ 
  /* Run all of the selftests within this file.  */
  
  void
*************** tree_c_tests ()
*** 13962,13967 ****
--- 14353,14359 ----
    test_integer_constants ();
    test_identifiers ();
    test_labels ();
+   test_vector_cst_patterns ();
  }
  
  } // namespace selftest
Index: gcc/lto-streamer-out.c
===================================================================
*** gcc/lto-streamer-out.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/lto-streamer-out.c	2017-11-29 11:08:00.185993912 +0000
*************** #define DFS_follow_tree_edge(DEST) \
*** 747,754 ****
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
!       for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
! 	DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
--- 747,758 ----
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
!       for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
! 	{
! 	  for (unsigned int j = 0; j < 2; ++j)
! 	    DFS_follow_tree_edge (VECTOR_CST_BASE (expr, i, j));
! 	  DFS_follow_tree_edge (VECTOR_CST_STEP (expr, i));
! 	}
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
*************** #define visit(SIBLING) \
*** 1195,1202 ****
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
!     for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
!       visit (VECTOR_CST_ELT (t, i));
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
      {
--- 1199,1210 ----
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
!     for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (t); ++i)
!       {
! 	for (unsigned int j = 0; j < 2; ++j)
! 	  visit (VECTOR_CST_BASE (t, i, j));
! 	visit (VECTOR_CST_STEP (t, i));
!       }
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
      {
Index: gcc/tree-streamer-out.c
===================================================================
*** gcc/tree-streamer-out.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree-streamer-out.c	2017-11-29 11:08:00.186993912 +0000
*************** write_ts_common_tree_pointers (struct ou
*** 533,543 ****
  static void
  write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
  {
-   unsigned i;
    /* Note that the number of elements for EXPR has already been emitted
       in EXPR's header (see streamer_write_tree_header).  */
!   for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
!     stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
  }
  
  
--- 533,546 ----
  static void
  write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
  {
    /* Note that the number of elements for EXPR has already been emitted
       in EXPR's header (see streamer_write_tree_header).  */
!   for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
!     {
!       for (unsigned int j = 0; j < 2; ++j)
! 	stream_write_tree (ob, VECTOR_CST_BASE (expr, i, j), ref_p);
!       stream_write_tree (ob, VECTOR_CST_STEP (expr, i), ref_p);
!     }
  }
  
  
*************** streamer_write_tree_header (struct outpu
*** 960,966 ****
    else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
      write_identifier (ob, ob->main_stream, expr);
    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
!     streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
    else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
      streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
--- 963,975 ----
    else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
      write_identifier (ob, ob->main_stream, expr);
    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
!     {
!       bitpack_d bp = bitpack_create (ob->main_stream);
!       bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 16);
!       bp_pack_value (&bp, VECTOR_CST_DUPLICATE_P (expr), 1);
!       bp_pack_value (&bp, VECTOR_CST_SERIES_P (expr), 1);
!       streamer_write_bitpack (&bp);
!     }
    else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
      streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
Index: gcc/tree-streamer-in.c
===================================================================
*** gcc/tree-streamer-in.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/tree-streamer-in.c	2017-11-29 11:08:00.186993912 +0000
*************** streamer_alloc_tree (struct lto_input_bl
*** 592,599 ****
      }
    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
!       HOST_WIDE_INT len = streamer_read_hwi (ib);
!       result = make_vector (len);
      }
    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
      {
--- 592,602 ----
      }
    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
!       bitpack_d bp = streamer_read_bitpack (ib);
!       unsigned int log2_npatterns = bp_unpack_value (&bp, 16);
!       unsigned int duplicate_p = bp_unpack_value (&bp, 1);
!       unsigned int series_p = bp_unpack_value (&bp, 1);
!       result = make_vector (log2_npatterns, duplicate_p, series_p);
      }
    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
      {
*************** lto_input_ts_common_tree_pointers (struc
*** 650,658 ****
  lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
  				   struct data_in *data_in, tree expr)
  {
!   unsigned i;
!   for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
!     VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
  }
  
  
--- 653,664 ----
  lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
  				   struct data_in *data_in, tree expr)
  {
!   for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
!     {
!       for (unsigned int j = 0; j < 2; ++j)
! 	VECTOR_CST_BASE (expr, i, j) = stream_read_tree (ib, data_in);
!       VECTOR_CST_STEP (expr, i) = stream_read_tree (ib, data_in);
!     }
  }
  
  
Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/fold-const.c	2017-11-29 11:08:00.184993912 +0000
*************** fold_ternary_loc (location_t loc, enum t
*** 11610,11618 ****
  		  unsigned int nelts = VECTOR_CST_NELTS (arg0);
  		  auto_vec<tree, 32> elts (nelts);
  		  elts.quick_grow (nelts);
! 		  memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
! 			  sizeof (tree) * nelts);
! 		  elts[k] = arg1;
  		  return build_vector (type, elts);
  		}
  	    }
--- 11610,11617 ----
  		  unsigned int nelts = VECTOR_CST_NELTS (arg0);
  		  auto_vec<tree, 32> elts (nelts);
  		  elts.quick_grow (nelts);
! 		  for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
! 		    elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
  		  return build_vector (type, elts);
  		}
  	    }
Index: gcc/lto/lto.c
===================================================================
*** gcc/lto/lto.c	2017-11-29 11:07:59.961993930 +0000
--- gcc/lto/lto.c	2017-11-29 11:08:00.185993912 +0000
*************** #define compare_values(X) \
*** 1065,1070 ****
--- 1065,1077 ----
  			TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
        return false;
  
+   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
+     {
+       compare_values (VECTOR_CST_LOG2_NPATTERNS);
+       compare_values (VECTOR_CST_DUPLICATE_P);
+       compare_values (VECTOR_CST_SERIES_P);
+     }
+ 
    if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
      {
        compare_values (DECL_MODE);
*************** #define compare_tree_edges(E1, E2) \
*** 1281,1291 ****
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
-       unsigned i;
        /* Note that the number of elements for EXPR has already been emitted
  	 in EXPR's header (see streamer_write_tree_header).  */
!       for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
! 	compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
--- 1288,1303 ----
  
    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
      {
        /* Note that the number of elements for EXPR has already been emitted
  	 in EXPR's header (see streamer_write_tree_header).  */
!       for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (t1); ++i)
! 	{
! 	  for (unsigned int j = 0; j < 2; ++j)
! 	    compare_tree_edges (VECTOR_CST_BASE (t1, i, j),
! 				VECTOR_CST_BASE (t2, i, j));
! 	  compare_tree_edges (VECTOR_CST_STEP (t1, i),
! 			      VECTOR_CST_STEP (t2, i));
! 	}
      }
  
    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-29 12:14 RFC: Variable-length VECTOR_CSTs Richard Sandiford
@ 2017-11-29 15:11 ` David Malcolm
  2017-12-06 14:40   ` Richard Sandiford
  2017-11-29 15:56 ` RFC: Variable-length VECTOR_CSTs Richard Biener
  1 sibling, 1 reply; 24+ messages in thread
From: David Malcolm @ 2017-11-29 15:11 UTC (permalink / raw)
  To: Richard Sandiford, gcc-patches

On Wed, 2017-11-29 at 11:57 +0000, Richard Sandiford wrote:

[...]

I can't really comment on the representation ideas, but I'm always
happy to see new selftests...

*************** test_labels ()
> *** 13954,13959 ****
> --- 14179,14350 ----
>     ASSERT_FALSE (FORCED_LABEL (label_decl));
>   }
>   
> + /* Check that VECTOR_CST Y contains the elements in X.  */
> + 
> + static void
> + check_vector_cst (vec<tree> x, tree y)
> + {
> +   for (unsigned int i = 0; i < x.length (); ++i)
> +     ASSERT_EQ (wi::to_wide (x[i]), wi::to_wide (vector_cst_elt (y,
> i)));
> + }

...a couple of nits/suggestions:

(a) How about renaming "x" to "expected"?  Maybe rename "y" to
"actual"?  (to better document the sense of the test).

(b) At first glance, I wondered if this routine should also have
something like:

  ASSERT_EQ (expected.length (), VECTOR_CST_NELTS (actual));

Though that seems to come from the vector type, and it's always 8 in
these examples, so I'm not sure.

 
Hope this is constructive
Dave

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-29 12:14 RFC: Variable-length VECTOR_CSTs Richard Sandiford
  2017-11-29 15:11 ` David Malcolm
@ 2017-11-29 15:56 ` Richard Biener
  2017-11-29 18:24   ` Richard Sandiford
  1 sibling, 1 reply; 24+ messages in thread
From: Richard Biener @ 2017-11-29 15:56 UTC (permalink / raw)
  To: GCC Patches, Richard Sandiford

On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> It was clear from the SVE reviews that people were unhappy with how
> "special" the variable-length case was.  One particular concern was
> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
> that would in turn lead to different representations of VEC_PERM_EXPRs
> with constant permute vectors, and difficulties in invoking
> vec_perm_const_ok.
>
> This is an RFC for a VECTOR_CST representation that treats each
> specific constant as one instance of an arbitrary-length sequence.
> The reprensentation then extends to variable-length vectors in a
> natural way.
>
> As discussed on IRC, if a vector contains X*N elements for some
> constant N and integer X>0, the main features we need are:
>
> 1) the ability to represent a sequence that duplicates N values
>
>    This is useful for SLP invariants.
>
> 2) the ability to represent a sequence that starts with N values and
>    is followed by zeros
>
>    This is useful for the initial value in a double or SLP reduction
>
> 3) the ability to represent N interleaved series
>
>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>
> For (2), zero isn't necessarily special, since vectors used in an AND
> reduction might need to fill with ones.  Also, we might need up to N
> different fill values with mixed SLP operations; it isn't necessarily
> safe to assume that a single fill value will always be enough.
>
> The same goes for (3): there's no reason in principle why the
> steps in an SLP induction should all be the same (although they
> do need to be at the moment).  E.g. once we support SLP on:
>
>   for (unsigned int i = 0; i < n; i += 2)
>     {
>       x[i] += 4 + i;
>       x[i + 1] += 11 + i * 3;
>     }
>
> we'll need {[4, 14], +, [2, 6]}.
>
> So the idea is to represent vectors as P interleaved patterns of the form:
>
>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>
> where the STEP is always zero (actually null) for non-integer vectors.
> This is effectively projecting a "foreground" value of P elements
> onto an arbitrary-length "background" sequenece, where the background
> sequence contains P parallel linear series.
>
> E.g. to pick an extreme and unlikely example,
>
>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>
> has 2 patterns:
>
>   BASE0 = 42, BASE1 = 2, STEP = 1
>   BASE0 = 99, BASE1 = 20, STEP = 10
>
> The more useful cases are degenerate versions of this general case.
>
> As far as memory consumption goes: the number of patterns needed for a
> fixed-length vector with 2*N elements is always at most N; in the worst
> case, we simply interleave the first N elements with the second N elements.
> The worst-case increase in footprint is therefore N trees for the steps.
> In practice the footprint is usually smaller than it was before, since
> most constants do have a pattern.
>
> The patch below implements this for trees.  I have patches to use the
> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
> tree one is probably easiest to read.
>
> The patch only adds the representation.  Follow-on patches make more
> use of it (and usually make things simpler; e.g. integer_zerop is no
> longer a looping operation).
>
> Does this look better?

Yes, the overall design looks good.  I wonder why you chose to have
the number of patterns being a power of two?  I suppose this is
to have the same number of elements from all patterns in the final
vector (which is power-of-two sized)?  I wonder if there exists
a vector where say a three-pattern interleaving would be smaller
than a four-pattern one?

Given you add flags for various purposes would it make sense to
overload 'step' with a regular element to avoid the storage increase
in case step is unnecessary?  This makes it have three elements
which is of course awkward :/

Few more comments below.

Otherwise looks good to go!

Thanks for doing this.

> Thanks,
> Richard
>
>
> 2017-11-29  Richard Sandiford  <richard.sandiford@arm.com>
>
> gcc/
>         * doc/generic.texi (VECTOR_CST): Describe new representation of
>         vector constants.
>         * tree.def (VECTOR_CST): Update comment to refer to generic.texi.
>         * tree-core.h (tree_base): Add a vector_cst field to the u union.
>         (tree_vector_pattern): New struct.
>         (tree_vector): Replace elts array with a patterns array.
>         * tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
>         (VECTOR_CST_ELTS): Delete.
>         (VECTOR_CST_ELT): Redefine using vector_cst_elt.
>         (VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
>         (VECTOR_CST_DUPLICATE_P, VECTOR_CST_SERIES_P, VECTOR_CST_BASE)
>         (VECTOR_CST_STEP): Likewise.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS,
>         VECTOR_CST_DUPLICATE_P and VECTOR_CST_SERIES_P as arguments.
>         (build_vector): Declare an overload that takes a vector of
>         tree_vector_patterns.
>         (vector_cst_int_elt, vector_cst_elt): Declare.
>         * tree.c (tree_code_size): Abort if passed VECTOR_CST.
>         (tree_size): Update for new VECTOR_CST layout.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS,
>         VECTOR_CST_DUPLICATE_P and VECTOR_CST_SERIES_P as arguments.
>         (combine_patterns, compress_vector_patterns, vector_overflow_p)
>         (vector_duplicate_p, vector_series_p): New functions.
>         (build_vector): Add an overload that takes a vector of
>         tree_vector_patterns.  Use it for the overload that takes
>         a vector of elements.
>         (vector_cst_int_elt, vector_cst_elt): New functions.
>         (drop_tree_overflow): For VECTOR_CST, drop the overflow flags
>         from the VECTOR_CST_BASEs.
>         (check_vector_cst, test_vector_cst_patterns): New functions.
>         (tree_c_tests): Call it.
>         * lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
>         VECTOR_CST fields.
>         (hash_tree): Likewise.
>         * tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
>         (streamer_write_tree_header): Likewise.
>         * tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
>         (streamer_alloc_tree): Likewise.  Update call to make_vector.
>         * fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.
>
> gcc/lto/
>         * lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.
>
> Index: gcc/doc/generic.texi
> ===================================================================
> *** gcc/doc/generic.texi        2017-11-29 11:07:59.961993930 +0000
> --- gcc/doc/generic.texi        2017-11-29 11:08:00.183993912 +0000
> *************** These nodes are used to represent comple
> *** 1084,1093 ****
>   imaginary parts respectively.
>
>   @item VECTOR_CST
> ! These nodes are used to represent vector constants, whose parts are
> ! constant nodes.  Each individual constant node is either an integer or a
> ! double constant node.  The first operand is a @code{TREE_LIST} of the
> ! constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
>
>   @item STRING_CST
>   These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
> --- 1084,1147 ----
>   imaginary parts respectively.
>
>   @item VECTOR_CST
> ! These nodes are used to represent vector constants.  Each vector
> ! constant @var{v} is treated as a specific instance of an arbitrary-length
> ! sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
> ! interleaved patterns.  Each pattern @var{i} has three parts:
> !
> ! @itemize @samp
> ! @item VECTOR_CST_BASE (@var{v}, @var{i}, 0)
> ! The vector element value for the first instance of the pattern.
> !
> ! @item VECTOR_CST_BASE (@var{v}, @var{i}, 1)
> ! The vector element value for the second instance of the pattern.
> !
> ! @item VECTOR_CST_STEP (@var{v}, @var{i})
> ! If the first and second instances of the pattern are @code{INTEGER_CST}s,
> ! this is the difference between each subsequent instance of the pattern
> ! and the previous instance.  In other cases it is null, which indicates
> ! that subsequent instances of the pattern have the same value as the
> ! second instance.
> !
> ! If @var{v} only needs two instances of a pattern, and if both instances
> ! are @code{INTEGER_CST}s, the step is the difference between them.
> !
> ! The addition of the step to get third and subsequent elements is always
> ! a wrapping operation: there is no undefined behavior or overflow.
> ! @end itemize
> !
> ! For example, the constant:
> ! @smallexample
> ! @{ 1, 5, 2, 6, 3, 7, 4, 8 @}
> ! @end smallexample
> ! is encoded using the interleaved patterns:
> ! @smallexample
> ! @{ 1, 2, 3, @dots{} @}
> ! @{ 5, 6, 7, @dots{} @}
> ! @end smallexample
> ! where:
> ! @smallexample
> ! wi::to_wide (VECTOR_CST_BASE (@var{v}, 0, 0)) == 1
> ! wi::to_wide (VECTOR_CST_BASE (@var{v}, 0, 1)) == 2
> ! wi::to_wide (VECTOR_CST_STEP (@var{v}, 0)) == 1
> ! wi::to_wide (VECTOR_CST_BASE (@var{v}, 1, 0)) == 5
> ! wi::to_wide (VECTOR_CST_BASE (@var{v}, 1, 1)) == 6
> ! wi::to_wide (VECTOR_CST_STEP (@var{v}, 1)) == 1
> ! @end smallexample
> !
> ! @samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply
> ! contains repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})}
> ! values.  In this case the two bases in each pattern are equal and
> ! the steps for integer vectors are zero.
> !
> ! @samp{VECTOR_CST_SERIES_P (@var{v})} is true if each pattern can
> ! be seen as a linear series, with @samp{VECTOR_CST_BASE (@var{v}, @var{i}, 0)}
> ! giving the start value and @samp{VECTOR_CST_STEP (@var{v}, @var{i}))}
> ! giving the step.
> !
> ! The utility function @code{vector_cst_elt} gives the value of an
> ! arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
> ! the same value as a @code{wide_int}.
>
>   @item STRING_CST
>   These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
> Index: gcc/tree.def
> ===================================================================
> *** gcc/tree.def        2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree.def        2017-11-29 11:08:00.187993912 +0000
> *************** DEFTREECODE (FIXED_CST, "fixed_cst", tcc
> *** 301,307 ****
>      whose contents are other constant nodes.  */
>   DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
>
> ! /* Contents are in VECTOR_CST_ELTS field.  */
>   DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
>
>   /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
> --- 301,307 ----
>      whose contents are other constant nodes.  */
>   DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
>
> ! /* See generic.texi for details.  */
>   DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
>
>   /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
> Index: gcc/tree-core.h
> ===================================================================
> *** gcc/tree-core.h     2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree-core.h     2017-11-29 11:08:00.185993912 +0000
> *************** struct GTY(()) tree_base {
> *** 976,983 ****
>       /* VEC length.  This field is only used with TREE_VEC.  */
>       int length;
>
> !     /* Number of elements.  This field is only used with VECTOR_CST.  */
> !     unsigned int nelts;
>
>       /* SSA version number.  This field is only used with SSA_NAME.  */
>       unsigned int version;
> --- 976,995 ----
>       /* VEC length.  This field is only used with TREE_VEC.  */
>       int length;
>
> !     /* This field is only used with VECTOR_CST.  */
> !     struct {
> !       /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
> !       unsigned int log2_npatterns : 16;
> !
> !       /* The value of VECTOR_CST_DUPLICATE_P.  */
> !       unsigned int duplicate_p : 1;
> !
> !       /* The value of VECTOR_CST_SERIES_P.  */
> !       unsigned int series_p : 1;
> !
> !       /* For future expansion.  */
> !       unsigned int unused : 14;
> !     } vector_cst;
>
>       /* SSA version number.  This field is only used with SSA_NAME.  */
>       unsigned int version;
> *************** struct GTY(()) tree_complex {
> *** 1331,1339 ****
>     tree imag;
>   };
>
>   struct GTY(()) tree_vector {
>     struct tree_typed typed;
> !   tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
>   };
>
>   struct GTY(()) tree_identifier {
> --- 1343,1363 ----
>     tree imag;
>   };
>
> + /* One pattern in a VECTOR_CST.  Instance I of the pattern has the value:
> +
> +       I == 0 ? BASE[0] : BASE[1] + (I - 1) * STEP
> +
> +    The STEP is nonnull iff BASE[0] and BASE[1] are INTEGER_CSTs;
> +    in other cases the step is implicitly 0.  */
> + struct GTY(()) tree_vector_pattern {
> +   tree base[2];
> +   tree step;
> + };
> +
>   struct GTY(()) tree_vector {
>     struct tree_typed typed;
> !   tree_vector_pattern
> !     GTY ((length ("VECTOR_CST_NPATTERNS ((tree) &%h)"))) patterns[1];
>   };
>
>   struct GTY(()) tree_identifier {
> Index: gcc/tree.h
> ===================================================================
> *** gcc/tree.h  2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree.h  2017-11-29 11:08:00.188993912 +0000
> *************** #define TREE_STRING_POINTER(NODE) \
> *** 1012,1021 ****
>   #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
>   #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
>
> ! /* In a VECTOR_CST node.  */
> ! #define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
> ! #define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
> ! #define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
>
>   /* Define fields and accessors for some special-purpose tree nodes.  */
>
> --- 1012,1033 ----
>   #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
>   #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
>
> ! /* In a VECTOR_CST node.  See generic.texi for details.  */
> ! #define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
> ! #define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
> !
> ! #define VECTOR_CST_LOG2_NPATTERNS(NODE) \
> !   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
> ! #define VECTOR_CST_NPATTERNS(NODE) \
> !   (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
> ! #define VECTOR_CST_DUPLICATE_P(NODE) \
> !   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.duplicate_p)
> ! #define VECTOR_CST_SERIES_P(NODE) \
> !   (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.series_p)
> ! #define VECTOR_CST_BASE(NODE, ELT, SUBELT) \
> !   (VECTOR_CST_CHECK (NODE)->vector.patterns[ELT].base[SUBELT])
> ! #define VECTOR_CST_STEP(NODE, ELT) \
> !   (VECTOR_CST_CHECK (NODE)->vector.patterns[ELT].step)
>
>   /* Define fields and accessors for some special-purpose tree nodes.  */
>
> *************** extern tree force_fit_type (tree, const
> *** 4024,4030 ****
>   extern tree build_int_cst (tree, HOST_WIDE_INT);
>   extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
>   extern tree build_int_cst_type (tree, HOST_WIDE_INT);
> ! extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
>   extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
>   extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
>   extern tree build_vector_from_val (tree, tree);
> --- 4036,4043 ----
>   extern tree build_int_cst (tree, HOST_WIDE_INT);
>   extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
>   extern tree build_int_cst_type (tree, HOST_WIDE_INT);
> ! extern tree make_vector (unsigned, bool, bool CXX_MEM_STAT_INFO);
> ! extern tree build_vector (tree, vec<tree_vector_pattern> CXX_MEM_STAT_INFO);
>   extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
>   extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
>   extern tree build_vector_from_val (tree, tree);
> *************** extern tree first_field (const_tree);
> *** 4271,4276 ****
> --- 4284,4292 ----
>
>   extern bool initializer_zerop (const_tree);
>
> + extern wide_int vector_cst_int_elt (const_tree, unsigned int);
> + extern tree vector_cst_elt (const_tree, unsigned int);
> +
>   /* Given a vector VEC, return its first element if all elements are
>      the same.  Otherwise return NULL_TREE.  */
>
> Index: gcc/tree.c
> ===================================================================
> *** gcc/tree.c  2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree.c  2017-11-29 11:08:00.187993912 +0000
> *************** tree_code_size (enum tree_code code)
> *** 839,845 ****
>         case REAL_CST:          return sizeof (tree_real_cst);
>         case FIXED_CST:         return sizeof (tree_fixed_cst);
>         case COMPLEX_CST:       return sizeof (tree_complex);
> !       case VECTOR_CST:        return sizeof (tree_vector);
>         case STRING_CST:        gcc_unreachable ();
>         default:
>           gcc_checking_assert (code >= NUM_TREE_CODES);
> --- 839,845 ----
>         case REAL_CST:          return sizeof (tree_real_cst);
>         case FIXED_CST:         return sizeof (tree_fixed_cst);
>         case COMPLEX_CST:       return sizeof (tree_complex);
> !       case VECTOR_CST:        gcc_unreachable ();
>         case STRING_CST:        gcc_unreachable ();
>         default:
>           gcc_checking_assert (code >= NUM_TREE_CODES);
> *************** tree_size (const_tree node)
> *** 899,905 ****
>
>       case VECTOR_CST:
>         return (sizeof (struct tree_vector)
> !             + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
>
>       case STRING_CST:
>         return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
> --- 899,906 ----
>
>       case VECTOR_CST:
>         return (sizeof (struct tree_vector)
> !             + ((VECTOR_CST_NPATTERNS (node) - 1)
> !                * sizeof (tree_vector_pattern)));
>
>       case STRING_CST:
>         return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
> *************** cst_and_fits_in_hwi (const_tree x)
> *** 1708,1720 ****
>           && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
>   }
>
> ! /* Build a newly constructed VECTOR_CST node of length LEN.  */
>
>   tree
> ! make_vector (unsigned len MEM_STAT_DECL)
>   {
>     tree t;
> !   unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
>
>     record_node_allocation_statistics (VECTOR_CST, length);
>
> --- 1709,1726 ----
>           && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
>   }
>
> ! /* Build a newly constructed VECTOR_CST with the given values of
> !    (VECTOR_CST_)LOG2_NPATTERNS, (VECTOR_CST_)DUPLICATE_P amd
> !    (VECTOR_CST_)SERIES_P.  */
>
>   tree
> ! make_vector (unsigned log2_npatterns, bool duplicate_p,
> !            bool series_p MEM_STAT_DECL)
>   {
>     tree t;
> !   unsigned npatterns = 1 << log2_npatterns;
> !   unsigned length = (sizeof (struct tree_vector)
> !                    + (npatterns - 1) * sizeof (tree_vector_pattern));
>
>     record_node_allocation_statistics (VECTOR_CST, length);
>
> *************** make_vector (unsigned len MEM_STAT_DECL)
> *** 1722,1764 ****
>
>     TREE_SET_CODE (t, VECTOR_CST);
>     TREE_CONSTANT (t) = 1;
> !   VECTOR_CST_NELTS (t) = len;
>
>     return t;
>   }
>
> ! /* Return a new VECTOR_CST node whose type is TYPE and whose values
> !    are given by VALS.  */
>
> ! tree
> ! build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
>   {
> !   unsigned int nelts = vals.length ();
> !   gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
> !   int over = 0;
> !   unsigned cnt = 0;
> !   tree v = make_vector (nelts);
> !   TREE_TYPE (v) = type;
>
> !   /* Iterate through elements and check for overflow.  */
> !   for (cnt = 0; cnt < nelts; ++cnt)
>       {
> !       tree value = vals[cnt];
>
> !       VECTOR_CST_ELT (v, cnt) = value;
>
> !       /* Don't crash if we get an address constant.  */
> !       if (!CONSTANT_CLASS_P (value))
> !       continue;
>
> !       over |= TREE_OVERFLOW (value);
>       }
>
> !   TREE_OVERFLOW (v) = over;
>     return v;
>   }
>
>   /* Return a new VECTOR_CST node whose type is TYPE and whose values
>      are extracted from V, a vector of CONSTRUCTOR_ELT.  */
>
>   tree
> --- 1728,1949 ----
>
>     TREE_SET_CODE (t, VECTOR_CST);
>     TREE_CONSTANT (t) = 1;
> !   VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
> !   VECTOR_CST_DUPLICATE_P (t) = duplicate_p;
> !   VECTOR_CST_SERIES_P (t) = series_p;
>
>     return t;
>   }
>
> ! /* Try to represent the interleaving of SRC1 and SRC2 as a single
> !    pattern.  Return true on success, adding the pattern to DEST.
> !    FIRST_P is true if all elements are represented by the bases,
> !    with no values determined by the steps.  */
>
> ! static bool
> ! combine_patterns (vec<tree_vector_pattern> *dest,
> !                 const tree_vector_pattern *src1,
> !                 const tree_vector_pattern *src2,
> !                 bool first_p)
>   {
> !   /* Any new pattern would represent:
> !
> !      { src1->base[0], vals[0], vals[1], vals[2], ... }
> !
> !      where ... exists only if !FIRST_P.  */
> !   tree vals[3] = { src2->base[0], src1->base[1], src2->base[1] };
> !
> !   /* See whether any values overflowed.  */
> !   tree overflowed = NULL_TREE;
> !   for (unsigned int i = 0; i < 3; ++i)
> !     if (CONSTANT_CLASS_P (vals[i]) && TREE_OVERFLOW (vals[i]))
> !       {
> !       overflowed = vals[i];
> !       break;
> !       }
> !
> !   bool zero_step1_p = (!src1->step || wi::to_wide (src1->step) == 0);
> !   bool zero_step2_p = (!src2->step || wi::to_wide (src1->step) == 0);
>
> !   /* Option 1: use VALS[0] as the second base without a step.  */
> !   if ((first_p || (zero_step1_p && zero_step2_p))
> !       && operand_equal_p (vals[0], vals[1], 0)
> !       && operand_equal_p (vals[1], vals[2], 0))
> !     {
> !       tree_vector_pattern pattern (*src1);
> !       /* Set TREE_OVERFLOW on the result if it was set for any of
> !        the values being merged.  */
> !       if (overflowed)
> !       pattern.base[1] = overflowed;
> !       if (!zero_step1_p)
> !       pattern.step = build_zero_cst (TREE_TYPE (pattern.step));
> !       dest->quick_push (pattern);
> !       return true;
> !     }
> !
> !   /* Option 2: use VALS[0] as the second base and handle the rest
> !      via a step.  In this case we must have nonnull steps (which
> !      indicates that using a step is valid).  */
> !   if (!src1->step || !src2->step)
> !     return false;
> !
> !   /* Check that VALS has a consistent step.  */
> !   wide_int step = wi::to_wide (vals[1]) - wi::to_wide (vals[0]);
> !   if (step != wi::to_wide (vals[2]) - wi::to_wide (vals[1]))
> !     return false;
> !
> !   /* Check that that step is in turn consistent with any existing one.  */
> !   if (!first_p)
>       {
> !       wide_int double_step = wi::lshift (step, 1);
> !       if (double_step != wi::to_wide (src1->step)
> !         || double_step != wi::to_wide (src2->step))
> !       return false;
> !     }
>
> !   /* The combination is valid.  */
> !   tree_vector_pattern pattern (*src1);
> !   /* Set TREE_OVERFLOW on the result if it was set for any of
> !      the values being merged.  */
> !   if (!overflowed || overflowed == vals[0])
> !     pattern.base[1] = vals[0];
> !   else
> !     pattern.base[1] = force_fit_type (TREE_TYPE (vals[0]),
> !                                     wi::to_wide (vals[0]), 0, true);
> !   pattern.step = wide_int_to_tree (TREE_TYPE (vals[0]), step);
> !   dest->quick_push (pattern);
> !   return true;
> ! }
>
> ! /* PATTERNS represents a constant of type TYPE.  Compress it into the
> !    canonical form, returning the new vector of patterns.  Use CUR and NEXT
> !    as temporary workspace.  */
> !
> ! static vec<tree_vector_pattern>
> ! compress_vector_patterns (vec<tree_vector_pattern> *cur,
> !                         vec<tree_vector_pattern> *next,
> !                         tree type, vec<tree_vector_pattern> patterns)
> ! {
> !   while (patterns.length () > 1)
> !     {
> !       unsigned int npatterns = patterns.length ();
> !       gcc_assert ((npatterns & 1) == 0);
> !       unsigned int step = npatterns / 2;
> !       cur->truncate (0);
> !       cur->reserve (step);
> !       bool first_p = npatterns * 2 == TYPE_VECTOR_SUBPARTS (type);
> !       for (unsigned int i = 0; i < step; ++i)
> !       if (!combine_patterns (cur, &patterns[i], &patterns[i + step],
> !                              first_p))
> !         return patterns;
> !       patterns = *cur;
> !       std::swap (cur, next);
> !     }
> !   return patterns;
> ! }

Wow, that looks complicated ;)  Which means it needs some more comments.

I wonder if avoiding all this for the special cases we like to handle by
providing vector_cst constructors for one and two pattern cases would
be best.  Or even more specialized, not exposing "patterns".

> ! /* Return true if PATTERNS has an overflow bit set.  */
> !
> ! static bool
> ! vector_overflow_p (vec<tree_vector_pattern> patterns)
> ! {
> !   unsigned int i;
> !   tree_vector_pattern *pattern;
> !   FOR_EACH_VEC_ELT (patterns, i, pattern)
> !     for (unsigned int j = 0; j < 2; ++j)
> !       if (CONSTANT_CLASS_P (pattern->base[j])
> !         && TREE_OVERFLOW (pattern->base[j]))
> !       return true;
> !   return false;
> ! }
> !
> ! /* Return true if PATTERNS represents a duplicate operation.  */
>
> ! static bool
> ! vector_duplicate_p (vec<tree_vector_pattern> patterns)
> ! {
> !   unsigned int i;
> !   tree_vector_pattern *pattern;
> !   FOR_EACH_VEC_ELT (patterns, i, pattern)
> !     {
> !       if (pattern->step && wi::to_wide (pattern->step) != 0)

I wonder if step == 0 should be canonicalized to NULL_TREE?

> !       return false;
> !       if (!operand_equal_p (pattern->base[0], pattern->base[1], 0))
> !       return false;
>       }
> +   return true;
> + }
> +
> + /* Return true if PATTERNS represents a vector series.  */
> +
> + static bool
> + vector_series_p (vec<tree_vector_pattern> patterns)
> + {
> +   unsigned int i;
> +   tree_vector_pattern *pattern;
> +   FOR_EACH_VEC_ELT (patterns, i, pattern)
> +     if (!pattern->step
> +       || (wi::to_wide (pattern->base[1]) - wi::to_wide (pattern->base[0])
> +           != wi::to_wide (pattern->step)))
> +       return false;
> +   return true;
> + }
> +
> + /* Build a VECTOR_CST of type TYPE using the patterns in PATTERNS,
> +    canonicalizing it first if necessary.  */
> +
> + tree
> + build_vector (tree type, vec<tree_vector_pattern> patterns MEM_STAT_DECL)
> + {
> +   auto_vec<tree_vector_pattern, 16> tmp1, tmp2;
> +   patterns = compress_vector_patterns (&tmp1, &tmp2, type, patterns);
> +   unsigned int npatterns = patterns.length ();
> +
> +   gcc_assert (pow2p_hwi (npatterns));
> +   bool overflow_p = vector_overflow_p (patterns);
> +   bool duplicate_p = vector_duplicate_p (patterns);
> +   bool series_p = vector_series_p (patterns);
> +   tree v = make_vector (exact_log2 (npatterns), duplicate_p, series_p);
> +   TREE_TYPE (v) = type;
> +   TREE_OVERFLOW (v) = overflow_p;
>
> !   memcpy (v->vector.patterns, patterns.address (),
> !         npatterns * sizeof (tree_vector_pattern));
>     return v;
>   }
>
>   /* Return a new VECTOR_CST node whose type is TYPE and whose values
> +    are given by ELTS.  */
> +
> + tree
> + build_vector (tree type, vec<tree> elts MEM_STAT_DECL)
> + {
> +   unsigned int nelts = elts.length ();
> +   gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
> +
> +   gcc_assert (pow2p_hwi (nelts));
> +   bool single_p = nelts == 1;
> +   unsigned int npatterns = single_p ? 1 : nelts / 2;
> +   auto_vec<tree_vector_pattern, 16> patterns (npatterns);
> +   for (unsigned int i = 0; i < npatterns; ++i)
> +     {
> +       tree_vector_pattern pattern;
> +       pattern.base[0] = elts[i];
> +       pattern.base[1] = elts[single_p ? i : i + npatterns];
> +       if (INTEGRAL_TYPE_P (TREE_TYPE (type))
> +         && TREE_CODE (pattern.base[0]) == INTEGER_CST
> +         && TREE_CODE (pattern.base[1]) == INTEGER_CST)
> +       pattern.step = wide_int_to_tree (TREE_TYPE (type),
> +                                        wi::to_wide (pattern.base[1])
> +                                        - wi::to_wide (pattern.base[0]));
> +       else
> +       pattern.step = NULL_TREE;

I wonder why given the simple representation with n/2 patterns
you don't use step == NULL_TREE unconditionally here.

Is there a target macro specifying the maximum target supported
vector length?  It might be nice to use that in the auto_vec <> size.

OTOH sometimes I really like

  tree_vector_pattern *p = XALLOCAVEC (npatterns);

more... (ok, __attribute__((vector_size(1048576))) might blow the stack...)

> +       patterns.quick_push (pattern);
> +     }
> +   return build_vector (type, patterns);
> + }
> +
> + /* Return a new VECTOR_CST node whose type is TYPE and whose values
>      are extracted from V, a vector of CONSTRUCTOR_ELT.  */
>
>   tree
> *************** build_opaque_vector_type (tree innertype
> *** 10353,10358 ****
> --- 10538,10581 ----
>     return cand;
>   }
>
> + /* Return the value of element I of VECTOR_CST T as a wide_int.  */
> +
> + wide_int
> + vector_cst_int_elt (const_tree t, unsigned int i)
> + {
> +   unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +   if (i < npatterns)
> +     return wi::to_wide (VECTOR_CST_BASE (t, i, 0));
> +
> +   unsigned int pattern = i & (npatterns - 1);
> +   tree base = VECTOR_CST_BASE (t, pattern, 1);
> +   tree step = VECTOR_CST_STEP (t, pattern);
> +   if (i < npatterns * 2 || !step || wi::to_wide (step) == 0)
> +     return wi::to_wide (base);
> +
> +   unsigned int factor = (i >> VECTOR_CST_LOG2_NPATTERNS (t)) - 1;
> +   return wi::to_wide (base) + factor * wi::to_wide (step);
> + }
> +
> + /* Return the value of element I of VECTOR_CST T.  */
> +
> + tree
> + vector_cst_elt (const_tree t, unsigned int i)
> + {
> +   unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +   if (i < npatterns)
> +     return VECTOR_CST_BASE (t, i, 0);
> +
> +   unsigned int pattern = i & (npatterns - 1);
> +   tree base = VECTOR_CST_BASE (t, pattern, 1);
> +   tree step = VECTOR_CST_STEP (t, pattern);
> +   if (i < npatterns * 2 || !step || wi::to_wide (step) == 0)
> +     return base;
> +
> +   unsigned int factor = (i >> VECTOR_CST_LOG2_NPATTERNS (t)) - 1;
> +   return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
> +                          wi::to_wide (base) + factor * wi::to_wide (step));
> + }
>
>   /* Given an initializer INIT, return TRUE if INIT is zero or some
>      aggregate of zeros.  Otherwise return FALSE.  */
> *************** drop_tree_overflow (tree t)
> *** 12447,12461 ****
>         if (TREE_OVERFLOW (TREE_IMAGPART (t)))
>         TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
>       }
>     if (TREE_CODE (t) == VECTOR_CST)
> !     {
> !       for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> !       {
> !         tree& elt = VECTOR_CST_ELT (t, i);
> !         if (TREE_OVERFLOW (elt))
> !           elt = drop_tree_overflow (elt);
>         }
> !     }
>     return t;
>   }
>
> --- 12670,12686 ----
>         if (TREE_OVERFLOW (TREE_IMAGPART (t)))
>         TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
>       }
> +
>     if (TREE_CODE (t) == VECTOR_CST)
> !     /* Only the base values can have an overflow bit set.  */
> !     for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (t); ++i)
> !       for (unsigned int j = 0; j < 2; ++j)
> !       {
> !         tree *elt = &VECTOR_CST_BASE (t, i, j);
> !         if (TREE_OVERFLOW (*elt))
> !           *elt = drop_tree_overflow (*elt);
>         }
> !
>     return t;
>   }
>
> *************** test_labels ()
> *** 13954,13959 ****
> --- 14179,14350 ----
>     ASSERT_FALSE (FORCED_LABEL (label_decl));
>   }
>
> + /* Check that VECTOR_CST Y contains the elements in X.  */
> +
> + static void
> + check_vector_cst (vec<tree> x, tree y)
> + {
> +   for (unsigned int i = 0; i < x.length (); ++i)
> +     ASSERT_EQ (wi::to_wide (x[i]), wi::to_wide (vector_cst_elt (y, i)));
> + }
> +
> + /* Test the creation of VECTOR_CSTs.  */
> +
> + static void
> + test_vector_cst_patterns ()
> + {
> +   auto_vec<tree, 8> elements (8);
> +   elements.quick_grow (8);
> +   tree element_type = build_nonstandard_integer_type (16, true);
> +   tree vector_type = build_vector_type (element_type, 8);
> +
> +   /* Test a simple linear series with a base of 0 and a step of 1:
> +      { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
> +   for (unsigned int i = 0; i < 8; ++i)
> +     elements[i] = build_int_cst (element_type, i);
> +   tree series_0_1 = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (series_0_1), 1);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_BASE (series_0_1, 0, 0)));
> +   ASSERT_TRUE (integer_onep (VECTOR_CST_BASE (series_0_1, 0, 1)));
> +   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (series_0_1, 0)));
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (series_0_1));
> +   ASSERT_TRUE (VECTOR_CST_SERIES_P (series_0_1));
> +   check_vector_cst (elements, series_0_1);
> +
> +   /* Try the same with the first element replaced by 100:
> +      { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
> +   elements[0] = build_int_cst (element_type, 100);
> +   tree jump_series = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (jump_series), 1);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_series, 0, 0)), 100);
> +   ASSERT_TRUE (integer_onep (VECTOR_CST_BASE (jump_series, 0, 1)));
> +   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (jump_series, 0)));
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (jump_series));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (jump_series));
> +   check_vector_cst (elements, jump_series);
> +
> +   /* Try a series that wraps around.
> +      { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
> +   for (unsigned int i = 1; i < 8; ++i)
> +     elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
> +   tree wrap_series = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (wrap_series), 1);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (wrap_series, 0, 0)), 100);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (wrap_series, 0, 1)), 65531);
> +   ASSERT_TRUE (integer_onep (VECTOR_CST_STEP (wrap_series, 0)));
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (wrap_series));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (wrap_series));
> +   check_vector_cst (elements, wrap_series);
> +
> +   /* Try a downward series:
> +      { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
> +   for (unsigned int i = 1; i < 8; ++i)
> +     elements[i] = build_int_cst (element_type, 80 - i);
> +   tree down_series = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (down_series), 1);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (down_series, 0, 0)), 100);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (down_series, 0, 1)), 79);
> +   ASSERT_TRUE (integer_minus_onep (VECTOR_CST_STEP (down_series, 0)));
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (down_series));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (down_series));
> +   check_vector_cst (elements, down_series);
> +
> +   /* Try two interleaved series with different bases and steps:
> +      { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
> +   elements[1] = build_int_cst (element_type, 53);
> +   for (unsigned int i = 2; i < 8; i += 2)
> +     {
> +       elements[i] = build_int_cst (element_type, 70 - i * 2);
> +       elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
> +     }
> +   tree mixed_series = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (mixed_series), 2);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 0, 0)), 100);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 0, 1)), 66);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed_series, 0)), -4);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 1, 0)), 53);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed_series, 1, 1)), 206);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed_series, 1)), 6);
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (mixed_series));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (mixed_series));
> +   check_vector_cst (elements, mixed_series);
> +
> +   /* Try a duplicated value:
> +      { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
> +   for (unsigned int i = 1; i < 8; ++i)
> +     elements[i] = elements[0];
> +   tree dup1 = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (dup1), 1);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup1, 0, 0)), 100);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup1, 0, 1)), 100);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup1, 0)));
> +   ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (dup1));
> +   ASSERT_TRUE (VECTOR_CST_SERIES_P (dup1));
> +   check_vector_cst (elements, dup1);
> +
> +   /* Try an interleaved duplicated value:
> +      { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
> +   elements[1] = build_int_cst (element_type, 55);
> +   for (unsigned int i = 2; i < 8; ++i)
> +     elements[i] = elements[i - 2];
> +   tree dup2 = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (dup2), 2);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 0, 0)), 100);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 0, 1)), 100);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup2, 0)));
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 1, 0)), 55);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (dup2, 1, 1)), 55);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (dup2, 1)));
> +   ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (dup2));
> +   ASSERT_TRUE (VECTOR_CST_SERIES_P (dup2));
> +   check_vector_cst (elements, dup2);
> +
> +   /* Try a duplicated value with 2 exceptions
> +      { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
> +   elements[0] = build_int_cst (element_type, 41);
> +   elements[1] = build_int_cst (element_type, 97);
> +   tree jump_dup2 = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (jump_dup2), 2);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 0, 0)), 41);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 0, 1)), 100);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (jump_dup2, 0)));
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 1, 0)), 97);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (jump_dup2, 1, 1)), 55);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (jump_dup2, 1)));
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (jump_dup2));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (jump_dup2));
> +   check_vector_cst (elements, jump_dup2);
> +
> +   /* Try with and without a step
> +      { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
> +   for (unsigned int i = 3; i < 8; i += 2)
> +     elements[i] = build_int_cst (element_type, i * 7);
> +   tree mixed2 = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (mixed2), 2);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 0, 0)), 41);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 0, 1)), 100);
> +   ASSERT_TRUE (integer_zerop (VECTOR_CST_STEP (mixed2, 0)));
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 1, 0)), 97);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_BASE (mixed2, 1, 1)), 21);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (mixed2, 1)), 14);
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (mixed2));
> +   ASSERT_FALSE (VECTOR_CST_SERIES_P (mixed2));
> +   check_vector_cst (elements, mixed2);
> +
> +   /* Try a fully-general constant:
> +      { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
> +   elements[5] = build_int_cst (element_type, 9990);
> +   tree general = build_vector (vector_type, elements);
> +   ASSERT_EQ (VECTOR_CST_NPATTERNS (general), 4);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 0)), 59);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 1)), 9893);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 2)), 0);
> +   ASSERT_EQ (wi::to_wide (VECTOR_CST_STEP (general, 3)), 28);
> +   ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (general));
> +   ASSERT_TRUE (VECTOR_CST_SERIES_P (general));
> +   check_vector_cst (elements, general);
> + }
> +
>   /* Run all of the selftests within this file.  */
>
>   void
> *************** tree_c_tests ()
> *** 13962,13967 ****
> --- 14353,14359 ----
>     test_integer_constants ();
>     test_identifiers ();
>     test_labels ();
> +   test_vector_cst_patterns ();
>   }
>
>   } // namespace selftest
> Index: gcc/lto-streamer-out.c
> ===================================================================
> *** gcc/lto-streamer-out.c      2017-11-29 11:07:59.961993930 +0000
> --- gcc/lto-streamer-out.c      2017-11-29 11:08:00.185993912 +0000
> *************** #define DFS_follow_tree_edge(DEST) \
> *** 747,754 ****
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
> !       for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> !       DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
> --- 747,758 ----
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
> !       for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
> !       {
> !         for (unsigned int j = 0; j < 2; ++j)
> !           DFS_follow_tree_edge (VECTOR_CST_BASE (expr, i, j));
> !         DFS_follow_tree_edge (VECTOR_CST_STEP (expr, i));
> !       }
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
> *************** #define visit(SIBLING) \
> *** 1195,1202 ****
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> !     for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> !       visit (VECTOR_CST_ELT (t, i));
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
>       {
> --- 1199,1210 ----
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> !     for (unsigned i = 0; i < VECTOR_CST_NPATTERNS (t); ++i)
> !       {
> !       for (unsigned int j = 0; j < 2; ++j)
> !         visit (VECTOR_CST_BASE (t, i, j));
> !       visit (VECTOR_CST_STEP (t, i));
> !       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
>       {
> Index: gcc/tree-streamer-out.c
> ===================================================================
> *** gcc/tree-streamer-out.c     2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree-streamer-out.c     2017-11-29 11:08:00.186993912 +0000
> *************** write_ts_common_tree_pointers (struct ou
> *** 533,543 ****
>   static void
>   write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
>   {
> -   unsigned i;
>     /* Note that the number of elements for EXPR has already been emitted
>        in EXPR's header (see streamer_write_tree_header).  */
> !   for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> !     stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
>   }
>
>
> --- 533,546 ----
>   static void
>   write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
>   {
>     /* Note that the number of elements for EXPR has already been emitted
>        in EXPR's header (see streamer_write_tree_header).  */
> !   for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
> !     {
> !       for (unsigned int j = 0; j < 2; ++j)
> !       stream_write_tree (ob, VECTOR_CST_BASE (expr, i, j), ref_p);
> !       stream_write_tree (ob, VECTOR_CST_STEP (expr, i), ref_p);
> !     }
>   }
>
>
> *************** streamer_write_tree_header (struct outpu
> *** 960,966 ****
>     else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
>       write_identifier (ob, ob->main_stream, expr);
>     else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> !     streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
>     else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
>       streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
>     else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
> --- 963,975 ----
>     else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
>       write_identifier (ob, ob->main_stream, expr);
>     else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> !     {
> !       bitpack_d bp = bitpack_create (ob->main_stream);
> !       bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 16);
> !       bp_pack_value (&bp, VECTOR_CST_DUPLICATE_P (expr), 1);
> !       bp_pack_value (&bp, VECTOR_CST_SERIES_P (expr), 1);
> !       streamer_write_bitpack (&bp);
> !     }
>     else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
>       streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
>     else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
> Index: gcc/tree-streamer-in.c
> ===================================================================
> *** gcc/tree-streamer-in.c      2017-11-29 11:07:59.961993930 +0000
> --- gcc/tree-streamer-in.c      2017-11-29 11:08:00.186993912 +0000
> *************** streamer_alloc_tree (struct lto_input_bl
> *** 592,599 ****
>       }
>     else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
> !       HOST_WIDE_INT len = streamer_read_hwi (ib);
> !       result = make_vector (len);
>       }
>     else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
>       {
> --- 592,602 ----
>       }
>     else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
> !       bitpack_d bp = streamer_read_bitpack (ib);
> !       unsigned int log2_npatterns = bp_unpack_value (&bp, 16);
> !       unsigned int duplicate_p = bp_unpack_value (&bp, 1);
> !       unsigned int series_p = bp_unpack_value (&bp, 1);
> !       result = make_vector (log2_npatterns, duplicate_p, series_p);
>       }
>     else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
>       {
> *************** lto_input_ts_common_tree_pointers (struc
> *** 650,658 ****
>   lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
>                                    struct data_in *data_in, tree expr)
>   {
> !   unsigned i;
> !   for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> !     VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
>   }
>
>
> --- 653,664 ----
>   lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
>                                    struct data_in *data_in, tree expr)
>   {
> !   for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (expr); ++i)
> !     {
> !       for (unsigned int j = 0; j < 2; ++j)
> !       VECTOR_CST_BASE (expr, i, j) = stream_read_tree (ib, data_in);
> !       VECTOR_CST_STEP (expr, i) = stream_read_tree (ib, data_in);
> !     }
>   }
>
>
> Index: gcc/fold-const.c
> ===================================================================
> *** gcc/fold-const.c    2017-11-29 11:07:59.961993930 +0000
> --- gcc/fold-const.c    2017-11-29 11:08:00.184993912 +0000
> *************** fold_ternary_loc (location_t loc, enum t
> *** 11610,11618 ****
>                   unsigned int nelts = VECTOR_CST_NELTS (arg0);
>                   auto_vec<tree, 32> elts (nelts);
>                   elts.quick_grow (nelts);
> !                 memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
> !                         sizeof (tree) * nelts);
> !                 elts[k] = arg1;
>                   return build_vector (type, elts);
>                 }
>             }
> --- 11610,11617 ----
>                   unsigned int nelts = VECTOR_CST_NELTS (arg0);
>                   auto_vec<tree, 32> elts (nelts);
>                   elts.quick_grow (nelts);
> !                 for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
> !                   elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
>                   return build_vector (type, elts);
>                 }
>             }
> Index: gcc/lto/lto.c
> ===================================================================
> *** gcc/lto/lto.c       2017-11-29 11:07:59.961993930 +0000
> --- gcc/lto/lto.c       2017-11-29 11:08:00.185993912 +0000
> *************** #define compare_values(X) \
> *** 1065,1070 ****
> --- 1065,1077 ----
>                         TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
>         return false;
>
> +   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> +     {
> +       compare_values (VECTOR_CST_LOG2_NPATTERNS);
> +       compare_values (VECTOR_CST_DUPLICATE_P);
> +       compare_values (VECTOR_CST_SERIES_P);
> +     }
> +
>     if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
>       {
>         compare_values (DECL_MODE);
> *************** #define compare_tree_edges(E1, E2) \
> *** 1281,1291 ****
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
> -       unsigned i;
>         /* Note that the number of elements for EXPR has already been emitted
>          in EXPR's header (see streamer_write_tree_header).  */
> !       for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
> !       compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
> --- 1288,1303 ----
>
>     if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>       {
>         /* Note that the number of elements for EXPR has already been emitted
>          in EXPR's header (see streamer_write_tree_header).  */
> !       for (unsigned int i = 0; i < VECTOR_CST_NPATTERNS (t1); ++i)
> !       {
> !         for (unsigned int j = 0; j < 2; ++j)
> !           compare_tree_edges (VECTOR_CST_BASE (t1, i, j),
> !                               VECTOR_CST_BASE (t2, i, j));
> !         compare_tree_edges (VECTOR_CST_STEP (t1, i),
> !                             VECTOR_CST_STEP (t2, i));
> !       }
>       }
>
>     if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-29 15:56 ` RFC: Variable-length VECTOR_CSTs Richard Biener
@ 2017-11-29 18:24   ` Richard Sandiford
  2017-11-30 14:10     ` Richard Sandiford
  0 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-11-29 18:24 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

Thanks for the quick feedback!

Richard Biener <richard.guenther@gmail.com> writes:
> On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
> <richard.sandiford@linaro.org> wrote:
>> It was clear from the SVE reviews that people were unhappy with how
>> "special" the variable-length case was.  One particular concern was
>> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
>> that would in turn lead to different representations of VEC_PERM_EXPRs
>> with constant permute vectors, and difficulties in invoking
>> vec_perm_const_ok.
>>
>> This is an RFC for a VECTOR_CST representation that treats each
>> specific constant as one instance of an arbitrary-length sequence.
>> The reprensentation then extends to variable-length vectors in a
>> natural way.
>>
>> As discussed on IRC, if a vector contains X*N elements for some
>> constant N and integer X>0, the main features we need are:
>>
>> 1) the ability to represent a sequence that duplicates N values
>>
>>    This is useful for SLP invariants.
>>
>> 2) the ability to represent a sequence that starts with N values and
>>    is followed by zeros
>>
>>    This is useful for the initial value in a double or SLP reduction
>>
>> 3) the ability to represent N interleaved series
>>
>>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>>
>> For (2), zero isn't necessarily special, since vectors used in an AND
>> reduction might need to fill with ones.  Also, we might need up to N
>> different fill values with mixed SLP operations; it isn't necessarily
>> safe to assume that a single fill value will always be enough.
>>
>> The same goes for (3): there's no reason in principle why the
>> steps in an SLP induction should all be the same (although they
>> do need to be at the moment).  E.g. once we support SLP on:
>>
>>   for (unsigned int i = 0; i < n; i += 2)
>>     {
>>       x[i] += 4 + i;
>>       x[i + 1] += 11 + i * 3;
>>     }
>>
>> we'll need {[4, 14], +, [2, 6]}.
>>
>> So the idea is to represent vectors as P interleaved patterns of the form:
>>
>>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>>
>> where the STEP is always zero (actually null) for non-integer vectors.
>> This is effectively projecting a "foreground" value of P elements
>> onto an arbitrary-length "background" sequenece, where the background
>> sequence contains P parallel linear series.
>>
>> E.g. to pick an extreme and unlikely example,
>>
>>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>>
>> has 2 patterns:
>>
>>   BASE0 = 42, BASE1 = 2, STEP = 1
>>   BASE0 = 99, BASE1 = 20, STEP = 10
>>
>> The more useful cases are degenerate versions of this general case.
>>
>> As far as memory consumption goes: the number of patterns needed for a
>> fixed-length vector with 2*N elements is always at most N; in the worst
>> case, we simply interleave the first N elements with the second N elements.
>> The worst-case increase in footprint is therefore N trees for the steps.
>> In practice the footprint is usually smaller than it was before, since
>> most constants do have a pattern.
>>
>> The patch below implements this for trees.  I have patches to use the
>> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
>> tree one is probably easiest to read.
>>
>> The patch only adds the representation.  Follow-on patches make more
>> use of it (and usually make things simpler; e.g. integer_zerop is no
>> longer a looping operation).
>>
>> Does this look better?
>
> Yes, the overall design looks good.  I wonder why you chose to have
> the number of patterns being a power of two?  I suppose this is
> to have the same number of elements from all patterns in the final
> vector (which is power-of-two sized)?

Right.  The rtl and vec_perm_indices parts don't have this restriction,
since some ports do define non-power-of-2 vectors for internal use.
The problem is that, since VECTOR_CSTs are used by the FE, we need
to support all valid vector lengths without blowing the 16-bit field.
Using the same style of representation as TYPE_VECTOR_SUBPARTS seemed
like the safest way of doing that.

> I wonder if there exists a vector where say a three-pattern
> interleaving would be smaller than a four-pattern one?

Only in the non-power-of-2 case.

> Given you add flags for various purposes would it make sense to
> overload 'step' with a regular element to avoid the storage increase
> in case step is unnecessary?  This makes it have three elements
> which is of course awkward :/

I wondered about keeping it as an array of trees and tacking the
steps onto the end as an optional addition.  But the idea is that
tree_vector_pattern becomes the preferred way of handling constant
vectors, if it can be used, so it seemed neater to use in the tree
node too.

> Few more comments below.
>
> Otherwise looks good to go!
>
> Thanks for doing this.
>
> [...]
>> ! /* PATTERNS represents a constant of type TYPE.  Compress it into the
>> !    canonical form, returning the new vector of patterns.  Use CUR and NEXT
>> !    as temporary workspace.  */
>> !
>> ! static vec<tree_vector_pattern>
>> ! compress_vector_patterns (vec<tree_vector_pattern> *cur,
>> !                         vec<tree_vector_pattern> *next,
>> !                         tree type, vec<tree_vector_pattern> patterns)
>> ! {
>> !   while (patterns.length () > 1)
>> !     {
>> !       unsigned int npatterns = patterns.length ();
>> !       gcc_assert ((npatterns & 1) == 0);
>> !       unsigned int step = npatterns / 2;
>> !       cur->truncate (0);
>> !       cur->reserve (step);
>> !       bool first_p = npatterns * 2 == TYPE_VECTOR_SUBPARTS (type);
>> !       for (unsigned int i = 0; i < step; ++i)
>> !       if (!combine_patterns (cur, &patterns[i], &patterns[i + step],
>> !                              first_p))
>> !         return patterns;
>> !       patterns = *cur;
>> !       std::swap (cur, next);
>> !     }
>> !   return patterns;
>> ! }
>
> Wow, that looks complicated ;)  Which means it needs some more comments.

OK :-)

> I wonder if avoiding all this for the special cases we like to handle by
> providing vector_cst constructors for one and two pattern cases would
> be best.  Or even more specialized, not exposing "patterns".

We still have build_vector_from_val, and there'll be one for building
normal series.  But code working on general vectors should usually
operate on patterns where possible, so that it extends to variable-length
vectors.  Such code can't make any assumptions about the shape of an
existing vector.

We also need the complication above for building vectors from vec<tree>s,
so that there's only one canonical representation of a given vector,
however it was built.

>> ! /* Return true if PATTERNS has an overflow bit set.  */
>> !
>> ! static bool
>> ! vector_overflow_p (vec<tree_vector_pattern> patterns)
>> ! {
>> !   unsigned int i;
>> !   tree_vector_pattern *pattern;
>> !   FOR_EACH_VEC_ELT (patterns, i, pattern)
>> !     for (unsigned int j = 0; j < 2; ++j)
>> !       if (CONSTANT_CLASS_P (pattern->base[j])
>> !         && TREE_OVERFLOW (pattern->base[j]))
>> !       return true;
>> !   return false;
>> ! }
>> !
>> ! /* Return true if PATTERNS represents a duplicate operation.  */
>>
>> ! static bool
>> ! vector_duplicate_p (vec<tree_vector_pattern> patterns)
>> ! {
>> !   unsigned int i;
>> !   tree_vector_pattern *pattern;
>> !   FOR_EACH_VEC_ELT (patterns, i, pattern)
>> !     {
>> !       if (pattern->step && wi::to_wide (pattern->step) != 0)
>
> I wonder if step == 0 should be canonicalized to NULL_TREE?

Kept wondering about that, but couldn't make my mind up which way was
better.  I'll see how it looks with that change.

>> !       return false;
>> !       if (!operand_equal_p (pattern->base[0], pattern->base[1], 0))
>> !       return false;
>>       }
>> +   return true;
>> + }
>> +
>> + /* Return true if PATTERNS represents a vector series.  */
>> +
>> + static bool
>> + vector_series_p (vec<tree_vector_pattern> patterns)
>> + {
>> +   unsigned int i;
>> +   tree_vector_pattern *pattern;
>> +   FOR_EACH_VEC_ELT (patterns, i, pattern)
>> +     if (!pattern->step
>> +       || (wi::to_wide (pattern->base[1]) - wi::to_wide (pattern->base[0])
>> +           != wi::to_wide (pattern->step)))
>> +       return false;
>> +   return true;
>> + }
>> +
>> + /* Build a VECTOR_CST of type TYPE using the patterns in PATTERNS,
>> +    canonicalizing it first if necessary.  */
>> +
>> + tree
>> + build_vector (tree type, vec<tree_vector_pattern> patterns MEM_STAT_DECL)
>> + {
>> +   auto_vec<tree_vector_pattern, 16> tmp1, tmp2;
>> +   patterns = compress_vector_patterns (&tmp1, &tmp2, type, patterns);
>> +   unsigned int npatterns = patterns.length ();
>> +
>> +   gcc_assert (pow2p_hwi (npatterns));
>> +   bool overflow_p = vector_overflow_p (patterns);
>> +   bool duplicate_p = vector_duplicate_p (patterns);
>> +   bool series_p = vector_series_p (patterns);
>> +   tree v = make_vector (exact_log2 (npatterns), duplicate_p, series_p);
>> +   TREE_TYPE (v) = type;
>> +   TREE_OVERFLOW (v) = overflow_p;
>>
>> !   memcpy (v->vector.patterns, patterns.address (),
>> !         npatterns * sizeof (tree_vector_pattern));
>>     return v;
>>   }
>>
>>   /* Return a new VECTOR_CST node whose type is TYPE and whose values
>> +    are given by ELTS.  */
>> +
>> + tree
>> + build_vector (tree type, vec<tree> elts MEM_STAT_DECL)
>> + {
>> +   unsigned int nelts = elts.length ();
>> +   gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
>> +
>> +   gcc_assert (pow2p_hwi (nelts));
>> +   bool single_p = nelts == 1;
>> +   unsigned int npatterns = single_p ? 1 : nelts / 2;
>> +   auto_vec<tree_vector_pattern, 16> patterns (npatterns);
>> +   for (unsigned int i = 0; i < npatterns; ++i)
>> +     {
>> +       tree_vector_pattern pattern;
>> +       pattern.base[0] = elts[i];
>> +       pattern.base[1] = elts[single_p ? i : i + npatterns];
>> +       if (INTEGRAL_TYPE_P (TREE_TYPE (type))
>> +         && TREE_CODE (pattern.base[0]) == INTEGER_CST
>> +         && TREE_CODE (pattern.base[1]) == INTEGER_CST)
>> +       pattern.step = wide_int_to_tree (TREE_TYPE (type),
>> +                                        wi::to_wide (pattern.base[1])
>> +                                        - wi::to_wide (pattern.base[0]));
>> +       else
>> +       pattern.step = NULL_TREE;
>
> I wonder why given the simple representation with n/2 patterns
> you don't use step == NULL_TREE unconditionally here.

The idea is to make constants with exactly 2*npatterns elements less
special.  When a constant ends after exactly 2 patterns, we could
choose to extend it with any step.  But since the most common use of a
series is to have a+b*x (with no special case for the first element),
using the difference seemed like the best default choice.

E.g. if we want to ask "is element x equal to 0+x*2?", the answer
will be the same for {0, 2} and {0, 2, 4, 6}.

Of course, this means that "is this equal to { 1, 0, 0, ... }?"
does need to treat the 2-pattern case as special.  So yeah,
using a null step and always treating the 2-pattern case as
special would be another alternative.

> Is there a target macro specifying the maximum target supported
> vector length?  It might be nice to use that in the auto_vec <> size.

Not that I know of.  But this code is laso used for generic vectors
that don't have target support, so it might make sense to have a
target-independent choice anyway.  I can use a typedef to avoid
hard-coding the number everywhere.

> OTOH sometimes I really like
>
>   tree_vector_pattern *p = XALLOCAVEC (npatterns);
>
> more... (ok, __attribute__((vector_size(1048576))) might blow the stack...)

Yeah :-)  Plus it gets awkward in loops.

Thanks,
Richard

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-29 18:24   ` Richard Sandiford
@ 2017-11-30 14:10     ` Richard Sandiford
  2017-12-01 11:28       ` Richard Biener
  0 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-11-30 14:10 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

Richard Sandiford <richard.sandiford@linaro.org> writes:
> Richard Biener <richard.guenther@gmail.com> writes:
>> On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
>> <richard.sandiford@linaro.org> wrote:
>>> It was clear from the SVE reviews that people were unhappy with how
>>> "special" the variable-length case was.  One particular concern was
>>> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
>>> that would in turn lead to different representations of VEC_PERM_EXPRs
>>> with constant permute vectors, and difficulties in invoking
>>> vec_perm_const_ok.
>>>
>>> This is an RFC for a VECTOR_CST representation that treats each
>>> specific constant as one instance of an arbitrary-length sequence.
>>> The reprensentation then extends to variable-length vectors in a
>>> natural way.
>>>
>>> As discussed on IRC, if a vector contains X*N elements for some
>>> constant N and integer X>0, the main features we need are:
>>>
>>> 1) the ability to represent a sequence that duplicates N values
>>>
>>>    This is useful for SLP invariants.
>>>
>>> 2) the ability to represent a sequence that starts with N values and
>>>    is followed by zeros
>>>
>>>    This is useful for the initial value in a double or SLP reduction
>>>
>>> 3) the ability to represent N interleaved series
>>>
>>>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>>>
>>> For (2), zero isn't necessarily special, since vectors used in an AND
>>> reduction might need to fill with ones.  Also, we might need up to N
>>> different fill values with mixed SLP operations; it isn't necessarily
>>> safe to assume that a single fill value will always be enough.
>>>
>>> The same goes for (3): there's no reason in principle why the
>>> steps in an SLP induction should all be the same (although they
>>> do need to be at the moment).  E.g. once we support SLP on:
>>>
>>>   for (unsigned int i = 0; i < n; i += 2)
>>>     {
>>>       x[i] += 4 + i;
>>>       x[i + 1] += 11 + i * 3;
>>>     }
>>>
>>> we'll need {[4, 14], +, [2, 6]}.
>>>
>>> So the idea is to represent vectors as P interleaved patterns of the form:
>>>
>>>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>>>
>>> where the STEP is always zero (actually null) for non-integer vectors.
>>> This is effectively projecting a "foreground" value of P elements
>>> onto an arbitrary-length "background" sequenece, where the background
>>> sequence contains P parallel linear series.
>>>
>>> E.g. to pick an extreme and unlikely example,
>>>
>>>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>>>
>>> has 2 patterns:
>>>
>>>   BASE0 = 42, BASE1 = 2, STEP = 1
>>>   BASE0 = 99, BASE1 = 20, STEP = 10
>>>
>>> The more useful cases are degenerate versions of this general case.
>>>
>>> As far as memory consumption goes: the number of patterns needed for a
>>> fixed-length vector with 2*N elements is always at most N; in the worst
>>> case, we simply interleave the first N elements with the second N elements.
>>> The worst-case increase in footprint is therefore N trees for the steps.
>>> In practice the footprint is usually smaller than it was before, since
>>> most constants do have a pattern.
>>>
>>> The patch below implements this for trees.  I have patches to use the
>>> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
>>> tree one is probably easiest to read.
>>>
>>> The patch only adds the representation.  Follow-on patches make more
>>> use of it (and usually make things simpler; e.g. integer_zerop is no
>>> longer a looping operation).
>>>
>>> Does this look better?
>>
>> Yes, the overall design looks good.  I wonder why you chose to have
>> the number of patterns being a power of two?  I suppose this is
>> to have the same number of elements from all patterns in the final
>> vector (which is power-of-two sized)?
>
> Right.  The rtl and vec_perm_indices parts don't have this restriction,
> since some ports do define non-power-of-2 vectors for internal use.
> The problem is that, since VECTOR_CSTs are used by the FE, we need
> to support all valid vector lengths without blowing the 16-bit field.
> Using the same style of representation as TYPE_VECTOR_SUBPARTS seemed
> like the safest way of doing that.
>
>> I wonder if there exists a vector where say a three-pattern
>> interleaving would be smaller than a four-pattern one?
>
> Only in the non-power-of-2 case.
>
>> Given you add flags for various purposes would it make sense to
>> overload 'step' with a regular element to avoid the storage increase
>> in case step is unnecessary?  This makes it have three elements
>> which is of course awkward :/
>
> I wondered about keeping it as an array of trees and tacking the
> steps onto the end as an optional addition.  But the idea is that
> tree_vector_pattern becomes the preferred way of handling constant
> vectors, if it can be used, so it seemed neater to use in the tree
> node too.

In the end it seemed better to encode the first NPATTERNS * N
elements of the vector, where:

- N==3 if at least one pattern needs a step; otherwise
- N==2 if at least one pattern has different BASE0s and BASE1s; otherwise
- N==1 (i.e. the vector is a repeated sequence of NPATTERNS elements)

So effectively all we're doing for the constant-length case is
reducing the number of elements that need to be stored and processed
(once code is converted to use the new routines).

The patch below does this.  It also introduces a new class
vector_builder<T> for building vectors of Ts, with a derived
tree_vector_builder specifically for trees.  This allows the
compression code to be shared between representations and also
avoids hard-coding the auto_vec<> count everywhere.

I've also fixed the selftests to get the actual and expected values
the right way round (thanks David for noticing that).

What do you think?  Does this look better?

Thanks,
Richard


2017-11-30  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
	* doc/generic.texi (VECTOR_CST): Describe new representation of
	vector constants.
	* vector-builder.h: New file.
	* tree-vector-builder.h: Likewise.
	* tree-vector-builder.c: Likewise.
	* Makefile.in (OBJS): Add tree-vector-builder.o.
	* tree.def (VECTOR_CST): Update comment to refer to generic.texi.
	* tree-core.h (tree_base): Add a vector_cst field to the u union.
	(tree_vector): Change the number of elements to
	vector_cst_encoded_nelts.
	* tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
	(VECTOR_CST_ELTS): Delete.
	(VECTOR_CST_ELT): Redefine using vector_cst_elt.
	(VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
	(VECTOR_CST_NELTS_PER_PATTERN, VECTOR_CST_DUPLICATE_P): Likewise.
	(VECTOR_CST_STRIDED_P, VECTOR_CST_ENCODED_ELTS): Likewise.
	(VECTOR_CST_ENCODED_ELT): Likewise.
	(vector_cst_encoded_nelts): New function.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
	VECTOR_CST_NELTS_PER_PATTERN as arguments.
	(vector_cst_int_elt, vector_cst_elt): Declare.
	* tree.c: Include tree-vector-builder.h.
	(tree_code_size): Abort if passed VECTOR_CST.
	(tree_size): Update for new VECTOR_CST layout.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
	VECTOR_CST_NELTS_PER_PATTERN as arguments.
	(build_vector): Use tree_vector_builder.
	(vector_cst_int_elt, vector_cst_elt): New functions.
	(drop_tree_overflow): For VECTOR_CST, drop the TREE_OVERFLOW from the
	encoded elements and then create the vector in the canonical form.
	(check_vector_cst, test_vector_cst_patterns): New functions.
	(tree_c_tests): Call it.
	* lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
	VECTOR_CST fields.
	(hash_tree): Likewise.
	* tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
	(streamer_write_tree_header): Likewise.
	* tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
	(streamer_alloc_tree): Likewise.  Update call to make_vector.
	* fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.

gcc/lto/
	* lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.

Index: gcc/doc/generic.texi
===================================================================
--- gcc/doc/generic.texi	2017-11-30 13:07:23.752425675 +0000
+++ gcc/doc/generic.texi	2017-11-30 13:07:24.028415186 +0000
@@ -1084,10 +1084,64 @@ These nodes are used to represent comple
 imaginary parts respectively.
 
 @item VECTOR_CST
-These nodes are used to represent vector constants, whose parts are
-constant nodes.  Each individual constant node is either an integer or a
-double constant node.  The first operand is a @code{TREE_LIST} of the
-constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
+These nodes are used to represent vector constants.  Each vector
+constant @var{v} is treated as a specific instance of an arbitrary-length
+sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
+interleaved patterns.  Each pattern has the form:
+
+@smallexample
+@{ @var{base0}, @var{base1}, @var{base1} + @var{step}, @var{base1} + @var{step} * 2, @dots{} @}
+@end smallexample
+
+The first three elements in each pattern are enough to determine the
+values of other elements.  However, if all @var{step}s are zero,
+only the first two elements are needed.  If in addition each @var{base1}
+is equal to the corresponding @var{base0}, only the first element in
+each pattern is needed.  The number of encoded elements per pattern
+is given by @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v})}.
+
+For example, the constant:
+
+@smallexample
+@{ 0, 1, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18 @}
+@end smallexample
+
+is encoded using the interleaved sequences:
+
+@smallexample
+@{ 0, 2, 3, 4, 5, 6, 7, 8 @}
+@{ 1, 6, 8, 10, 12, 14, 16, 18 @}
+@end smallexample
+
+where the sequences are represented by the following patterns:
+
+@smallexample
+@var{base0} == 0, @var{base1} == 2, @var{step} == 1
+@var{base0} == 1, @var{base1} == 6, @var{step} == 2
+@end smallexample
+
+In this case:
+
+@smallexample
+VECTOR_CST_NPATTERNS (@var{v}) == 2
+VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3
+@end smallexample
+
+The vector is therefore encoded using the first 6 elements, with the
+remaining 10 elements being implicit extensions of them.
+
+@samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply
+contains repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})}
+values.  This is a shorthand for testing
+@samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 1}.
+
+@samp{VECTOR_CST_STEPPED_P (@var{v})} is true if at least one
+pattern in @var{v} has a nonzero step.  This is a shorthand for
+testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3}.
+
+The utility function @code{vector_cst_elt} gives the value of an
+arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
+the same value as a @code{wide_int}.
 
 @item STRING_CST
 These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
Index: gcc/vector-builder.h
===================================================================
--- /dev/null	2017-11-30 11:13:00.800636184 +0000
+++ gcc/vector-builder.h	2017-11-30 13:07:24.033414996 +0000
@@ -0,0 +1,372 @@
+/* A class for building vector constant patterns.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_VECTOR_BUILDER_H
+#define GCC_VECTOR_BUILDER_H
+
+/* This class is a wrapper around auto_vec<T> for building vectors of T.
+   It aims to encode each vector as npatterns interleaved patterns,
+   where each pattern represents a sequence:
+
+     { BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, BASE1 + STEP*3, ... }
+
+   The first three elements in each pattern provide enough information
+   to derive the other elements.  If all patterns have a STEP of zero,
+   we only need to encode the first two elements in each pattern.
+   If BASE1 is also equal to BASE0 for all patterns, we only need to
+   encode the first element in each pattern.  The number of encoded
+   elements per pattern is given by nelts_per_pattern.
+
+   The class can be used in two ways:
+
+   1. It can be used to build a full image of the vector, which is then
+      canonicalized.  In this case npatterns is initially the number of
+      elements in the vector and nelts_per_pattern is initially 1.
+
+   2. It can be used to build a vector that already has a known encoding.
+      This is preferred since it is more efficient and copes with
+      variable-length vectors.  The class then canonicalizes the encoding
+      to a simpler form if possible.
+
+   Derived classes provide this functionality for specific Ts.
+   The "Derived *" arguments to some functions are pointers to the
+   derived class, which needs to provide the following interface:
+
+      bool equal_p (unsigned int i1, unsigned int i2) const;
+
+	  Return true if elements I1 and I2 are equal.
+
+      bool allow_steps_p () const;
+
+	  Return true if a stepped representation is OK.  We don't allow
+	  linear series for anything other than integers, to avoid problems
+	  with rounding.
+
+      bool integral_p (unsigned int i) const;
+
+	  Return true if element I can be interpreted as an integer.
+
+      StepType step (unsigned int i1, unsigned int i2) const;
+
+	  Return the value of element I2 minus the value of element I1,
+	  given integral_p (I1) && integral_p (I2).  There is no fixed
+	  choice of StepType.
+
+      bool can_elide_p (unsigned int i) const;
+
+	  Return true if we can drop element I, even if the retained
+	  elements are different.  This is provided for TREE_OVERFLOW
+	  handling.
+
+      void note_representative (unsigned int i1, unsigned int i2);
+
+	  Record that I2 is being elided and that I1 is the last encoded
+	  element for the containing pattern.  This is again provided
+	  for TREE_OVERFLOW handling.  */
+
+template<typename T>
+class vector_builder : public auto_vec<T, 32>
+{
+public:
+  vector_builder (unsigned int, unsigned int, unsigned int);
+  vector_builder (const vector_builder &);
+
+  unsigned int full_nelts () const { return m_full_nelts; }
+  unsigned int npatterns () const { return m_npatterns; }
+  unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
+  unsigned int encoded_nelts () const;
+  bool encoded_full_vector_p () const;
+
+  void reshape (unsigned int, unsigned int);
+
+protected:
+  template<typename Derived>
+  void reshape (Derived *, unsigned int, unsigned int);
+  template<typename Derived>
+  bool repeating_sequence_p (Derived *, unsigned int, unsigned int,
+			     unsigned int);
+  template<typename Derived>
+  bool stepped_sequence_p (Derived *, unsigned int, unsigned int,
+			   unsigned int);
+  template<typename Derived>
+  bool try_npatterns (Derived *, unsigned int);
+  template<typename Derived>
+  void finalize (Derived *);
+
+private:
+  vector_builder &operator= (const vector_builder &);
+
+  unsigned int m_full_nelts;
+  unsigned int m_npatterns;
+  unsigned int m_nelts_per_pattern;
+};
+
+/* Initialize a vector_builder for a vector that has FULL_NELTS elements.
+   Initially encode it using NPATTERNS patterns with NELTS_PER_PATTERN
+   each.  */
+
+template<typename T>
+inline
+vector_builder<T>::vector_builder (unsigned int full_nelts,
+				   unsigned int npatterns,
+				   unsigned int nelts_per_pattern)
+  : auto_vec<T, 32> (npatterns * nelts_per_pattern),
+    m_full_nelts (full_nelts),
+    m_npatterns (npatterns),
+    m_nelts_per_pattern (nelts_per_pattern)
+{}
+
+/* Copy constructor.  */
+
+template<typename T>
+inline
+vector_builder<T>::vector_builder (const vector_builder &other)
+  : auto_vec<T, 32> (other.length ()),
+    m_full_nelts (other.m_full_nelts),
+    m_npatterns (other.m_npatterns),
+    m_nelts_per_pattern (other.m_nelts_per_pattern)
+{
+  this->splice (other);
+}
+
+/* Return the number of elements that are explicitly encoded.  The vec
+   starts with these explicitly-encoded elements and may contain additional
+   elided elements.  */
+
+template<typename T>
+inline unsigned int
+vector_builder<T>::encoded_nelts () const
+{
+  return m_npatterns * m_nelts_per_pattern;
+}
+
+/* Return true if every element of the vector is explicitly encoded.  */
+
+template<typename T>
+inline bool
+vector_builder<T>::encoded_full_vector_p () const
+{
+  return m_npatterns * m_nelts_per_pattern == m_full_nelts;
+}
+
+/* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
+   but without changing the underlying vector.  We explicitly don't
+   truncate the vec here since callers might want to refer to the
+   elided elements.  */
+
+template<typename T>
+inline void
+vector_builder<T>::reshape (unsigned int npatterns,
+			    unsigned int nelts_per_pattern)
+{
+  m_npatterns = npatterns;
+  m_nelts_per_pattern = nelts_per_pattern;
+}
+
+/* Likewise, but invoke DERIVED->note_representative for each elided
+   element.  */
+
+template<typename T>
+template<typename Derived>
+inline void
+vector_builder<T>::reshape (Derived *derived, unsigned int npatterns,
+			    unsigned int nelts_per_pattern)
+{
+  unsigned int old_encoded_nelts = encoded_nelts ();
+  reshape (npatterns, nelts_per_pattern);
+  unsigned int new_encoded_nelts = encoded_nelts ();
+  unsigned int next = new_encoded_nelts - m_npatterns;
+  for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
+    {
+      derived->note_representative (next, i);
+      next += 1;
+      if (next == new_encoded_nelts)
+	next -= m_npatterns;
+    }
+}
+
+/* Return true if elements [START, END) contain a repeating sequence of
+   STEP elements.  */
+
+template<typename T>
+template<typename Derived>
+bool
+vector_builder<T>::repeating_sequence_p (Derived *derived, unsigned int start,
+					 unsigned int end, unsigned int step)
+{
+  for (unsigned int i = start; i < end - step; ++i)
+    if (!derived->equal_p (i, i + step))
+      return false;
+  return true;
+}
+
+/* Return true if elements [START, END) contain STEP interleaved linear
+   series.  */
+
+template<typename T>
+template<typename Derived>
+bool
+vector_builder<T>::stepped_sequence_p (Derived *derived, unsigned int start,
+				       unsigned int end, unsigned int step)
+{
+  if (!derived->allow_steps_p ())
+    return false;
+
+  for (unsigned int i3 = start + step * 2; i3 < end; ++i3)
+    {
+      unsigned int i2 = i3 - step;
+      unsigned int i1 = i2 - step;
+
+      if (!derived->integral_p (i1)
+	  || !derived->integral_p (i2)
+	  || !derived->integral_p (i3))
+	return false;
+
+      if (derived->step (i1, i2) != derived->step (i2, i3))
+	return false;
+
+      if (!derived->can_elide_p (i3))
+	return false;
+    }
+  return true;
+}
+
+/* Try to change the number of encoded patterns to NPATTERNS, returning
+   true on success.  */
+
+template<typename T>
+template<typename Derived>
+bool
+vector_builder<T>::try_npatterns (Derived *derived, unsigned int npatterns)
+{
+  if (m_nelts_per_pattern == 1)
+    {
+      /* See whether NPATTERNS is valid with the current 1-element-per-pattern
+	 encoding.  */
+      if (repeating_sequence_p (derived, 0, encoded_nelts (), npatterns))
+	{
+	  reshape (derived, npatterns, 1);
+	  return true;
+	}
+
+      /* We can only increase the number of elements per pattern if all
+	 elements are still encoded explicitly.  */
+      if (!encoded_full_vector_p ())
+	return false;
+    }
+
+  if (m_nelts_per_pattern <= 2)
+    {
+      /* See whether NPATTERNS is valid with a 2-element-per-pattern
+	 encoding.  */
+      if (repeating_sequence_p (derived, npatterns, encoded_nelts (),
+				npatterns))
+	{
+	  reshape (derived, npatterns, 2);
+	  return true;
+	}
+
+      /* We can only increase the number of elements per pattern if all
+	 elements are still encoded explicitly.  */
+      if (!encoded_full_vector_p ())
+	return false;
+    }
+
+  if (m_nelts_per_pattern <= 3)
+    {
+      /* See whether we have NPATTERNS interleaved linear series,
+	 giving a 3-element-per-pattern encoding.  */
+      if (stepped_sequence_p (derived, npatterns, encoded_nelts (), npatterns))
+	{
+	  reshape (derived, npatterns, 3);
+	  return true;
+	}
+      return false;
+    }
+
+  gcc_unreachable ();
+}
+
+/* Canonicalize the encoding.  */
+
+template<typename T>
+template<typename Derived>
+void
+vector_builder<T>::finalize (Derived *derived)
+{
+  /* Try to whittle down the number of elements per pattern.  That is:
+
+     1. If we have stepped patterns whose steps are all 0, reduce the
+        number of elements per pattern from 3 to 2.
+
+     2. If we have background fill values that are the same as the
+        foreground values, reduce the number of elements per pattern
+        from 2 to 1.  */
+  while (m_nelts_per_pattern > 1
+	 && repeating_sequence_p (derived, encoded_nelts () - m_npatterns * 2,
+				  encoded_nelts (), m_npatterns))
+    /* The last two sequences of M_NPATTERNS elements are equal,
+       so remove the last one.  */
+    reshape (derived, m_npatterns, m_nelts_per_pattern - 1);
+
+  /* Try to halve the number of patterns while doing so gives a valid pattern.
+
+     Each halving step tries to keep the number of elements per pattern
+     the same.  If that isn't possible, and if all elements are still
+     explicitly encoded, the halving step can instead increase the number
+     of elements per pattern.
+
+     E.g. for:
+
+        { 0, 2, 3, 4, 5, 6, 7, 8 }  npatterns == 8  full_nelts == 8
+
+     we first realize that the second half of the sequence is not equal
+     to the first, so we cannot maintain 1 element per pattern for
+     npatterns == 4.  Instead we halve the number of patterns and double
+     the number of elements per pattern, treating this as a "foreground"
+     { 0, 2, 3, 4 } against a "background" of { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
+
+        { 0, 2, 3, 4 | 5, 6, 7, 8 }  npatterns == 4
+
+     Next we realize that this is *not* a foreround of { 0, 2 } against
+     a background of { 3, 4 | 3, 4 ... }, so the only remaining option
+     for reducing the number of patterns is to use a foreground of { 0, 2 }
+     against a stepped background of { 1, 2 | 3, 4 | 5, 6 ... }.  This is
+     valid because we still haven't elided any elements:
+
+        { 0, 2 | 3, 4 | 5, 6 }  npatterns == 2
+
+     This in turn can be reduced to a foreground of { 0 } against a stepped
+     background of { 1 | 2 | 3 ... }:
+
+        { 0 | 2 | 3 }  npatterns == 1
+
+     This last step would not have been possible for:
+
+        { 0, 0 | 3, 4 | 5, 6 }  npatterns == 2.  */
+  while ((m_npatterns & 1) == 0
+	 && try_npatterns (derived, m_npatterns / 2))
+    continue;
+
+  /* At present this code doesn't (need to) cope with vector lengths
+     that are not a power of 2.  */
+  gcc_assert (pow2p_hwi (m_npatterns));
+}
+
+#endif
Index: gcc/tree-vector-builder.h
===================================================================
--- /dev/null	2017-11-30 11:13:00.800636184 +0000
+++ gcc/tree-vector-builder.h	2017-11-30 13:07:24.029415148 +0000
@@ -0,0 +1,48 @@
+/* A class for building vector tree constants.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_TREE_VECTOR_BUILDER_H
+#define GCC_TREE_VECTOR_BUILDER_H
+
+#include "vector-builder.h"
+
+/* This class is used to build VECTOR_CSTs from a sequence of elements.
+   See vector_builder for more details.  */
+class tree_vector_builder : public vector_builder<tree>
+{
+  friend class vector_builder<tree>;
+
+public:
+  tree_vector_builder (tree, unsigned int, unsigned int);
+  tree build ();
+
+  static tree_vector_builder unary (tree, bool);
+
+private:
+  bool equal_p (unsigned int, unsigned int) const;
+  bool allow_steps_p () const;
+  bool integral_p (unsigned int) const;
+  wide_int step (unsigned int, unsigned int) const;
+  bool can_elide_p (unsigned int) const;
+  void note_representative (unsigned int, unsigned int);
+
+  tree m_type;
+};
+
+#endif
Index: gcc/tree-vector-builder.c
===================================================================
--- /dev/null	2017-11-30 11:13:00.800636184 +0000
+++ gcc/tree-vector-builder.c	2017-11-30 13:07:24.029415148 +0000
@@ -0,0 +1,131 @@
+/* A class for building vector tree constants.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree-vector-builder.h"
+#include "tree.h"
+#include "fold-const.h"
+
+/* Create a new builder for a vector of type TYPE.  Initially encode the
+   value as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements
+   each.  */
+
+tree_vector_builder::tree_vector_builder (tree type, unsigned int npatterns,
+					  unsigned int nelts_per_pattern)
+  : vector_builder<tree> (TYPE_VECTOR_SUBPARTS (type), npatterns,
+			  nelts_per_pattern),
+    m_type (type)
+{
+}
+
+/* Create a new builder for a unary operation on VECTOR_CST T.
+   ALLOW_STEPPED_P is true if the operation can handle stepped encodings
+   directly, without having to expand the full sequence.  */
+
+tree_vector_builder
+tree_vector_builder::unary (tree t, bool allow_stepped_p)
+{
+  tree type = TREE_TYPE (t);
+  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+  unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (t);
+  if (!allow_stepped_p && nelts_per_pattern > 2)
+    {
+      npatterns = TYPE_VECTOR_SUBPARTS (type);
+      nelts_per_pattern = 1;
+    }
+  return tree_vector_builder (type, npatterns, nelts_per_pattern);
+}
+
+/* Return true if elements I1 and I2 are equal.  */
+
+inline bool
+tree_vector_builder::equal_p (unsigned int i1, unsigned int i2) const
+{
+  return operand_equal_p ((*this)[i1], (*this)[i2], 0);
+}
+
+/* Return true if a stepped representation is OK.  We don't allow
+   linear series for anything other than integers, to avoid problems
+   with rounding.  */
+
+inline bool
+tree_vector_builder::allow_steps_p () const
+{
+  return INTEGRAL_TYPE_P (TREE_TYPE (m_type));
+}
+
+/* Return true if element I can be interpreted as an integer.  */
+
+inline bool
+tree_vector_builder::integral_p (unsigned int i) const
+{
+  return TREE_CODE ((*this)[i]) == INTEGER_CST;
+}
+
+/* Return the value of element I2 minus the value of element I1.
+   Both elements are known to be INTEGER_CSTs.  */
+
+inline wide_int
+tree_vector_builder::step (unsigned int i1, unsigned int i2) const
+{
+  return wi::to_wide ((*this)[i2]) - wi::to_wide ((*this)[i1]);
+}
+
+/* Return true if we can drop element I, even if the retained elements
+   are different.  Return false if this would mean losing overflow
+   information.  */
+
+inline bool
+tree_vector_builder::can_elide_p (unsigned int i) const
+{
+  tree elt = (*this)[i];
+  return !CONSTANT_CLASS_P (elt) || !TREE_OVERFLOW (elt);
+}
+
+/* Record that I2 is being elided and that I1 is the last encoded element
+   for the containing pattern.  */
+
+inline void
+tree_vector_builder::note_representative (unsigned int i1, unsigned int i2)
+{
+  tree elt2 = (*this)[i2];
+  if (CONSTANT_CLASS_P (elt2) && TREE_OVERFLOW (elt2))
+    {
+      tree elt1 = (*this)[i1];
+      gcc_assert (operand_equal_p (elt1, elt2, 0));
+      if (!TREE_OVERFLOW (elt2))
+	(*this)[i1] = elt2;
+    }
+}
+
+/* Return a VECTOR_CST for the current constant.  */
+
+tree
+tree_vector_builder::build ()
+{
+  finalize (this);
+  gcc_assert (pow2p_hwi (npatterns ()));
+  tree v = make_vector (exact_log2 (npatterns ()), nelts_per_pattern ());
+  TREE_TYPE (v) = m_type;
+  memcpy (VECTOR_CST_ENCODED_ELTS (v), address (),
+	  encoded_nelts () * sizeof (tree));
+  return v;
+}
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	2017-11-29 11:06:34.324730917 +0000
+++ gcc/Makefile.in	2017-11-30 13:07:24.027415224 +0000
@@ -1568,6 +1568,7 @@ OBJS = \
 	tree-vect-loop-manip.o \
 	tree-vect-slp.o \
 	tree-vectorizer.o \
+	tree-vector-builder.o \
 	tree-vrp.o \
 	tree.o \
 	typed-splay-tree.o \
Index: gcc/tree.def
===================================================================
--- gcc/tree.def	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree.def	2017-11-30 13:07:24.032415034 +0000
@@ -301,7 +301,7 @@ DEFTREECODE (FIXED_CST, "fixed_cst", tcc
    whose contents are other constant nodes.  */
 DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
 
-/* Contents are in VECTOR_CST_ELTS field.  */
+/* See generic.texi for details.  */
 DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
 
 /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
Index: gcc/tree-core.h
===================================================================
--- gcc/tree-core.h	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree-core.h	2017-11-30 13:07:24.029415148 +0000
@@ -976,8 +976,17 @@ struct GTY(()) tree_base {
     /* VEC length.  This field is only used with TREE_VEC.  */
     int length;
 
-    /* Number of elements.  This field is only used with VECTOR_CST.  */
-    unsigned int nelts;
+    /* This field is only used with VECTOR_CST.  */
+    struct {
+      /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
+      unsigned int log2_npatterns : 8;
+
+      /* The value of VECTOR_CST_NELTS_PER_PATTERN.  */
+      unsigned int nelts_per_pattern : 8;
+
+      /* For future expansion.  */
+      unsigned int unused : 16;
+    } vector_cst;
 
     /* SSA version number.  This field is only used with SSA_NAME.  */
     unsigned int version;
@@ -1333,7 +1342,7 @@ struct GTY(()) tree_complex {
 
 struct GTY(()) tree_vector {
   struct tree_typed typed;
-  tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
+  tree GTY ((length ("vector_cst_encoded_nelts ((tree) &%h)"))) elts[1];
 };
 
 struct GTY(()) tree_identifier {
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree.h	2017-11-30 13:07:24.033414996 +0000
@@ -1012,10 +1012,24 @@ #define TREE_STRING_POINTER(NODE) \
 #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
 #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
 
-/* In a VECTOR_CST node.  */
-#define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
-#define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
-#define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
+/* In a VECTOR_CST node.  See generic.texi for details.  */
+#define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
+#define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
+
+#define VECTOR_CST_LOG2_NPATTERNS(NODE) \
+  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
+#define VECTOR_CST_NPATTERNS(NODE) \
+  (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
+#define VECTOR_CST_NELTS_PER_PATTERN(NODE) \
+  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.nelts_per_pattern)
+#define VECTOR_CST_DUPLICATE_P(NODE) \
+  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 1)
+#define VECTOR_CST_STEPPED_P(NODE) \
+  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 3)
+#define VECTOR_CST_ENCODED_ELTS(NODE) \
+  (VECTOR_CST_CHECK (NODE)->vector.elts)
+#define VECTOR_CST_ENCODED_ELT(NODE, ELT) \
+  (VECTOR_CST_CHECK (NODE)->vector.elts[ELT])
 
 /* Define fields and accessors for some special-purpose tree nodes.  */
 
@@ -3885,6 +3899,14 @@ #define error_operand_p(NODE)					\
   ((NODE) == error_mark_node					\
    || ((NODE) && TREE_TYPE ((NODE)) == error_mark_node))
 
+/* Return the number of elements encoded directly in a VECTOR_CST.  */
+
+inline unsigned int
+vector_cst_encoded_nelts (const_tree t)
+{
+  return VECTOR_CST_NPATTERNS (t) * VECTOR_CST_NELTS_PER_PATTERN (t);
+}
+
 extern tree decl_assembler_name (tree);
 extern void overwrite_decl_assembler_name (tree decl, tree name);
 extern tree decl_comdat_group (const_tree);
@@ -4024,7 +4046,7 @@ extern tree force_fit_type (tree, const
 extern tree build_int_cst (tree, HOST_WIDE_INT);
 extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
 extern tree build_int_cst_type (tree, HOST_WIDE_INT);
-extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
+extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
 extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
 extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
 extern tree build_vector_from_val (tree, tree);
@@ -4271,6 +4293,9 @@ extern tree first_field (const_tree);
 
 extern bool initializer_zerop (const_tree);
 
+extern wide_int vector_cst_int_elt (const_tree, unsigned int);
+extern tree vector_cst_elt (const_tree, unsigned int);
+
 /* Given a vector VEC, return its first element if all elements are
    the same.  Otherwise return NULL_TREE.  */
 
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree.c	2017-11-30 13:07:24.032415034 +0000
@@ -66,6 +66,7 @@ Software Foundation; either version 3, o
 #include "attribs.h"
 #include "rtl.h"
 #include "regs.h"
+#include "tree-vector-builder.h"
 
 /* Tree code classes.  */
 
@@ -839,7 +840,7 @@ tree_code_size (enum tree_code code)
 	case REAL_CST:		return sizeof (tree_real_cst);
 	case FIXED_CST:		return sizeof (tree_fixed_cst);
 	case COMPLEX_CST:	return sizeof (tree_complex);
-	case VECTOR_CST:	return sizeof (tree_vector);
+	case VECTOR_CST:	gcc_unreachable ();
 	case STRING_CST:	gcc_unreachable ();
 	default:
 	  gcc_checking_assert (code >= NUM_TREE_CODES);
@@ -899,7 +900,7 @@ tree_size (const_tree node)
 
     case VECTOR_CST:
       return (sizeof (struct tree_vector)
-	      + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
+	      + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
 
     case STRING_CST:
       return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
@@ -1708,13 +1709,19 @@ cst_and_fits_in_hwi (const_tree x)
 	  && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
 }
 
-/* Build a newly constructed VECTOR_CST node of length LEN.  */
+/* Build a newly constructed VECTOR_CST with the given values of
+   (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN.  */
 
 tree
-make_vector (unsigned len MEM_STAT_DECL)
+make_vector (unsigned log2_npatterns,
+	     unsigned int nelts_per_pattern MEM_STAT_DECL)
 {
+  gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
   tree t;
-  unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
+  unsigned npatterns = 1 << log2_npatterns;
+  unsigned encoded_nelts = npatterns * nelts_per_pattern;
+  unsigned length = (sizeof (struct tree_vector)
+		     + (encoded_nelts - 1) * sizeof (tree));
 
   record_node_allocation_statistics (VECTOR_CST, length);
 
@@ -1722,7 +1729,8 @@ make_vector (unsigned len MEM_STAT_DECL)
 
   TREE_SET_CODE (t, VECTOR_CST);
   TREE_CONSTANT (t) = 1;
-  VECTOR_CST_NELTS (t) = len;
+  VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
+  VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
 
   return t;
 }
@@ -1733,29 +1741,10 @@ make_vector (unsigned len MEM_STAT_DECL)
 tree
 build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
 {
-  unsigned int nelts = vals.length ();
-  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
-  int over = 0;
-  unsigned cnt = 0;
-  tree v = make_vector (nelts);
-  TREE_TYPE (v) = type;
-
-  /* Iterate through elements and check for overflow.  */
-  for (cnt = 0; cnt < nelts; ++cnt)
-    {
-      tree value = vals[cnt];
-
-      VECTOR_CST_ELT (v, cnt) = value;
-
-      /* Don't crash if we get an address constant.  */
-      if (!CONSTANT_CLASS_P (value))
-	continue;
-
-      over |= TREE_OVERFLOW (value);
-    }
-
-  TREE_OVERFLOW (v) = over;
-  return v;
+  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
+  tree_vector_builder builder (type, vals.length (), 1);
+  builder.splice (vals);
+  return builder.build ();
 }
 
 /* Return a new VECTOR_CST node whose type is TYPE and whose values
@@ -10353,6 +10342,57 @@ build_opaque_vector_type (tree innertype
   return cand;
 }
 
+/* Return the value of element I of VECTOR_CST T as a wide_int.  */
+
+wide_int
+vector_cst_int_elt (const_tree t, unsigned int i)
+{
+  /* First handle elements that are directly encoded.  */
+  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
+  if (i < encoded_nelts)
+    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
+
+  /* Identify the pattern that contains element I and work out the index of
+     the last encoded element for that pattern.  */
+  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+  unsigned int pattern = i & (npatterns - 1);
+  unsigned int final_i = encoded_nelts - npatterns + pattern;
+
+  /* If there are no steps, the final encoded value is the right one.  */
+  if (!VECTOR_CST_STEPPED_P (t))
+    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
+
+  /* Otherwise work out the value from the last two encoded elements.  */
+  unsigned int factor = (i - final_i) >> VECTOR_CST_LOG2_NPATTERNS (t);
+  tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
+  tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
+  return wi::to_wide (v2) + factor * (wi::to_wide (v2) - wi::to_wide (v1));
+}
+
+/* Return the value of element I of VECTOR_CST T.  */
+
+tree
+vector_cst_elt (const_tree t, unsigned int i)
+{
+  /* First handle elements that are directly encoded.  */
+  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
+  if (i < encoded_nelts)
+    return VECTOR_CST_ENCODED_ELT (t, i);
+
+  /* Identify the pattern that contains element I and work out the index of
+     the last encoded element for that pattern.  */
+  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+  unsigned int pattern = i & (npatterns - 1);
+  unsigned int final_i = encoded_nelts - npatterns + pattern;
+
+  /* If there are no steps, the final encoded value is the right one.  */
+  if (!VECTOR_CST_STEPPED_P (t))
+    return VECTOR_CST_ENCODED_ELT (t, final_i);
+
+  /* Otherwise work out the value from the last two encoded elements.  */
+  return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
+			   vector_cst_int_elt (t, i));
+}
 
 /* Given an initializer INIT, return TRUE if INIT is zero or some
    aggregate of zeros.  Otherwise return FALSE.  */
@@ -12433,6 +12473,22 @@ drop_tree_overflow (tree t)
   if (TREE_CODE (t) == INTEGER_CST)
     return wide_int_to_tree (TREE_TYPE (t), wi::to_wide (t));
 
+  /* For VECTOR_CST, remove the overflow bits from the encoded elements
+     and canonicalize the result.  */
+  if (TREE_CODE (t) == VECTOR_CST)
+    {
+      tree_vector_builder builder (tree_vector_builder::unary (t, true));
+      unsigned int count = builder.encoded_nelts ();
+      for (unsigned int i = 0; i < count; ++i)
+	{
+	  tree elt = VECTOR_CST_ELT (t, i);
+	  if (TREE_OVERFLOW (elt))
+	    elt = drop_tree_overflow (elt);
+	  builder.quick_push (elt);
+	}
+      return builder.build ();
+    }
+
   /* Otherwise, as all tcc_constants are possibly shared, copy the node
      and drop the flag.  */
   t = copy_node (t);
@@ -12447,15 +12503,7 @@ drop_tree_overflow (tree t)
       if (TREE_OVERFLOW (TREE_IMAGPART (t)))
 	TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
     }
-  if (TREE_CODE (t) == VECTOR_CST)
-    {
-      for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
-	{
-	  tree& elt = VECTOR_CST_ELT (t, i);
-	  if (TREE_OVERFLOW (elt))
-	    elt = drop_tree_overflow (elt);
-	}
-    }
+
   return t;
 }
 
@@ -13954,6 +14002,138 @@ test_labels ()
   ASSERT_FALSE (FORCED_LABEL (label_decl));
 }
 
+/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
+
+static void
+check_vector_cst (vec<tree> expected, tree actual)
+{
+  for (unsigned int i = 0; i < expected.length (); ++i)
+    ASSERT_EQ (wi::to_wide (expected[i]),
+	       wi::to_wide (vector_cst_elt (actual, i)));
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
+   and that its elements match EXPECTED.  */
+
+static void
+check_vector_cst_duplicate (vec<tree> expected, tree actual,
+			    unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
+   and NPATTERNS background elements, and that its elements match
+   EXPECTED.  */
+
+static void
+check_vector_cst_fill (vec<tree> expected, tree actual,
+		       unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
+   and that its elements match EXPECTED.  */
+
+static void
+check_vector_cst_stepped (vec<tree> expected, tree actual,
+			  unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Test the creation of VECTOR_CSTs.  */
+
+static void
+test_vector_cst_patterns ()
+{
+  auto_vec<tree, 8> elements (8);
+  elements.quick_grow (8);
+  tree element_type = build_nonstandard_integer_type (16, true);
+  tree vector_type = build_vector_type (element_type, 8);
+
+  /* Test a simple linear series with a base of 0 and a step of 1:
+     { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
+  for (unsigned int i = 0; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, i);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try the same with the first element replaced by 100:
+     { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
+  elements[0] = build_int_cst (element_type, 100);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try a series that wraps around.
+     { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try a downward series:
+     { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, 80 - i);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try two interleaved series with different bases and steps:
+     { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
+  elements[1] = build_int_cst (element_type, 53);
+  for (unsigned int i = 2; i < 8; i += 2)
+    {
+      elements[i] = build_int_cst (element_type, 70 - i * 2);
+      elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
+    }
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
+
+  /* Try a duplicated value:
+     { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = elements[0];
+  check_vector_cst_duplicate (elements,
+			      build_vector (vector_type, elements), 1);
+
+  /* Try an interleaved duplicated value:
+     { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
+  elements[1] = build_int_cst (element_type, 55);
+  for (unsigned int i = 2; i < 8; ++i)
+    elements[i] = elements[i - 2];
+  check_vector_cst_duplicate (elements,
+			      build_vector (vector_type, elements), 2);
+
+  /* Try a duplicated value with 2 exceptions
+     { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
+  elements[0] = build_int_cst (element_type, 41);
+  elements[1] = build_int_cst (element_type, 97);
+  check_vector_cst_fill (elements, build_vector (vector_type, elements), 2);
+
+  /* Try with and without a step
+     { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
+  for (unsigned int i = 3; i < 8; i += 2)
+    elements[i] = build_int_cst (element_type, i * 7);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
+
+  /* Try a fully-general constant:
+     { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
+  elements[5] = build_int_cst (element_type, 9990);
+  check_vector_cst_fill (elements, build_vector (vector_type, elements), 4);
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -13962,6 +14142,7 @@ tree_c_tests ()
   test_integer_constants ();
   test_identifiers ();
   test_labels ();
+  test_vector_cst_patterns ();
 }
 
 } // namespace selftest
Index: gcc/lto-streamer-out.c
===================================================================
--- gcc/lto-streamer-out.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/lto-streamer-out.c	2017-11-30 13:07:24.029415148 +0000
@@ -747,8 +747,9 @@ #define DFS_follow_tree_edge(DEST) \
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
+      unsigned int count = vector_cst_encoded_nelts (expr);
+      for (unsigned int i = 0; i < count; ++i)
+	DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
@@ -1195,8 +1196,11 @@ #define visit(SIBLING) \
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
-      visit (VECTOR_CST_ELT (t, i));
+    {
+      unsigned int count = vector_cst_encoded_nelts (t);
+      for (unsigned int i = 0; i < count; ++i)
+	visit (VECTOR_CST_ENCODED_ELT (t, i));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
     {
Index: gcc/tree-streamer-out.c
===================================================================
--- gcc/tree-streamer-out.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree-streamer-out.c	2017-11-30 13:07:24.029415148 +0000
@@ -533,11 +533,11 @@ write_ts_common_tree_pointers (struct ou
 static void
 write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
 {
-  unsigned i;
   /* Note that the number of elements for EXPR has already been emitted
      in EXPR's header (see streamer_write_tree_header).  */
-  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-    stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
+  unsigned int count = vector_cst_encoded_nelts (expr);
+  for (unsigned int i = 0; i < count; ++i)
+    stream_write_tree (ob, VECTOR_CST_ENCODED_ELT (expr, i), ref_p);
 }
 
 
@@ -960,7 +960,12 @@ streamer_write_tree_header (struct outpu
   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
     write_identifier (ob, ob->main_stream, expr);
   else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
+    {
+      bitpack_d bp = bitpack_create (ob->main_stream);
+      bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 8);
+      bp_pack_value (&bp, VECTOR_CST_NELTS_PER_PATTERN (expr), 8);
+      streamer_write_bitpack (&bp);
+    }
   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
     streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
Index: gcc/tree-streamer-in.c
===================================================================
--- gcc/tree-streamer-in.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/tree-streamer-in.c	2017-11-30 13:07:24.029415148 +0000
@@ -592,8 +592,10 @@ streamer_alloc_tree (struct lto_input_bl
     }
   else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      HOST_WIDE_INT len = streamer_read_hwi (ib);
-      result = make_vector (len);
+      bitpack_d bp = streamer_read_bitpack (ib);
+      unsigned int log2_npatterns = bp_unpack_value (&bp, 8);
+      unsigned int nelts_per_pattern = bp_unpack_value (&bp, 8);
+      result = make_vector (log2_npatterns, nelts_per_pattern);
     }
   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
     {
@@ -650,9 +652,9 @@ lto_input_ts_common_tree_pointers (struc
 lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
 				   struct data_in *data_in, tree expr)
 {
-  unsigned i;
-  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-    VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
+  unsigned int count = vector_cst_encoded_nelts (expr);
+  for (unsigned int i = 0; i < count; ++i)
+    VECTOR_CST_ENCODED_ELT (expr, i) = stream_read_tree (ib, data_in);
 }
 
 
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/fold-const.c	2017-11-30 13:07:24.029415148 +0000
@@ -11610,9 +11610,8 @@ fold_ternary_loc (location_t loc, enum t
 		  unsigned int nelts = VECTOR_CST_NELTS (arg0);
 		  auto_vec<tree, 32> elts (nelts);
 		  elts.quick_grow (nelts);
-		  memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
-			  sizeof (tree) * nelts);
-		  elts[k] = arg1;
+		  for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
+		    elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
 		  return build_vector (type, elts);
 		}
 	    }
Index: gcc/lto/lto.c
===================================================================
--- gcc/lto/lto.c	2017-11-30 13:07:23.752425675 +0000
+++ gcc/lto/lto.c	2017-11-30 13:07:24.029415148 +0000
@@ -1065,6 +1065,12 @@ #define compare_values(X) \
 			TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
       return false;
 
+  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
+    {
+      compare_values (VECTOR_CST_LOG2_NPATTERNS);
+      compare_values (VECTOR_CST_NELTS_PER_PATTERN);
+    }
+
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
     {
       compare_values (DECL_MODE);
@@ -1281,11 +1287,12 @@ #define compare_tree_edges(E1, E2) \
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      unsigned i;
       /* Note that the number of elements for EXPR has already been emitted
 	 in EXPR's header (see streamer_write_tree_header).  */
-      for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
-	compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
+      unsigned int count = vector_cst_encoded_nelts (t1);
+      for (unsigned int i = 0; i < count; ++i)
+	compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
+			    VECTOR_CST_ENCODED_ELT (t2, i));
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-30 14:10     ` Richard Sandiford
@ 2017-12-01 11:28       ` Richard Biener
  2017-12-06 15:12         ` Richard Sandiford
  0 siblings, 1 reply; 24+ messages in thread
From: Richard Biener @ 2017-12-01 11:28 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Thu, Nov 30, 2017 at 2:18 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> Richard Sandiford <richard.sandiford@linaro.org> writes:
>> Richard Biener <richard.guenther@gmail.com> writes:
>>> On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
>>> <richard.sandiford@linaro.org> wrote:
>>>> It was clear from the SVE reviews that people were unhappy with how
>>>> "special" the variable-length case was.  One particular concern was
>>>> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
>>>> that would in turn lead to different representations of VEC_PERM_EXPRs
>>>> with constant permute vectors, and difficulties in invoking
>>>> vec_perm_const_ok.
>>>>
>>>> This is an RFC for a VECTOR_CST representation that treats each
>>>> specific constant as one instance of an arbitrary-length sequence.
>>>> The reprensentation then extends to variable-length vectors in a
>>>> natural way.
>>>>
>>>> As discussed on IRC, if a vector contains X*N elements for some
>>>> constant N and integer X>0, the main features we need are:
>>>>
>>>> 1) the ability to represent a sequence that duplicates N values
>>>>
>>>>    This is useful for SLP invariants.
>>>>
>>>> 2) the ability to represent a sequence that starts with N values and
>>>>    is followed by zeros
>>>>
>>>>    This is useful for the initial value in a double or SLP reduction
>>>>
>>>> 3) the ability to represent N interleaved series
>>>>
>>>>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>>>>
>>>> For (2), zero isn't necessarily special, since vectors used in an AND
>>>> reduction might need to fill with ones.  Also, we might need up to N
>>>> different fill values with mixed SLP operations; it isn't necessarily
>>>> safe to assume that a single fill value will always be enough.
>>>>
>>>> The same goes for (3): there's no reason in principle why the
>>>> steps in an SLP induction should all be the same (although they
>>>> do need to be at the moment).  E.g. once we support SLP on:
>>>>
>>>>   for (unsigned int i = 0; i < n; i += 2)
>>>>     {
>>>>       x[i] += 4 + i;
>>>>       x[i + 1] += 11 + i * 3;
>>>>     }
>>>>
>>>> we'll need {[4, 14], +, [2, 6]}.
>>>>
>>>> So the idea is to represent vectors as P interleaved patterns of the form:
>>>>
>>>>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>>>>
>>>> where the STEP is always zero (actually null) for non-integer vectors.
>>>> This is effectively projecting a "foreground" value of P elements
>>>> onto an arbitrary-length "background" sequenece, where the background
>>>> sequence contains P parallel linear series.
>>>>
>>>> E.g. to pick an extreme and unlikely example,
>>>>
>>>>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>>>>
>>>> has 2 patterns:
>>>>
>>>>   BASE0 = 42, BASE1 = 2, STEP = 1
>>>>   BASE0 = 99, BASE1 = 20, STEP = 10
>>>>
>>>> The more useful cases are degenerate versions of this general case.
>>>>
>>>> As far as memory consumption goes: the number of patterns needed for a
>>>> fixed-length vector with 2*N elements is always at most N; in the worst
>>>> case, we simply interleave the first N elements with the second N elements.
>>>> The worst-case increase in footprint is therefore N trees for the steps.
>>>> In practice the footprint is usually smaller than it was before, since
>>>> most constants do have a pattern.
>>>>
>>>> The patch below implements this for trees.  I have patches to use the
>>>> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
>>>> tree one is probably easiest to read.
>>>>
>>>> The patch only adds the representation.  Follow-on patches make more
>>>> use of it (and usually make things simpler; e.g. integer_zerop is no
>>>> longer a looping operation).
>>>>
>>>> Does this look better?
>>>
>>> Yes, the overall design looks good.  I wonder why you chose to have
>>> the number of patterns being a power of two?  I suppose this is
>>> to have the same number of elements from all patterns in the final
>>> vector (which is power-of-two sized)?
>>
>> Right.  The rtl and vec_perm_indices parts don't have this restriction,
>> since some ports do define non-power-of-2 vectors for internal use.
>> The problem is that, since VECTOR_CSTs are used by the FE, we need
>> to support all valid vector lengths without blowing the 16-bit field.
>> Using the same style of representation as TYPE_VECTOR_SUBPARTS seemed
>> like the safest way of doing that.
>>
>>> I wonder if there exists a vector where say a three-pattern
>>> interleaving would be smaller than a four-pattern one?
>>
>> Only in the non-power-of-2 case.
>>
>>> Given you add flags for various purposes would it make sense to
>>> overload 'step' with a regular element to avoid the storage increase
>>> in case step is unnecessary?  This makes it have three elements
>>> which is of course awkward :/
>>
>> I wondered about keeping it as an array of trees and tacking the
>> steps onto the end as an optional addition.  But the idea is that
>> tree_vector_pattern becomes the preferred way of handling constant
>> vectors, if it can be used, so it seemed neater to use in the tree
>> node too.
>
> In the end it seemed better to encode the first NPATTERNS * N
> elements of the vector, where:
>
> - N==3 if at least one pattern needs a step; otherwise
> - N==2 if at least one pattern has different BASE0s and BASE1s; otherwise
> - N==1 (i.e. the vector is a repeated sequence of NPATTERNS elements)
>
> So effectively all we're doing for the constant-length case is
> reducing the number of elements that need to be stored and processed
> (once code is converted to use the new routines).
>
> The patch below does this.  It also introduces a new class
> vector_builder<T> for building vectors of Ts, with a derived
> tree_vector_builder specifically for trees.  This allows the
> compression code to be shared between representations and also
> avoids hard-coding the auto_vec<> count everywhere.
>
> I've also fixed the selftests to get the actual and expected values
> the right way round (thanks David for noticing that).
>
> What do you think?  Does this look better?

Yes!  This is ok for trunk.

Thanks,
Richard.

> Thanks,
> Richard
>
>
> 2017-11-30  Richard Sandiford  <richard.sandiford@arm.com>
>
> gcc/
>         * doc/generic.texi (VECTOR_CST): Describe new representation of
>         vector constants.
>         * vector-builder.h: New file.
>         * tree-vector-builder.h: Likewise.
>         * tree-vector-builder.c: Likewise.
>         * Makefile.in (OBJS): Add tree-vector-builder.o.
>         * tree.def (VECTOR_CST): Update comment to refer to generic.texi.
>         * tree-core.h (tree_base): Add a vector_cst field to the u union.
>         (tree_vector): Change the number of elements to
>         vector_cst_encoded_nelts.
>         * tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
>         (VECTOR_CST_ELTS): Delete.
>         (VECTOR_CST_ELT): Redefine using vector_cst_elt.
>         (VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
>         (VECTOR_CST_NELTS_PER_PATTERN, VECTOR_CST_DUPLICATE_P): Likewise.
>         (VECTOR_CST_STRIDED_P, VECTOR_CST_ENCODED_ELTS): Likewise.
>         (VECTOR_CST_ENCODED_ELT): Likewise.
>         (vector_cst_encoded_nelts): New function.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
>         VECTOR_CST_NELTS_PER_PATTERN as arguments.
>         (vector_cst_int_elt, vector_cst_elt): Declare.
>         * tree.c: Include tree-vector-builder.h.
>         (tree_code_size): Abort if passed VECTOR_CST.
>         (tree_size): Update for new VECTOR_CST layout.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
>         VECTOR_CST_NELTS_PER_PATTERN as arguments.
>         (build_vector): Use tree_vector_builder.
>         (vector_cst_int_elt, vector_cst_elt): New functions.
>         (drop_tree_overflow): For VECTOR_CST, drop the TREE_OVERFLOW from the
>         encoded elements and then create the vector in the canonical form.
>         (check_vector_cst, test_vector_cst_patterns): New functions.
>         (tree_c_tests): Call it.
>         * lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
>         VECTOR_CST fields.
>         (hash_tree): Likewise.
>         * tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
>         (streamer_write_tree_header): Likewise.
>         * tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
>         (streamer_alloc_tree): Likewise.  Update call to make_vector.
>         * fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.
>
> gcc/lto/
>         * lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.
>
> Index: gcc/doc/generic.texi
> ===================================================================
> --- gcc/doc/generic.texi        2017-11-30 13:07:23.752425675 +0000
> +++ gcc/doc/generic.texi        2017-11-30 13:07:24.028415186 +0000
> @@ -1084,10 +1084,64 @@ These nodes are used to represent comple
>  imaginary parts respectively.
>
>  @item VECTOR_CST
> -These nodes are used to represent vector constants, whose parts are
> -constant nodes.  Each individual constant node is either an integer or a
> -double constant node.  The first operand is a @code{TREE_LIST} of the
> -constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
> +These nodes are used to represent vector constants.  Each vector
> +constant @var{v} is treated as a specific instance of an arbitrary-length
> +sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
> +interleaved patterns.  Each pattern has the form:
> +
> +@smallexample
> +@{ @var{base0}, @var{base1}, @var{base1} + @var{step}, @var{base1} + @var{step} * 2, @dots{} @}
> +@end smallexample
> +
> +The first three elements in each pattern are enough to determine the
> +values of other elements.  However, if all @var{step}s are zero,
> +only the first two elements are needed.  If in addition each @var{base1}
> +is equal to the corresponding @var{base0}, only the first element in
> +each pattern is needed.  The number of encoded elements per pattern
> +is given by @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v})}.
> +
> +For example, the constant:
> +
> +@smallexample
> +@{ 0, 1, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18 @}
> +@end smallexample
> +
> +is encoded using the interleaved sequences:
> +
> +@smallexample
> +@{ 0, 2, 3, 4, 5, 6, 7, 8 @}
> +@{ 1, 6, 8, 10, 12, 14, 16, 18 @}
> +@end smallexample
> +
> +where the sequences are represented by the following patterns:
> +
> +@smallexample
> +@var{base0} == 0, @var{base1} == 2, @var{step} == 1
> +@var{base0} == 1, @var{base1} == 6, @var{step} == 2
> +@end smallexample
> +
> +In this case:
> +
> +@smallexample
> +VECTOR_CST_NPATTERNS (@var{v}) == 2
> +VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3
> +@end smallexample
> +
> +The vector is therefore encoded using the first 6 elements, with the
> +remaining 10 elements being implicit extensions of them.
> +
> +@samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply
> +contains repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})}
> +values.  This is a shorthand for testing
> +@samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 1}.
> +
> +@samp{VECTOR_CST_STEPPED_P (@var{v})} is true if at least one
> +pattern in @var{v} has a nonzero step.  This is a shorthand for
> +testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3}.
> +
> +The utility function @code{vector_cst_elt} gives the value of an
> +arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
> +the same value as a @code{wide_int}.
>
>  @item STRING_CST
>  These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
> Index: gcc/vector-builder.h
> ===================================================================
> --- /dev/null   2017-11-30 11:13:00.800636184 +0000
> +++ gcc/vector-builder.h        2017-11-30 13:07:24.033414996 +0000
> @@ -0,0 +1,372 @@
> +/* A class for building vector constant patterns.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GCC_VECTOR_BUILDER_H
> +#define GCC_VECTOR_BUILDER_H
> +
> +/* This class is a wrapper around auto_vec<T> for building vectors of T.
> +   It aims to encode each vector as npatterns interleaved patterns,
> +   where each pattern represents a sequence:
> +
> +     { BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, BASE1 + STEP*3, ... }
> +
> +   The first three elements in each pattern provide enough information
> +   to derive the other elements.  If all patterns have a STEP of zero,
> +   we only need to encode the first two elements in each pattern.
> +   If BASE1 is also equal to BASE0 for all patterns, we only need to
> +   encode the first element in each pattern.  The number of encoded
> +   elements per pattern is given by nelts_per_pattern.
> +
> +   The class can be used in two ways:
> +
> +   1. It can be used to build a full image of the vector, which is then
> +      canonicalized.  In this case npatterns is initially the number of
> +      elements in the vector and nelts_per_pattern is initially 1.
> +
> +   2. It can be used to build a vector that already has a known encoding.
> +      This is preferred since it is more efficient and copes with
> +      variable-length vectors.  The class then canonicalizes the encoding
> +      to a simpler form if possible.
> +
> +   Derived classes provide this functionality for specific Ts.
> +   The "Derived *" arguments to some functions are pointers to the
> +   derived class, which needs to provide the following interface:
> +
> +      bool equal_p (unsigned int i1, unsigned int i2) const;
> +
> +         Return true if elements I1 and I2 are equal.
> +
> +      bool allow_steps_p () const;
> +
> +         Return true if a stepped representation is OK.  We don't allow
> +         linear series for anything other than integers, to avoid problems
> +         with rounding.
> +
> +      bool integral_p (unsigned int i) const;
> +
> +         Return true if element I can be interpreted as an integer.
> +
> +      StepType step (unsigned int i1, unsigned int i2) const;
> +
> +         Return the value of element I2 minus the value of element I1,
> +         given integral_p (I1) && integral_p (I2).  There is no fixed
> +         choice of StepType.
> +
> +      bool can_elide_p (unsigned int i) const;
> +
> +         Return true if we can drop element I, even if the retained
> +         elements are different.  This is provided for TREE_OVERFLOW
> +         handling.
> +
> +      void note_representative (unsigned int i1, unsigned int i2);
> +
> +         Record that I2 is being elided and that I1 is the last encoded
> +         element for the containing pattern.  This is again provided
> +         for TREE_OVERFLOW handling.  */
> +
> +template<typename T>
> +class vector_builder : public auto_vec<T, 32>
> +{
> +public:
> +  vector_builder (unsigned int, unsigned int, unsigned int);
> +  vector_builder (const vector_builder &);
> +
> +  unsigned int full_nelts () const { return m_full_nelts; }
> +  unsigned int npatterns () const { return m_npatterns; }
> +  unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
> +  unsigned int encoded_nelts () const;
> +  bool encoded_full_vector_p () const;
> +
> +  void reshape (unsigned int, unsigned int);
> +
> +protected:
> +  template<typename Derived>
> +  void reshape (Derived *, unsigned int, unsigned int);
> +  template<typename Derived>
> +  bool repeating_sequence_p (Derived *, unsigned int, unsigned int,
> +                            unsigned int);
> +  template<typename Derived>
> +  bool stepped_sequence_p (Derived *, unsigned int, unsigned int,
> +                          unsigned int);
> +  template<typename Derived>
> +  bool try_npatterns (Derived *, unsigned int);
> +  template<typename Derived>
> +  void finalize (Derived *);
> +
> +private:
> +  vector_builder &operator= (const vector_builder &);
> +
> +  unsigned int m_full_nelts;
> +  unsigned int m_npatterns;
> +  unsigned int m_nelts_per_pattern;
> +};
> +
> +/* Initialize a vector_builder for a vector that has FULL_NELTS elements.
> +   Initially encode it using NPATTERNS patterns with NELTS_PER_PATTERN
> +   each.  */
> +
> +template<typename T>
> +inline
> +vector_builder<T>::vector_builder (unsigned int full_nelts,
> +                                  unsigned int npatterns,
> +                                  unsigned int nelts_per_pattern)
> +  : auto_vec<T, 32> (npatterns * nelts_per_pattern),
> +    m_full_nelts (full_nelts),
> +    m_npatterns (npatterns),
> +    m_nelts_per_pattern (nelts_per_pattern)
> +{}
> +
> +/* Copy constructor.  */
> +
> +template<typename T>
> +inline
> +vector_builder<T>::vector_builder (const vector_builder &other)
> +  : auto_vec<T, 32> (other.length ()),
> +    m_full_nelts (other.m_full_nelts),
> +    m_npatterns (other.m_npatterns),
> +    m_nelts_per_pattern (other.m_nelts_per_pattern)
> +{
> +  this->splice (other);
> +}
> +
> +/* Return the number of elements that are explicitly encoded.  The vec
> +   starts with these explicitly-encoded elements and may contain additional
> +   elided elements.  */
> +
> +template<typename T>
> +inline unsigned int
> +vector_builder<T>::encoded_nelts () const
> +{
> +  return m_npatterns * m_nelts_per_pattern;
> +}
> +
> +/* Return true if every element of the vector is explicitly encoded.  */
> +
> +template<typename T>
> +inline bool
> +vector_builder<T>::encoded_full_vector_p () const
> +{
> +  return m_npatterns * m_nelts_per_pattern == m_full_nelts;
> +}
> +
> +/* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
> +   but without changing the underlying vector.  We explicitly don't
> +   truncate the vec here since callers might want to refer to the
> +   elided elements.  */
> +
> +template<typename T>
> +inline void
> +vector_builder<T>::reshape (unsigned int npatterns,
> +                           unsigned int nelts_per_pattern)
> +{
> +  m_npatterns = npatterns;
> +  m_nelts_per_pattern = nelts_per_pattern;
> +}
> +
> +/* Likewise, but invoke DERIVED->note_representative for each elided
> +   element.  */
> +
> +template<typename T>
> +template<typename Derived>
> +inline void
> +vector_builder<T>::reshape (Derived *derived, unsigned int npatterns,
> +                           unsigned int nelts_per_pattern)
> +{
> +  unsigned int old_encoded_nelts = encoded_nelts ();
> +  reshape (npatterns, nelts_per_pattern);
> +  unsigned int new_encoded_nelts = encoded_nelts ();
> +  unsigned int next = new_encoded_nelts - m_npatterns;
> +  for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
> +    {
> +      derived->note_representative (next, i);
> +      next += 1;
> +      if (next == new_encoded_nelts)
> +       next -= m_npatterns;
> +    }
> +}
> +
> +/* Return true if elements [START, END) contain a repeating sequence of
> +   STEP elements.  */
> +
> +template<typename T>
> +template<typename Derived>
> +bool
> +vector_builder<T>::repeating_sequence_p (Derived *derived, unsigned int start,
> +                                        unsigned int end, unsigned int step)
> +{
> +  for (unsigned int i = start; i < end - step; ++i)
> +    if (!derived->equal_p (i, i + step))
> +      return false;
> +  return true;
> +}
> +
> +/* Return true if elements [START, END) contain STEP interleaved linear
> +   series.  */
> +
> +template<typename T>
> +template<typename Derived>
> +bool
> +vector_builder<T>::stepped_sequence_p (Derived *derived, unsigned int start,
> +                                      unsigned int end, unsigned int step)
> +{
> +  if (!derived->allow_steps_p ())
> +    return false;
> +
> +  for (unsigned int i3 = start + step * 2; i3 < end; ++i3)
> +    {
> +      unsigned int i2 = i3 - step;
> +      unsigned int i1 = i2 - step;
> +
> +      if (!derived->integral_p (i1)
> +         || !derived->integral_p (i2)
> +         || !derived->integral_p (i3))
> +       return false;
> +
> +      if (derived->step (i1, i2) != derived->step (i2, i3))
> +       return false;
> +
> +      if (!derived->can_elide_p (i3))
> +       return false;
> +    }
> +  return true;
> +}
> +
> +/* Try to change the number of encoded patterns to NPATTERNS, returning
> +   true on success.  */
> +
> +template<typename T>
> +template<typename Derived>
> +bool
> +vector_builder<T>::try_npatterns (Derived *derived, unsigned int npatterns)
> +{
> +  if (m_nelts_per_pattern == 1)
> +    {
> +      /* See whether NPATTERNS is valid with the current 1-element-per-pattern
> +        encoding.  */
> +      if (repeating_sequence_p (derived, 0, encoded_nelts (), npatterns))
> +       {
> +         reshape (derived, npatterns, 1);
> +         return true;
> +       }
> +
> +      /* We can only increase the number of elements per pattern if all
> +        elements are still encoded explicitly.  */
> +      if (!encoded_full_vector_p ())
> +       return false;
> +    }
> +
> +  if (m_nelts_per_pattern <= 2)
> +    {
> +      /* See whether NPATTERNS is valid with a 2-element-per-pattern
> +        encoding.  */
> +      if (repeating_sequence_p (derived, npatterns, encoded_nelts (),
> +                               npatterns))
> +       {
> +         reshape (derived, npatterns, 2);
> +         return true;
> +       }
> +
> +      /* We can only increase the number of elements per pattern if all
> +        elements are still encoded explicitly.  */
> +      if (!encoded_full_vector_p ())
> +       return false;
> +    }
> +
> +  if (m_nelts_per_pattern <= 3)
> +    {
> +      /* See whether we have NPATTERNS interleaved linear series,
> +        giving a 3-element-per-pattern encoding.  */
> +      if (stepped_sequence_p (derived, npatterns, encoded_nelts (), npatterns))
> +       {
> +         reshape (derived, npatterns, 3);
> +         return true;
> +       }
> +      return false;
> +    }
> +
> +  gcc_unreachable ();
> +}
> +
> +/* Canonicalize the encoding.  */
> +
> +template<typename T>
> +template<typename Derived>
> +void
> +vector_builder<T>::finalize (Derived *derived)
> +{
> +  /* Try to whittle down the number of elements per pattern.  That is:
> +
> +     1. If we have stepped patterns whose steps are all 0, reduce the
> +        number of elements per pattern from 3 to 2.
> +
> +     2. If we have background fill values that are the same as the
> +        foreground values, reduce the number of elements per pattern
> +        from 2 to 1.  */
> +  while (m_nelts_per_pattern > 1
> +        && repeating_sequence_p (derived, encoded_nelts () - m_npatterns * 2,
> +                                 encoded_nelts (), m_npatterns))
> +    /* The last two sequences of M_NPATTERNS elements are equal,
> +       so remove the last one.  */
> +    reshape (derived, m_npatterns, m_nelts_per_pattern - 1);
> +
> +  /* Try to halve the number of patterns while doing so gives a valid pattern.
> +
> +     Each halving step tries to keep the number of elements per pattern
> +     the same.  If that isn't possible, and if all elements are still
> +     explicitly encoded, the halving step can instead increase the number
> +     of elements per pattern.
> +
> +     E.g. for:
> +
> +        { 0, 2, 3, 4, 5, 6, 7, 8 }  npatterns == 8  full_nelts == 8
> +
> +     we first realize that the second half of the sequence is not equal
> +     to the first, so we cannot maintain 1 element per pattern for
> +     npatterns == 4.  Instead we halve the number of patterns and double
> +     the number of elements per pattern, treating this as a "foreground"
> +     { 0, 2, 3, 4 } against a "background" of { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
> +
> +        { 0, 2, 3, 4 | 5, 6, 7, 8 }  npatterns == 4
> +
> +     Next we realize that this is *not* a foreround of { 0, 2 } against
> +     a background of { 3, 4 | 3, 4 ... }, so the only remaining option
> +     for reducing the number of patterns is to use a foreground of { 0, 2 }
> +     against a stepped background of { 1, 2 | 3, 4 | 5, 6 ... }.  This is
> +     valid because we still haven't elided any elements:
> +
> +        { 0, 2 | 3, 4 | 5, 6 }  npatterns == 2
> +
> +     This in turn can be reduced to a foreground of { 0 } against a stepped
> +     background of { 1 | 2 | 3 ... }:
> +
> +        { 0 | 2 | 3 }  npatterns == 1
> +
> +     This last step would not have been possible for:
> +
> +        { 0, 0 | 3, 4 | 5, 6 }  npatterns == 2.  */
> +  while ((m_npatterns & 1) == 0
> +        && try_npatterns (derived, m_npatterns / 2))
> +    continue;
> +
> +  /* At present this code doesn't (need to) cope with vector lengths
> +     that are not a power of 2.  */
> +  gcc_assert (pow2p_hwi (m_npatterns));
> +}
> +
> +#endif
> Index: gcc/tree-vector-builder.h
> ===================================================================
> --- /dev/null   2017-11-30 11:13:00.800636184 +0000
> +++ gcc/tree-vector-builder.h   2017-11-30 13:07:24.029415148 +0000
> @@ -0,0 +1,48 @@
> +/* A class for building vector tree constants.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GCC_TREE_VECTOR_BUILDER_H
> +#define GCC_TREE_VECTOR_BUILDER_H
> +
> +#include "vector-builder.h"
> +
> +/* This class is used to build VECTOR_CSTs from a sequence of elements.
> +   See vector_builder for more details.  */
> +class tree_vector_builder : public vector_builder<tree>
> +{
> +  friend class vector_builder<tree>;
> +
> +public:
> +  tree_vector_builder (tree, unsigned int, unsigned int);
> +  tree build ();
> +
> +  static tree_vector_builder unary (tree, bool);
> +
> +private:
> +  bool equal_p (unsigned int, unsigned int) const;
> +  bool allow_steps_p () const;
> +  bool integral_p (unsigned int) const;
> +  wide_int step (unsigned int, unsigned int) const;
> +  bool can_elide_p (unsigned int) const;
> +  void note_representative (unsigned int, unsigned int);
> +
> +  tree m_type;
> +};
> +
> +#endif
> Index: gcc/tree-vector-builder.c
> ===================================================================
> --- /dev/null   2017-11-30 11:13:00.800636184 +0000
> +++ gcc/tree-vector-builder.c   2017-11-30 13:07:24.029415148 +0000
> @@ -0,0 +1,131 @@
> +/* A class for building vector tree constants.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#include "config.h"
> +#include "system.h"
> +#include "coretypes.h"
> +#include "tree-vector-builder.h"
> +#include "tree.h"
> +#include "fold-const.h"
> +
> +/* Create a new builder for a vector of type TYPE.  Initially encode the
> +   value as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements
> +   each.  */
> +
> +tree_vector_builder::tree_vector_builder (tree type, unsigned int npatterns,
> +                                         unsigned int nelts_per_pattern)
> +  : vector_builder<tree> (TYPE_VECTOR_SUBPARTS (type), npatterns,
> +                         nelts_per_pattern),
> +    m_type (type)
> +{
> +}
> +
> +/* Create a new builder for a unary operation on VECTOR_CST T.
> +   ALLOW_STEPPED_P is true if the operation can handle stepped encodings
> +   directly, without having to expand the full sequence.  */
> +
> +tree_vector_builder
> +tree_vector_builder::unary (tree t, bool allow_stepped_p)
> +{
> +  tree type = TREE_TYPE (t);
> +  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +  unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (t);
> +  if (!allow_stepped_p && nelts_per_pattern > 2)
> +    {
> +      npatterns = TYPE_VECTOR_SUBPARTS (type);
> +      nelts_per_pattern = 1;
> +    }
> +  return tree_vector_builder (type, npatterns, nelts_per_pattern);
> +}
> +
> +/* Return true if elements I1 and I2 are equal.  */
> +
> +inline bool
> +tree_vector_builder::equal_p (unsigned int i1, unsigned int i2) const
> +{
> +  return operand_equal_p ((*this)[i1], (*this)[i2], 0);
> +}
> +
> +/* Return true if a stepped representation is OK.  We don't allow
> +   linear series for anything other than integers, to avoid problems
> +   with rounding.  */
> +
> +inline bool
> +tree_vector_builder::allow_steps_p () const
> +{
> +  return INTEGRAL_TYPE_P (TREE_TYPE (m_type));
> +}
> +
> +/* Return true if element I can be interpreted as an integer.  */
> +
> +inline bool
> +tree_vector_builder::integral_p (unsigned int i) const
> +{
> +  return TREE_CODE ((*this)[i]) == INTEGER_CST;
> +}
> +
> +/* Return the value of element I2 minus the value of element I1.
> +   Both elements are known to be INTEGER_CSTs.  */
> +
> +inline wide_int
> +tree_vector_builder::step (unsigned int i1, unsigned int i2) const
> +{
> +  return wi::to_wide ((*this)[i2]) - wi::to_wide ((*this)[i1]);
> +}
> +
> +/* Return true if we can drop element I, even if the retained elements
> +   are different.  Return false if this would mean losing overflow
> +   information.  */
> +
> +inline bool
> +tree_vector_builder::can_elide_p (unsigned int i) const
> +{
> +  tree elt = (*this)[i];
> +  return !CONSTANT_CLASS_P (elt) || !TREE_OVERFLOW (elt);
> +}
> +
> +/* Record that I2 is being elided and that I1 is the last encoded element
> +   for the containing pattern.  */
> +
> +inline void
> +tree_vector_builder::note_representative (unsigned int i1, unsigned int i2)
> +{
> +  tree elt2 = (*this)[i2];
> +  if (CONSTANT_CLASS_P (elt2) && TREE_OVERFLOW (elt2))
> +    {
> +      tree elt1 = (*this)[i1];
> +      gcc_assert (operand_equal_p (elt1, elt2, 0));
> +      if (!TREE_OVERFLOW (elt2))
> +       (*this)[i1] = elt2;
> +    }
> +}
> +
> +/* Return a VECTOR_CST for the current constant.  */
> +
> +tree
> +tree_vector_builder::build ()
> +{
> +  finalize (this);
> +  gcc_assert (pow2p_hwi (npatterns ()));
> +  tree v = make_vector (exact_log2 (npatterns ()), nelts_per_pattern ());
> +  TREE_TYPE (v) = m_type;
> +  memcpy (VECTOR_CST_ENCODED_ELTS (v), address (),
> +         encoded_nelts () * sizeof (tree));
> +  return v;
> +}
> Index: gcc/Makefile.in
> ===================================================================
> --- gcc/Makefile.in     2017-11-29 11:06:34.324730917 +0000
> +++ gcc/Makefile.in     2017-11-30 13:07:24.027415224 +0000
> @@ -1568,6 +1568,7 @@ OBJS = \
>         tree-vect-loop-manip.o \
>         tree-vect-slp.o \
>         tree-vectorizer.o \
> +       tree-vector-builder.o \
>         tree-vrp.o \
>         tree.o \
>         typed-splay-tree.o \
> Index: gcc/tree.def
> ===================================================================
> --- gcc/tree.def        2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree.def        2017-11-30 13:07:24.032415034 +0000
> @@ -301,7 +301,7 @@ DEFTREECODE (FIXED_CST, "fixed_cst", tcc
>     whose contents are other constant nodes.  */
>  DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
>
> -/* Contents are in VECTOR_CST_ELTS field.  */
> +/* See generic.texi for details.  */
>  DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
>
>  /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
> Index: gcc/tree-core.h
> ===================================================================
> --- gcc/tree-core.h     2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree-core.h     2017-11-30 13:07:24.029415148 +0000
> @@ -976,8 +976,17 @@ struct GTY(()) tree_base {
>      /* VEC length.  This field is only used with TREE_VEC.  */
>      int length;
>
> -    /* Number of elements.  This field is only used with VECTOR_CST.  */
> -    unsigned int nelts;
> +    /* This field is only used with VECTOR_CST.  */
> +    struct {
> +      /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
> +      unsigned int log2_npatterns : 8;
> +
> +      /* The value of VECTOR_CST_NELTS_PER_PATTERN.  */
> +      unsigned int nelts_per_pattern : 8;
> +
> +      /* For future expansion.  */
> +      unsigned int unused : 16;
> +    } vector_cst;
>
>      /* SSA version number.  This field is only used with SSA_NAME.  */
>      unsigned int version;
> @@ -1333,7 +1342,7 @@ struct GTY(()) tree_complex {
>
>  struct GTY(()) tree_vector {
>    struct tree_typed typed;
> -  tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
> +  tree GTY ((length ("vector_cst_encoded_nelts ((tree) &%h)"))) elts[1];
>  };
>
>  struct GTY(()) tree_identifier {
> Index: gcc/tree.h
> ===================================================================
> --- gcc/tree.h  2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree.h  2017-11-30 13:07:24.033414996 +0000
> @@ -1012,10 +1012,24 @@ #define TREE_STRING_POINTER(NODE) \
>  #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
>  #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
>
> -/* In a VECTOR_CST node.  */
> -#define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
> -#define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
> -#define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
> +/* In a VECTOR_CST node.  See generic.texi for details.  */
> +#define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
> +#define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
> +
> +#define VECTOR_CST_LOG2_NPATTERNS(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
> +#define VECTOR_CST_NPATTERNS(NODE) \
> +  (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
> +#define VECTOR_CST_NELTS_PER_PATTERN(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.nelts_per_pattern)
> +#define VECTOR_CST_DUPLICATE_P(NODE) \
> +  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 1)
> +#define VECTOR_CST_STEPPED_P(NODE) \
> +  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 3)
> +#define VECTOR_CST_ENCODED_ELTS(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->vector.elts)
> +#define VECTOR_CST_ENCODED_ELT(NODE, ELT) \
> +  (VECTOR_CST_CHECK (NODE)->vector.elts[ELT])
>
>  /* Define fields and accessors for some special-purpose tree nodes.  */
>
> @@ -3885,6 +3899,14 @@ #define error_operand_p(NODE)                                    \
>    ((NODE) == error_mark_node                                   \
>     || ((NODE) && TREE_TYPE ((NODE)) == error_mark_node))
>
> +/* Return the number of elements encoded directly in a VECTOR_CST.  */
> +
> +inline unsigned int
> +vector_cst_encoded_nelts (const_tree t)
> +{
> +  return VECTOR_CST_NPATTERNS (t) * VECTOR_CST_NELTS_PER_PATTERN (t);
> +}
> +
>  extern tree decl_assembler_name (tree);
>  extern void overwrite_decl_assembler_name (tree decl, tree name);
>  extern tree decl_comdat_group (const_tree);
> @@ -4024,7 +4046,7 @@ extern tree force_fit_type (tree, const
>  extern tree build_int_cst (tree, HOST_WIDE_INT);
>  extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
>  extern tree build_int_cst_type (tree, HOST_WIDE_INT);
> -extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
> +extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
>  extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
>  extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
>  extern tree build_vector_from_val (tree, tree);
> @@ -4271,6 +4293,9 @@ extern tree first_field (const_tree);
>
>  extern bool initializer_zerop (const_tree);
>
> +extern wide_int vector_cst_int_elt (const_tree, unsigned int);
> +extern tree vector_cst_elt (const_tree, unsigned int);
> +
>  /* Given a vector VEC, return its first element if all elements are
>     the same.  Otherwise return NULL_TREE.  */
>
> Index: gcc/tree.c
> ===================================================================
> --- gcc/tree.c  2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree.c  2017-11-30 13:07:24.032415034 +0000
> @@ -66,6 +66,7 @@ Software Foundation; either version 3, o
>  #include "attribs.h"
>  #include "rtl.h"
>  #include "regs.h"
> +#include "tree-vector-builder.h"
>
>  /* Tree code classes.  */
>
> @@ -839,7 +840,7 @@ tree_code_size (enum tree_code code)
>         case REAL_CST:          return sizeof (tree_real_cst);
>         case FIXED_CST:         return sizeof (tree_fixed_cst);
>         case COMPLEX_CST:       return sizeof (tree_complex);
> -       case VECTOR_CST:        return sizeof (tree_vector);
> +       case VECTOR_CST:        gcc_unreachable ();
>         case STRING_CST:        gcc_unreachable ();
>         default:
>           gcc_checking_assert (code >= NUM_TREE_CODES);
> @@ -899,7 +900,7 @@ tree_size (const_tree node)
>
>      case VECTOR_CST:
>        return (sizeof (struct tree_vector)
> -             + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
> +             + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
>
>      case STRING_CST:
>        return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
> @@ -1708,13 +1709,19 @@ cst_and_fits_in_hwi (const_tree x)
>           && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
>  }
>
> -/* Build a newly constructed VECTOR_CST node of length LEN.  */
> +/* Build a newly constructed VECTOR_CST with the given values of
> +   (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN.  */
>
>  tree
> -make_vector (unsigned len MEM_STAT_DECL)
> +make_vector (unsigned log2_npatterns,
> +            unsigned int nelts_per_pattern MEM_STAT_DECL)
>  {
> +  gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
>    tree t;
> -  unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
> +  unsigned npatterns = 1 << log2_npatterns;
> +  unsigned encoded_nelts = npatterns * nelts_per_pattern;
> +  unsigned length = (sizeof (struct tree_vector)
> +                    + (encoded_nelts - 1) * sizeof (tree));
>
>    record_node_allocation_statistics (VECTOR_CST, length);
>
> @@ -1722,7 +1729,8 @@ make_vector (unsigned len MEM_STAT_DECL)
>
>    TREE_SET_CODE (t, VECTOR_CST);
>    TREE_CONSTANT (t) = 1;
> -  VECTOR_CST_NELTS (t) = len;
> +  VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
> +  VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
>
>    return t;
>  }
> @@ -1733,29 +1741,10 @@ make_vector (unsigned len MEM_STAT_DECL)
>  tree
>  build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
>  {
> -  unsigned int nelts = vals.length ();
> -  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
> -  int over = 0;
> -  unsigned cnt = 0;
> -  tree v = make_vector (nelts);
> -  TREE_TYPE (v) = type;
> -
> -  /* Iterate through elements and check for overflow.  */
> -  for (cnt = 0; cnt < nelts; ++cnt)
> -    {
> -      tree value = vals[cnt];
> -
> -      VECTOR_CST_ELT (v, cnt) = value;
> -
> -      /* Don't crash if we get an address constant.  */
> -      if (!CONSTANT_CLASS_P (value))
> -       continue;
> -
> -      over |= TREE_OVERFLOW (value);
> -    }
> -
> -  TREE_OVERFLOW (v) = over;
> -  return v;
> +  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
> +  tree_vector_builder builder (type, vals.length (), 1);
> +  builder.splice (vals);
> +  return builder.build ();
>  }
>
>  /* Return a new VECTOR_CST node whose type is TYPE and whose values
> @@ -10353,6 +10342,57 @@ build_opaque_vector_type (tree innertype
>    return cand;
>  }
>
> +/* Return the value of element I of VECTOR_CST T as a wide_int.  */
> +
> +wide_int
> +vector_cst_int_elt (const_tree t, unsigned int i)
> +{
> +  /* First handle elements that are directly encoded.  */
> +  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
> +  if (i < encoded_nelts)
> +    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
> +
> +  /* Identify the pattern that contains element I and work out the index of
> +     the last encoded element for that pattern.  */
> +  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +  unsigned int pattern = i & (npatterns - 1);
> +  unsigned int final_i = encoded_nelts - npatterns + pattern;
> +
> +  /* If there are no steps, the final encoded value is the right one.  */
> +  if (!VECTOR_CST_STEPPED_P (t))
> +    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
> +
> +  /* Otherwise work out the value from the last two encoded elements.  */
> +  unsigned int factor = (i - final_i) >> VECTOR_CST_LOG2_NPATTERNS (t);
> +  tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
> +  tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
> +  return wi::to_wide (v2) + factor * (wi::to_wide (v2) - wi::to_wide (v1));
> +}
> +
> +/* Return the value of element I of VECTOR_CST T.  */
> +
> +tree
> +vector_cst_elt (const_tree t, unsigned int i)
> +{
> +  /* First handle elements that are directly encoded.  */
> +  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
> +  if (i < encoded_nelts)
> +    return VECTOR_CST_ENCODED_ELT (t, i);
> +
> +  /* Identify the pattern that contains element I and work out the index of
> +     the last encoded element for that pattern.  */
> +  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +  unsigned int pattern = i & (npatterns - 1);
> +  unsigned int final_i = encoded_nelts - npatterns + pattern;
> +
> +  /* If there are no steps, the final encoded value is the right one.  */
> +  if (!VECTOR_CST_STEPPED_P (t))
> +    return VECTOR_CST_ENCODED_ELT (t, final_i);
> +
> +  /* Otherwise work out the value from the last two encoded elements.  */
> +  return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
> +                          vector_cst_int_elt (t, i));
> +}
>
>  /* Given an initializer INIT, return TRUE if INIT is zero or some
>     aggregate of zeros.  Otherwise return FALSE.  */
> @@ -12433,6 +12473,22 @@ drop_tree_overflow (tree t)
>    if (TREE_CODE (t) == INTEGER_CST)
>      return wide_int_to_tree (TREE_TYPE (t), wi::to_wide (t));
>
> +  /* For VECTOR_CST, remove the overflow bits from the encoded elements
> +     and canonicalize the result.  */
> +  if (TREE_CODE (t) == VECTOR_CST)
> +    {
> +      tree_vector_builder builder (tree_vector_builder::unary (t, true));
> +      unsigned int count = builder.encoded_nelts ();
> +      for (unsigned int i = 0; i < count; ++i)
> +       {
> +         tree elt = VECTOR_CST_ELT (t, i);
> +         if (TREE_OVERFLOW (elt))
> +           elt = drop_tree_overflow (elt);
> +         builder.quick_push (elt);
> +       }
> +      return builder.build ();
> +    }
> +
>    /* Otherwise, as all tcc_constants are possibly shared, copy the node
>       and drop the flag.  */
>    t = copy_node (t);
> @@ -12447,15 +12503,7 @@ drop_tree_overflow (tree t)
>        if (TREE_OVERFLOW (TREE_IMAGPART (t)))
>         TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
>      }
> -  if (TREE_CODE (t) == VECTOR_CST)
> -    {
> -      for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> -       {
> -         tree& elt = VECTOR_CST_ELT (t, i);
> -         if (TREE_OVERFLOW (elt))
> -           elt = drop_tree_overflow (elt);
> -       }
> -    }
> +
>    return t;
>  }
>
> @@ -13954,6 +14002,138 @@ test_labels ()
>    ASSERT_FALSE (FORCED_LABEL (label_decl));
>  }
>
> +/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
> +
> +static void
> +check_vector_cst (vec<tree> expected, tree actual)
> +{
> +  for (unsigned int i = 0; i < expected.length (); ++i)
> +    ASSERT_EQ (wi::to_wide (expected[i]),
> +              wi::to_wide (vector_cst_elt (actual, i)));
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
> +   and that its elements match EXPECTED.  */
> +
> +static void
> +check_vector_cst_duplicate (vec<tree> expected, tree actual,
> +                           unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
> +   and NPATTERNS background elements, and that its elements match
> +   EXPECTED.  */
> +
> +static void
> +check_vector_cst_fill (vec<tree> expected, tree actual,
> +                      unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
> +   and that its elements match EXPECTED.  */
> +
> +static void
> +check_vector_cst_stepped (vec<tree> expected, tree actual,
> +                         unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Test the creation of VECTOR_CSTs.  */
> +
> +static void
> +test_vector_cst_patterns ()
> +{
> +  auto_vec<tree, 8> elements (8);
> +  elements.quick_grow (8);
> +  tree element_type = build_nonstandard_integer_type (16, true);
> +  tree vector_type = build_vector_type (element_type, 8);
> +
> +  /* Test a simple linear series with a base of 0 and a step of 1:
> +     { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
> +  for (unsigned int i = 0; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, i);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try the same with the first element replaced by 100:
> +     { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
> +  elements[0] = build_int_cst (element_type, 100);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try a series that wraps around.
> +     { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try a downward series:
> +     { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, 80 - i);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try two interleaved series with different bases and steps:
> +     { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
> +  elements[1] = build_int_cst (element_type, 53);
> +  for (unsigned int i = 2; i < 8; i += 2)
> +    {
> +      elements[i] = build_int_cst (element_type, 70 - i * 2);
> +      elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
> +    }
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try a duplicated value:
> +     { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = elements[0];
> +  check_vector_cst_duplicate (elements,
> +                             build_vector (vector_type, elements), 1);
> +
> +  /* Try an interleaved duplicated value:
> +     { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
> +  elements[1] = build_int_cst (element_type, 55);
> +  for (unsigned int i = 2; i < 8; ++i)
> +    elements[i] = elements[i - 2];
> +  check_vector_cst_duplicate (elements,
> +                             build_vector (vector_type, elements), 2);
> +
> +  /* Try a duplicated value with 2 exceptions
> +     { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
> +  elements[0] = build_int_cst (element_type, 41);
> +  elements[1] = build_int_cst (element_type, 97);
> +  check_vector_cst_fill (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try with and without a step
> +     { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
> +  for (unsigned int i = 3; i < 8; i += 2)
> +    elements[i] = build_int_cst (element_type, i * 7);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try a fully-general constant:
> +     { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
> +  elements[5] = build_int_cst (element_type, 9990);
> +  check_vector_cst_fill (elements, build_vector (vector_type, elements), 4);
> +}
> +
>  /* Run all of the selftests within this file.  */
>
>  void
> @@ -13962,6 +14142,7 @@ tree_c_tests ()
>    test_integer_constants ();
>    test_identifiers ();
>    test_labels ();
> +  test_vector_cst_patterns ();
>  }
>
>  } // namespace selftest
> Index: gcc/lto-streamer-out.c
> ===================================================================
> --- gcc/lto-streamer-out.c      2017-11-30 13:07:23.752425675 +0000
> +++ gcc/lto-streamer-out.c      2017-11-30 13:07:24.029415148 +0000
> @@ -747,8 +747,9 @@ #define DFS_follow_tree_edge(DEST) \
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -       DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
> +      unsigned int count = vector_cst_encoded_nelts (expr);
> +      for (unsigned int i = 0; i < count; ++i)
> +       DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
> @@ -1195,8 +1196,11 @@ #define visit(SIBLING) \
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> -    for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> -      visit (VECTOR_CST_ELT (t, i));
> +    {
> +      unsigned int count = vector_cst_encoded_nelts (t);
> +      for (unsigned int i = 0; i < count; ++i)
> +       visit (VECTOR_CST_ENCODED_ELT (t, i));
> +    }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
>      {
> Index: gcc/tree-streamer-out.c
> ===================================================================
> --- gcc/tree-streamer-out.c     2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree-streamer-out.c     2017-11-30 13:07:24.029415148 +0000
> @@ -533,11 +533,11 @@ write_ts_common_tree_pointers (struct ou
>  static void
>  write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
>  {
> -  unsigned i;
>    /* Note that the number of elements for EXPR has already been emitted
>       in EXPR's header (see streamer_write_tree_header).  */
> -  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -    stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
> +  unsigned int count = vector_cst_encoded_nelts (expr);
> +  for (unsigned int i = 0; i < count; ++i)
> +    stream_write_tree (ob, VECTOR_CST_ENCODED_ELT (expr, i), ref_p);
>  }
>
>
> @@ -960,7 +960,12 @@ streamer_write_tree_header (struct outpu
>    else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
>      write_identifier (ob, ob->main_stream, expr);
>    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> -    streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
> +    {
> +      bitpack_d bp = bitpack_create (ob->main_stream);
> +      bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 8);
> +      bp_pack_value (&bp, VECTOR_CST_NELTS_PER_PATTERN (expr), 8);
> +      streamer_write_bitpack (&bp);
> +    }
>    else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
>      streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
>    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
> Index: gcc/tree-streamer-in.c
> ===================================================================
> --- gcc/tree-streamer-in.c      2017-11-30 13:07:23.752425675 +0000
> +++ gcc/tree-streamer-in.c      2017-11-30 13:07:24.029415148 +0000
> @@ -592,8 +592,10 @@ streamer_alloc_tree (struct lto_input_bl
>      }
>    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      HOST_WIDE_INT len = streamer_read_hwi (ib);
> -      result = make_vector (len);
> +      bitpack_d bp = streamer_read_bitpack (ib);
> +      unsigned int log2_npatterns = bp_unpack_value (&bp, 8);
> +      unsigned int nelts_per_pattern = bp_unpack_value (&bp, 8);
> +      result = make_vector (log2_npatterns, nelts_per_pattern);
>      }
>    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
>      {
> @@ -650,9 +652,9 @@ lto_input_ts_common_tree_pointers (struc
>  lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
>                                    struct data_in *data_in, tree expr)
>  {
> -  unsigned i;
> -  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -    VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
> +  unsigned int count = vector_cst_encoded_nelts (expr);
> +  for (unsigned int i = 0; i < count; ++i)
> +    VECTOR_CST_ENCODED_ELT (expr, i) = stream_read_tree (ib, data_in);
>  }
>
>
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-11-30 13:07:23.752425675 +0000
> +++ gcc/fold-const.c    2017-11-30 13:07:24.029415148 +0000
> @@ -11610,9 +11610,8 @@ fold_ternary_loc (location_t loc, enum t
>                   unsigned int nelts = VECTOR_CST_NELTS (arg0);
>                   auto_vec<tree, 32> elts (nelts);
>                   elts.quick_grow (nelts);
> -                 memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
> -                         sizeof (tree) * nelts);
> -                 elts[k] = arg1;
> +                 for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
> +                   elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
>                   return build_vector (type, elts);
>                 }
>             }
> Index: gcc/lto/lto.c
> ===================================================================
> --- gcc/lto/lto.c       2017-11-30 13:07:23.752425675 +0000
> +++ gcc/lto/lto.c       2017-11-30 13:07:24.029415148 +0000
> @@ -1065,6 +1065,12 @@ #define compare_values(X) \
>                         TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
>        return false;
>
> +  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> +    {
> +      compare_values (VECTOR_CST_LOG2_NPATTERNS);
> +      compare_values (VECTOR_CST_NELTS_PER_PATTERN);
> +    }
> +
>    if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
>      {
>        compare_values (DECL_MODE);
> @@ -1281,11 +1287,12 @@ #define compare_tree_edges(E1, E2) \
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      unsigned i;
>        /* Note that the number of elements for EXPR has already been emitted
>          in EXPR's header (see streamer_write_tree_header).  */
> -      for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
> -       compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
> +      unsigned int count = vector_cst_encoded_nelts (t1);
> +      for (unsigned int i = 0; i < count; ++i)
> +       compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
> +                           VECTOR_CST_ENCODED_ELT (t2, i));
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-11-29 15:11 ` David Malcolm
@ 2017-12-06 14:40   ` Richard Sandiford
  2017-12-06 15:10     ` ASSERT_* macros (was Re: RFC: Variable-length VECTOR_CSTs) David Malcolm
  2018-04-30 15:42     ` [PATCH] selftest: remove "Yoda ordering" in assertions David Malcolm
  0 siblings, 2 replies; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 14:40 UTC (permalink / raw)
  To: David Malcolm; +Cc: gcc-patches

David Malcolm <dmalcolm@redhat.com> writes:
> On Wed, 2017-11-29 at 11:57 +0000, Richard Sandiford wrote:
>
> [...]
>
> I can't really comment on the representation ideas, but I'm always
> happy to see new selftests...
>
> *************** test_labels ()
>> *** 13954,13959 ****
>> --- 14179,14350 ----
>>     ASSERT_FALSE (FORCED_LABEL (label_decl));
>>   }
>>   
>> + /* Check that VECTOR_CST Y contains the elements in X.  */
>> + 
>> + static void
>> + check_vector_cst (vec<tree> x, tree y)
>> + {
>> +   for (unsigned int i = 0; i < x.length (); ++i)
>> +     ASSERT_EQ (wi::to_wide (x[i]), wi::to_wide (vector_cst_elt (y,
>> i)));
>> + }
>
> ...a couple of nits/suggestions:
>
> (a) How about renaming "x" to "expected"?  Maybe rename "y" to
> "actual"?  (to better document the sense of the test).

Good idea.  I keep getting the ASSERT_* argument order for actual
vs. expected confused, so having more explicit names would help.
Done in the updated patch.

However, I needed the patch below to use those names, since "actual" and
"expected" are also used internally by the macros.  I tried to protect
other "user-level" names too.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Bordering on obvious, but just in case: OK to install?

> (b) At first glance, I wondered if this routine should also have
> something like:
>
>   ASSERT_EQ (expected.length (), VECTOR_CST_NELTS (actual));
>
> Though that seems to come from the vector type, and it's always 8 in
> these examples, so I'm not sure.

Guess it can't hurt :-).  Done in the updated patch.

Thanks,
Richard


gcc/
2017-12-05  Richard Sandiford  <richard.sandiford@linaro.org>

	* selftest.h (ASSERT_TRUE_AT, ASSERT_FALSE_AT, ASSERT_EQ_AT)
	(ASSERT_NE, ASSERT_PRED1): Add underscores to local variable names
	* selftest-rtl.h (ASSERT_RTX_EQ, ASSERT_RTX_PTR_EQ): Likewise.

Index: gcc/selftest.h
===================================================================
--- gcc/selftest.h	2017-11-29 11:06:34.324730917 +0000
+++ gcc/selftest.h	2017-12-06 14:34:28.377084919 +0000
@@ -219,12 +219,12 @@ #define ASSERT_TRUE(EXPR)				\
 
 #define ASSERT_TRUE_AT(LOC, EXPR)			\
   SELFTEST_BEGIN_STMT					\
-  const char *desc = "ASSERT_TRUE (" #EXPR ")";		\
-  bool actual = (EXPR);					\
-  if (actual)						\
-    ::selftest::pass ((LOC), desc);			\
+  const char *desc_ = "ASSERT_TRUE (" #EXPR ")";	\
+  bool actual_ = (EXPR);				\
+  if (actual_)						\
+    ::selftest::pass ((LOC), desc_);			\
   else							\
-    ::selftest::fail ((LOC), desc);			\
+    ::selftest::fail ((LOC), desc_);			\
   SELFTEST_END_STMT
 
 /* Evaluate EXPR and coerce to bool, calling
@@ -239,12 +239,12 @@ #define ASSERT_FALSE(EXPR)					\
 
 #define ASSERT_FALSE_AT(LOC, EXPR)				\
   SELFTEST_BEGIN_STMT						\
-  const char *desc = "ASSERT_FALSE (" #EXPR ")";			\
-  bool actual = (EXPR);							\
-  if (actual)								\
-    ::selftest::fail ((LOC), desc);			\
-  else									\
-    ::selftest::pass ((LOC), desc);					\
+  const char *desc_ = "ASSERT_FALSE (" #EXPR ")";		\
+  bool actual_ = (EXPR);					\
+  if (actual_)							\
+    ::selftest::fail ((LOC), desc_);				\
+  else								\
+    ::selftest::pass ((LOC), desc_);				\
   SELFTEST_END_STMT
 
 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
@@ -259,11 +259,11 @@ #define ASSERT_EQ(EXPECTED, ACTUAL) \
 
 #define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL)		       \
   SELFTEST_BEGIN_STMT					       \
-  const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
+  const char *desc_ = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
   if ((EXPECTED) == (ACTUAL))				       \
-    ::selftest::pass ((LOC), desc);			       \
+    ::selftest::pass ((LOC), desc_);			       \
   else							       \
-    ::selftest::fail ((LOC), desc);			       \
+    ::selftest::fail ((LOC), desc_);			       \
   SELFTEST_END_STMT
 
 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
@@ -272,11 +272,11 @@ #define ASSERT_EQ_AT(LOC, EXPECTED, ACTU
 
 #define ASSERT_NE(EXPECTED, ACTUAL)			       \
   SELFTEST_BEGIN_STMT					       \
-  const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
+  const char *desc_ = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
   if ((EXPECTED) != (ACTUAL))				       \
-    ::selftest::pass (SELFTEST_LOCATION, desc);			       \
+    ::selftest::pass (SELFTEST_LOCATION, desc_);	       \
   else							       \
-    ::selftest::fail (SELFTEST_LOCATION, desc);			       \
+    ::selftest::fail (SELFTEST_LOCATION, desc_);	       \
   SELFTEST_END_STMT
 
 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
@@ -312,14 +312,14 @@ #define ASSERT_STR_CONTAINS(HAYSTACK, NE
 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
    ::selftest::fail if it is false.  */
 
-#define ASSERT_PRED1(PRED1, VAL1)			\
-  SELFTEST_BEGIN_STMT					\
-  const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")";	\
-  bool actual = (PRED1) (VAL1);				\
-  if (actual)						\
-    ::selftest::pass (SELFTEST_LOCATION, desc);			\
-  else							\
-    ::selftest::fail (SELFTEST_LOCATION, desc);			\
+#define ASSERT_PRED1(PRED1, VAL1)				\
+  SELFTEST_BEGIN_STMT						\
+  const char *desc_ = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")";	\
+  bool actual_ = (PRED1) (VAL1);				\
+  if (actual_)							\
+    ::selftest::pass (SELFTEST_LOCATION, desc_);		\
+  else								\
+    ::selftest::fail (SELFTEST_LOCATION, desc_);		\
   SELFTEST_END_STMT
 
 #define SELFTEST_BEGIN_STMT do {
Index: gcc/selftest-rtl.h
===================================================================
--- gcc/selftest-rtl.h	2017-11-01 09:41:40.726615206 +0000
+++ gcc/selftest-rtl.h	2017-12-06 14:34:28.377084919 +0000
@@ -49,8 +49,8 @@ #define ASSERT_RTL_DUMP_EQ_WITH_REUSE(EX
 
 #define ASSERT_RTX_EQ(EXPECTED, ACTUAL) 				\
   SELFTEST_BEGIN_STMT							\
-  const char *desc = "ASSERT_RTX_EQ (" #EXPECTED ", " #ACTUAL ")";	\
-  ::selftest::assert_rtx_eq_at (SELFTEST_LOCATION, desc, (EXPECTED),	\
+  const char *desc_ = "ASSERT_RTX_EQ (" #EXPECTED ", " #ACTUAL ")";	\
+  ::selftest::assert_rtx_eq_at (SELFTEST_LOCATION, desc_, (EXPECTED),	\
 				(ACTUAL));				\
   SELFTEST_END_STMT
 
@@ -62,8 +62,8 @@ extern void assert_rtx_eq_at (const loca
 
 #define ASSERT_RTX_PTR_EQ(EXPECTED, ACTUAL) \
   SELFTEST_BEGIN_STMT							\
-  const char *desc = "ASSERT_RTX_PTR_EQ (" #EXPECTED ", " #ACTUAL ")";  \
-  ::selftest::assert_rtx_ptr_eq_at (SELFTEST_LOCATION, desc, (EXPECTED), \
+  const char *desc_ = "ASSERT_RTX_PTR_EQ (" #EXPECTED ", " #ACTUAL ")";  \
+  ::selftest::assert_rtx_ptr_eq_at (SELFTEST_LOCATION, desc_, (EXPECTED), \
 				    (ACTUAL));				\
   SELFTEST_END_STMT
 

^ permalink raw reply	[flat|nested] 24+ messages in thread

* ASSERT_* macros (was Re: RFC: Variable-length VECTOR_CSTs)
  2017-12-06 14:40   ` Richard Sandiford
@ 2017-12-06 15:10     ` David Malcolm
  2018-04-30 15:42     ` [PATCH] selftest: remove "Yoda ordering" in assertions David Malcolm
  1 sibling, 0 replies; 24+ messages in thread
From: David Malcolm @ 2017-12-06 15:10 UTC (permalink / raw)
  To: Richard Sandiford; +Cc: gcc-patches

On Wed, 2017-12-06 at 14:40 +0000, Richard Sandiford wrote:
> David Malcolm <dmalcolm@redhat.com> writes:
> > On Wed, 2017-11-29 at 11:57 +0000, Richard Sandiford wrote:
> > 
> > [...]
> > 
> > I can't really comment on the representation ideas, but I'm always
> > happy to see new selftests...
> > 
> > *************** test_labels ()
> > > *** 13954,13959 ****
> > > --- 14179,14350 ----
> > >     ASSERT_FALSE (FORCED_LABEL (label_decl));
> > >   }
> > >   
> > > + /* Check that VECTOR_CST Y contains the elements in X.  */
> > > + 
> > > + static void
> > > + check_vector_cst (vec<tree> x, tree y)
> > > + {
> > > +   for (unsigned int i = 0; i < x.length (); ++i)
> > > +     ASSERT_EQ (wi::to_wide (x[i]), wi::to_wide (vector_cst_elt
> > > (y,
> > > i)));
> > > + }
> > 
> > ...a couple of nits/suggestions:
> > 
> > (a) How about renaming "x" to "expected"?  Maybe rename "y" to
> > "actual"?  (to better document the sense of the test).
> 
> Good idea.  I keep getting the ASSERT_* argument order for actual
> vs. expected confused, so having more explicit names would help.

FWIW, so do I.  The ordering comes from googletest's API, which is what
the earliest version of the selftest code used (before Bernd persuaded
me to stop over-engineering it :) ), and changing it seemed like too
much trouble for not much gain.

That said I note that googletest's API now uses just "val1" and "val2"
for binary assertion macros, and their docs now say:

"Historical note: Before February 2016 *_EQ had a convention of calling
it as ASSERT_EQ(expected, actual), so lots of existing code uses this
order. Now *_EQ treats both parameters in the same way."

This seems to have been:
https://github.com/google/googletest/commit/f364e188372e489230ef4e44e1aec6bcb08f3acf

https://github.com/google/googletest/pull/713

(which FWIW talks about "expected, actual" as "Yoda ordering")

I guess we could make a similar change, but it seems low priority. 
Better to at least support "actual" and "expected" as names in domain-
specific assertions, like your patch does for check_vector_cst.

> Done in the updated patch.
> 
> However, I needed the patch below to use those names, since "actual"
> and
> "expected" are also used internally by the macros.  I tried to
> protect
> other "user-level" names too.
> 
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-
> gnu.
> Bordering on obvious, but just in case: OK to install?

Looks good to me, though I'm not an official reviewer for the selftest
stuff.

[...]

Thanks
Dave

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-12-01 11:28       ` Richard Biener
@ 2017-12-06 15:12         ` Richard Sandiford
  2017-12-06 15:22           ` Use tree_vector_builder instead of build_vector Richard Sandiford
                             ` (6 more replies)
  0 siblings, 7 replies; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:12 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

Richard Biener <richard.guenther@gmail.com> writes:
> On Thu, Nov 30, 2017 at 2:18 PM, Richard Sandiford
> <richard.sandiford@linaro.org> wrote:
>> Richard Sandiford <richard.sandiford@linaro.org> writes:
>>> Richard Biener <richard.guenther@gmail.com> writes:
>>>> On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
>>>> <richard.sandiford@linaro.org> wrote:
>>>>> It was clear from the SVE reviews that people were unhappy with how
>>>>> "special" the variable-length case was.  One particular concern was
>>>>> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
>>>>> that would in turn lead to different representations of VEC_PERM_EXPRs
>>>>> with constant permute vectors, and difficulties in invoking
>>>>> vec_perm_const_ok.
>>>>>
>>>>> This is an RFC for a VECTOR_CST representation that treats each
>>>>> specific constant as one instance of an arbitrary-length sequence.
>>>>> The reprensentation then extends to variable-length vectors in a
>>>>> natural way.
>>>>>
>>>>> As discussed on IRC, if a vector contains X*N elements for some
>>>>> constant N and integer X>0, the main features we need are:
>>>>>
>>>>> 1) the ability to represent a sequence that duplicates N values
>>>>>
>>>>>    This is useful for SLP invariants.
>>>>>
>>>>> 2) the ability to represent a sequence that starts with N values and
>>>>>    is followed by zeros
>>>>>
>>>>>    This is useful for the initial value in a double or SLP reduction
>>>>>
>>>>> 3) the ability to represent N interleaved series
>>>>>
>>>>>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>>>>>
>>>>> For (2), zero isn't necessarily special, since vectors used in an AND
>>>>> reduction might need to fill with ones.  Also, we might need up to N
>>>>> different fill values with mixed SLP operations; it isn't necessarily
>>>>> safe to assume that a single fill value will always be enough.
>>>>>
>>>>> The same goes for (3): there's no reason in principle why the
>>>>> steps in an SLP induction should all be the same (although they
>>>>> do need to be at the moment).  E.g. once we support SLP on:
>>>>>
>>>>>   for (unsigned int i = 0; i < n; i += 2)
>>>>>     {
>>>>>       x[i] += 4 + i;
>>>>>       x[i + 1] += 11 + i * 3;
>>>>>     }
>>>>>
>>>>> we'll need {[4, 14], +, [2, 6]}.
>>>>>
>>>>> So the idea is to represent vectors as P interleaved patterns of the form:
>>>>>
>>>>>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>>>>>
>>>>> where the STEP is always zero (actually null) for non-integer vectors.
>>>>> This is effectively projecting a "foreground" value of P elements
>>>>> onto an arbitrary-length "background" sequenece, where the background
>>>>> sequence contains P parallel linear series.
>>>>>
>>>>> E.g. to pick an extreme and unlikely example,
>>>>>
>>>>>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>>>>>
>>>>> has 2 patterns:
>>>>>
>>>>>   BASE0 = 42, BASE1 = 2, STEP = 1
>>>>>   BASE0 = 99, BASE1 = 20, STEP = 10
>>>>>
>>>>> The more useful cases are degenerate versions of this general case.
>>>>>
>>>>> As far as memory consumption goes: the number of patterns needed for a
>>>>> fixed-length vector with 2*N elements is always at most N; in the worst
>>>>> case, we simply interleave the first N elements with the second N elements.
>>>>> The worst-case increase in footprint is therefore N trees for the steps.
>>>>> In practice the footprint is usually smaller than it was before, since
>>>>> most constants do have a pattern.
>>>>>
>>>>> The patch below implements this for trees.  I have patches to use the
>>>>> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
>>>>> tree one is probably easiest to read.
>>>>>
>>>>> The patch only adds the representation.  Follow-on patches make more
>>>>> use of it (and usually make things simpler; e.g. integer_zerop is no
>>>>> longer a looping operation).
>>>>>
>>>>> Does this look better?
>>>>
>>>> Yes, the overall design looks good.  I wonder why you chose to have
>>>> the number of patterns being a power of two?  I suppose this is
>>>> to have the same number of elements from all patterns in the final
>>>> vector (which is power-of-two sized)?
>>>
>>> Right.  The rtl and vec_perm_indices parts don't have this restriction,
>>> since some ports do define non-power-of-2 vectors for internal use.
>>> The problem is that, since VECTOR_CSTs are used by the FE, we need
>>> to support all valid vector lengths without blowing the 16-bit field.
>>> Using the same style of representation as TYPE_VECTOR_SUBPARTS seemed
>>> like the safest way of doing that.
>>>
>>>> I wonder if there exists a vector where say a three-pattern
>>>> interleaving would be smaller than a four-pattern one?
>>>
>>> Only in the non-power-of-2 case.
>>>
>>>> Given you add flags for various purposes would it make sense to
>>>> overload 'step' with a regular element to avoid the storage increase
>>>> in case step is unnecessary?  This makes it have three elements
>>>> which is of course awkward :/
>>>
>>> I wondered about keeping it as an array of trees and tacking the
>>> steps onto the end as an optional addition.  But the idea is that
>>> tree_vector_pattern becomes the preferred way of handling constant
>>> vectors, if it can be used, so it seemed neater to use in the tree
>>> node too.
>>
>> In the end it seemed better to encode the first NPATTERNS * N
>> elements of the vector, where:
>>
>> - N==3 if at least one pattern needs a step; otherwise
>> - N==2 if at least one pattern has different BASE0s and BASE1s; otherwise
>> - N==1 (i.e. the vector is a repeated sequence of NPATTERNS elements)
>>
>> So effectively all we're doing for the constant-length case is
>> reducing the number of elements that need to be stored and processed
>> (once code is converted to use the new routines).
>>
>> The patch below does this.  It also introduces a new class
>> vector_builder<T> for building vectors of Ts, with a derived
>> tree_vector_builder specifically for trees.  This allows the
>> compression code to be shared between representations and also
>> avoids hard-coding the auto_vec<> count everywhere.
>>
>> I've also fixed the selftests to get the actual and expected values
>> the right way round (thanks David for noticing that).
>>
>> What do you think?  Does this look better?
>
> Yes!  This is ok for trunk.

Thanks :-)  I've since tidied up the documentation and vector-builder.h
stuff and tested the patch properly with the rest of the SVE series
(which seems to work well with it).  The encoding is the same as before.

Sorry for the delay in getting the update out, but I wanted to be
certain that no further changes were needed.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu
against current trunk.  Still OK?

Richard


2017-12-06  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
	* doc/generic.texi (VECTOR_CST): Describe new representation of
	vector constants.
	* vector-builder.h: New file.
	* tree-vector-builder.h: Likewise.
	* tree-vector-builder.c: Likewise.
	* Makefile.in (OBJS): Add tree-vector-builder.o.
	* tree.def (VECTOR_CST): Update comment to refer to generic.texi.
	* tree-core.h (tree_base): Add a vector_cst field to the u union.
	(tree_vector): Change the number of elements to
	vector_cst_encoded_nelts.
	* tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
	(VECTOR_CST_ELTS): Delete.
	(VECTOR_CST_ELT): Redefine using vector_cst_elt.
	(VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
	(VECTOR_CST_NELTS_PER_PATTERN, VECTOR_CST_DUPLICATE_P): Likewise.
	(VECTOR_CST_STEPPED_P, VECTOR_CST_ENCODED_ELTS): Likewise.
	(VECTOR_CST_ENCODED_ELT): Likewise.
	(vector_cst_encoded_nelts): New function.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
	VECTOR_CST_NELTS_PER_PATTERN as arguments.
	(vector_cst_int_elt, vector_cst_elt): Declare.
	* tree.c: Include tree-vector-builder.h.
	(tree_code_size): Abort if passed VECTOR_CST.
	(tree_size): Update for new VECTOR_CST layout.
	(make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
	VECTOR_CST_NELTS_PER_PATTERN as arguments.
	(build_vector): Use tree_vector_builder.
	(vector_cst_int_elt, vector_cst_elt): New functions.
	(drop_tree_overflow): For VECTOR_CST, drop the TREE_OVERFLOW from the
	encoded elements and then create the vector in the canonical form.
	(check_vector_cst, check_vector_cst_duplicate, check_vector_cst_fill)
	(check_vector_cst_stepped, test_vector_cst_patterns): New functions.
	(tree_c_tests): Call test_vector_cst_patterns.
	* lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
	VECTOR_CST fields.
	(hash_tree): Likewise.
	* tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
	(streamer_write_tree_header): Likewise.
	* tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
	(streamer_alloc_tree): Likewise.  Update call to make_vector.
	* fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.

gcc/lto/
	* lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.

Index: gcc/doc/generic.texi
===================================================================
--- gcc/doc/generic.texi	2017-12-06 14:46:13.860611208 +0000
+++ gcc/doc/generic.texi	2017-12-06 14:46:14.128600028 +0000
@@ -1084,10 +1084,77 @@ These nodes are used to represent comple
 imaginary parts respectively.
 
 @item VECTOR_CST
-These nodes are used to represent vector constants, whose parts are
-constant nodes.  Each individual constant node is either an integer or a
-double constant node.  The first operand is a @code{TREE_LIST} of the
-constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
+These nodes are used to represent vector constants.  Each vector
+constant @var{v} is treated as a specific instance of an arbitrary-length
+sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
+interleaved patterns.  Each pattern has the form:
+
+@smallexample
+@{ @var{base0}, @var{base1}, @var{base1} + @var{step}, @var{base1} + @var{step} * 2, @dots{} @}
+@end smallexample
+
+The first three elements in each pattern are enough to determine the
+values of the other elements.  However, if all @var{step}s are zero,
+only the first two elements are needed.  If in addition each @var{base1}
+is equal to the corresponding @var{base0}, only the first element in
+each pattern is needed.  The number of encoded elements per pattern
+is given by @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v})}.
+
+For example, the constant:
+
+@smallexample
+@{ 0, 1, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18 @}
+@end smallexample
+
+is interpreted as an interleaving of the sequences:
+
+@smallexample
+@{ 0, 2, 3, 4, 5, 6, 7, 8 @}
+@{ 1, 6, 8, 10, 12, 14, 16, 18 @}
+@end smallexample
+
+where the sequences are represented by the following patterns:
+
+@smallexample
+@var{base0} == 0, @var{base1} == 2, @var{step} == 1
+@var{base0} == 1, @var{base1} == 6, @var{step} == 2
+@end smallexample
+
+In this case:
+
+@smallexample
+VECTOR_CST_NPATTERNS (@var{v}) == 2
+VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3
+@end smallexample
+
+The vector is therefore encoded using the first 6 elements
+(@samp{@{ 0, 1, 2, 6, 3, 8 @}}), with the remaining 10 elements
+being implicit extensions of them.
+
+Sometimes this scheme can create two possible encodings of the same
+vector.  For example @{ 0, 1 @} could be seen as two patterns with
+one element each or one pattern with two elements (@var{base0} and
+@var{base1}).  The canonical encoding is always the one with the
+fewest patterns or (if both encodings have the same number of
+petterns) the one with the fewest encoded elements.
+
+@samp{vector_cst_encoding_nelts (@var{v})} gives the total number of
+encoded elements in @var{v}, which is 6 in the example above.
+@code{VECTOR_CST_ENCODED_ELTS (@var{v})} gives a pointer to the elements
+encoded in @var{v} and @code{VECTOR_CST_ENCODED_ELT (@var{v}, @var{i})}
+accesses the value of encoded element @var{i}.
+
+@samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply contains
+repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})} values.  This is
+a shorthand for testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 1}.
+
+@samp{VECTOR_CST_STEPPED_P (@var{v})} is true if at least one
+pattern in @var{v} has a nonzero step.  This is a shorthand for
+testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3}.
+
+The utility function @code{vector_cst_elt} gives the value of an
+arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
+the same value as a @code{wide_int}.
 
 @item STRING_CST
 These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
Index: gcc/vector-builder.h
===================================================================
--- /dev/null	2017-12-05 14:21:55.753572108 +0000
+++ gcc/vector-builder.h	2017-12-06 14:46:14.133599820 +0000
@@ -0,0 +1,394 @@
+/* A class for building vector constant patterns.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_VECTOR_BUILDER_H
+#define GCC_VECTOR_BUILDER_H
+
+/* This class is a wrapper around auto_vec<T> for building vectors of T.
+   It aims to encode each vector as npatterns interleaved patterns,
+   where each pattern represents a sequence:
+
+     { BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, BASE1 + STEP*3, ... }
+
+   The first three elements in each pattern provide enough information
+   to derive the other elements.  If all patterns have a STEP of zero,
+   we only need to encode the first two elements in each pattern.
+   If BASE1 is also equal to BASE0 for all patterns, we only need to
+   encode the first element in each pattern.  The number of encoded
+   elements per pattern is given by nelts_per_pattern.
+
+   The class can be used in two ways:
+
+   1. It can be used to build a full image of the vector, which is then
+      canonicalized by finalize ().  In this case npatterns is initially
+      the number of elements in the vector and nelts_per_pattern is
+      initially 1.
+
+   2. It can be used to build a vector that already has a known encoding.
+      This is preferred since it is more efficient and copes with
+      variable-length vectors.  finalize () then canonicalizes the encoding
+      to a simpler form if possible.
+
+   The derived class Derived provides this functionality for specific Ts.
+   Derived needs to provide the following interface:
+
+      bool equal_p (T elt1, T elt2) const;
+
+	  Return true if elements ELT1 and ELT2 are equal.
+
+      bool allow_steps_p () const;
+
+	  Return true if a stepped representation is OK.  We don't allow
+	  linear series for anything other than integers, to avoid problems
+	  with rounding.
+
+      bool integral_p (T elt) const;
+
+	  Return true if element ELT can be interpreted as an integer.
+
+      StepType step (T elt1, T elt2) const;
+
+	  Return the value of element ELT2 minus the value of element ELT1,
+	  given integral_p (ELT1) && integral_p (ELT2).  There is no fixed
+	  choice of StepType.
+
+      bool can_elide_p (T elt) const;
+
+	  Return true if we can drop element ELT, even if the retained
+	  elements are different.  This is provided for TREE_OVERFLOW
+	  handling.
+
+      void note_representative (T *elt1_ptr, T elt2);
+
+	  Record that ELT2 is being elided, given that ELT1_PTR points to
+	  the last encoded element for the containing pattern.  This is
+	  again provided for TREE_OVERFLOW handling.  */
+
+template<typename T, typename Derived>
+class vector_builder : public auto_vec<T, 32>
+{
+public:
+  vector_builder ();
+
+  unsigned int full_nelts () const { return m_full_nelts; }
+  unsigned int npatterns () const { return m_npatterns; }
+  unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
+  unsigned int encoded_nelts () const;
+  bool encoded_full_vector_p () const;
+
+  void finalize ();
+
+protected:
+  void new_vector (unsigned int, unsigned int, unsigned int);
+  void reshape (unsigned int, unsigned int);
+  bool repeating_sequence_p (unsigned int, unsigned int, unsigned int);
+  bool stepped_sequence_p (unsigned int, unsigned int, unsigned int);
+  bool try_npatterns (unsigned int);
+
+private:
+  vector_builder (const vector_builder &);
+  vector_builder &operator= (const vector_builder &);
+  Derived *derived () { return static_cast<Derived *> (this); }
+  const Derived *derived () const;
+
+  unsigned int m_full_nelts;
+  unsigned int m_npatterns;
+  unsigned int m_nelts_per_pattern;
+};
+
+template<typename T, typename Derived>
+inline const Derived *
+vector_builder<T, Derived>::derived () const
+{
+  return static_cast<const Derived *> (this);
+}
+
+template<typename T, typename Derived>
+inline
+vector_builder<T, Derived>::vector_builder ()
+  : m_full_nelts (0),
+    m_npatterns (0),
+    m_nelts_per_pattern (0)
+{}
+
+/* Return the number of elements that are explicitly encoded.  The vec
+   starts with these explicitly-encoded elements and may contain additional
+   elided elements.  */
+
+template<typename T, typename Derived>
+inline unsigned int
+vector_builder<T, Derived>::encoded_nelts () const
+{
+  return m_npatterns * m_nelts_per_pattern;
+}
+
+/* Return true if every element of the vector is explicitly encoded.  */
+
+template<typename T, typename Derived>
+inline bool
+vector_builder<T, Derived>::encoded_full_vector_p () const
+{
+  return m_npatterns * m_nelts_per_pattern == m_full_nelts;
+}
+
+/* Start building a vector that has FULL_NELTS elements.  Initially
+   encode it using NPATTERNS patterns with NELTS_PER_PATTERN each.  */
+
+template<typename T, typename Derived>
+void
+vector_builder<T, Derived>::new_vector (unsigned int full_nelts,
+					unsigned int npatterns,
+					unsigned int nelts_per_pattern)
+{
+  m_full_nelts = full_nelts;
+  m_npatterns = npatterns;
+  m_nelts_per_pattern = nelts_per_pattern;
+  this->reserve (encoded_nelts ());
+  this->truncate (0);
+}
+
+/* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
+   but without changing the underlying vector.  */
+
+template<typename T, typename Derived>
+void
+vector_builder<T, Derived>::reshape (unsigned int npatterns,
+				     unsigned int nelts_per_pattern)
+{
+  unsigned int old_encoded_nelts = encoded_nelts ();
+  unsigned int new_encoded_nelts = npatterns * nelts_per_pattern;
+  gcc_checking_assert (new_encoded_nelts <= old_encoded_nelts);
+  unsigned int next = new_encoded_nelts - npatterns;
+  for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
+    {
+      derived ()->note_representative (&(*this)[next], (*this)[i]);
+      next += 1;
+      if (next == new_encoded_nelts)
+	next -= npatterns;
+    }
+  m_npatterns = npatterns;
+  m_nelts_per_pattern = nelts_per_pattern;
+}
+
+/* Return true if elements [START, END) contain a repeating sequence of
+   STEP elements.  */
+
+template<typename T, typename Derived>
+bool
+vector_builder<T, Derived>::repeating_sequence_p (unsigned int start,
+						  unsigned int end,
+						  unsigned int step)
+{
+  for (unsigned int i = start; i < end - step; ++i)
+    if (!derived ()->equal_p ((*this)[i], (*this)[i + step]))
+      return false;
+  return true;
+}
+
+/* Return true if elements [START, END) contain STEP interleaved linear
+   series.  */
+
+template<typename T, typename Derived>
+bool
+vector_builder<T, Derived>::stepped_sequence_p (unsigned int start,
+						unsigned int end,
+						unsigned int step)
+{
+  if (!derived ()->allow_steps_p ())
+    return false;
+
+  for (unsigned int i = start + step * 2; i < end; ++i)
+    {
+      T elt1 = (*this)[i - step * 2];
+      T elt2 = (*this)[i - step];
+      T elt3 = (*this)[i];
+
+      if (!derived ()->integral_p (elt1)
+	  || !derived ()->integral_p (elt2)
+	  || !derived ()->integral_p (elt3))
+	return false;
+
+      if (derived ()->step (elt1, elt2) != derived ()->step (elt2, elt3))
+	return false;
+
+      if (!derived ()->can_elide_p (elt3))
+	return false;
+    }
+  return true;
+}
+
+/* Try to change the number of encoded patterns to NPATTERNS, returning
+   true on success.  */
+
+template<typename T, typename Derived>
+bool
+vector_builder<T, Derived>::try_npatterns (unsigned int npatterns)
+{
+  if (m_nelts_per_pattern == 1)
+    {
+      /* See whether NPATTERNS is valid with the current 1-element-per-pattern
+	 encoding.  */
+      if (repeating_sequence_p (0, encoded_nelts (), npatterns))
+	{
+	  reshape (npatterns, 1);
+	  return true;
+	}
+
+      /* We can only increase the number of elements per pattern if all
+	 elements are still encoded explicitly.  */
+      if (!encoded_full_vector_p ())
+	return false;
+    }
+
+  if (m_nelts_per_pattern <= 2)
+    {
+      /* See whether NPATTERNS is valid with a 2-element-per-pattern
+	 encoding.  */
+      if (repeating_sequence_p (npatterns, encoded_nelts (), npatterns))
+	{
+	  reshape (npatterns, 2);
+	  return true;
+	}
+
+      /* We can only increase the number of elements per pattern if all
+	 elements are still encoded explicitly.  */
+      if (!encoded_full_vector_p ())
+	return false;
+    }
+
+  if (m_nelts_per_pattern <= 3)
+    {
+      /* See whether we have NPATTERNS interleaved linear series,
+	 giving a 3-element-per-pattern encoding.  */
+      if (stepped_sequence_p (npatterns, encoded_nelts (), npatterns))
+	{
+	  reshape (npatterns, 3);
+	  return true;
+	}
+      return false;
+    }
+
+  gcc_unreachable ();
+}
+
+/* Replace the current encoding with the canonical form.  */
+
+template<typename T, typename Derived>
+void
+vector_builder<T, Derived>::finalize ()
+{
+  /* The encoding requires the same number of elements to come from each
+     pattern.  */
+  gcc_assert (m_full_nelts % m_npatterns == 0);
+
+  /* Allow the caller to build more elements than necessary.  For example,
+     it's often convenient to build a stepped vector from the natural
+     encoding of three elements even if the vector itself only has two.  */
+  if (m_full_nelts <= encoded_nelts ())
+    {
+      m_npatterns = m_full_nelts;
+      m_nelts_per_pattern = 1;
+    }
+
+  /* Try to whittle down the number of elements per pattern.  That is:
+
+     1. If we have stepped patterns whose steps are all 0, reduce the
+        number of elements per pattern from 3 to 2.
+
+     2. If we have background fill values that are the same as the
+        foreground values, reduce the number of elements per pattern
+        from 2 to 1.  */
+  while (m_nelts_per_pattern > 1
+	 && repeating_sequence_p (encoded_nelts () - m_npatterns * 2,
+				  encoded_nelts (), m_npatterns))
+    /* The last two sequences of M_NPATTERNS elements are equal,
+       so remove the last one.  */
+    reshape (m_npatterns, m_nelts_per_pattern - 1);
+
+  if (pow2p_hwi (m_npatterns))
+    {
+      /* Try to halve the number of patterns while doing so gives a
+	 valid pattern.  This approach is linear in the number of
+	 elements, whereas searcing from 1 up would be O(n*log(n)).
+
+	 Each halving step tries to keep the number of elements per pattern
+	 the same.  If that isn't possible, and if all elements are still
+	 explicitly encoded, the halving step can instead increase the number
+	 of elements per pattern.
+
+	 E.g. for:
+
+	     { 0, 2, 3, 4, 5, 6, 7, 8 }  npatterns == 8  full_nelts == 8
+
+	 we first realize that the second half of the sequence is not
+	 equal to the first, so we cannot maintain 1 element per pattern
+	 for npatterns == 4.  Instead we halve the number of patterns
+	 and double the number of elements per pattern, treating this
+	 as a "foreground" { 0, 2, 3, 4 } against a "background" of
+	 { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
+
+	     { 0, 2, 3, 4 | 5, 6, 7, 8 }  npatterns == 4
+
+	 Next we realize that this is *not* a foreround of { 0, 2 }
+	 against a background of { 3, 4 | 3, 4 ... }, so the only
+	 remaining option for reducing the number of patterns is
+	 to use a foreground of { 0, 2 } against a stepped background
+	 of { 1, 2 | 3, 4 | 5, 6 ... }.  This is valid because we still
+	 haven't elided any elements:
+
+	     { 0, 2 | 3, 4 | 5, 6 }  npatterns == 2
+
+	 This in turn can be reduced to a foreground of { 0 } against a
+	 stepped background of { 1 | 2 | 3 ... }:
+
+	     { 0 | 2 | 3 }  npatterns == 1
+
+	 This last step would not have been possible for:
+
+	     { 0, 0 | 3, 4 | 5, 6 }  npatterns == 2.  */
+      while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
+	continue;
+
+      /* Builders of arbitrary fixed-length vectors can use:
+
+	     new_vector (x, x, 1)
+
+	 so that every element is specified explicitly.  Handle cases
+	 that are actually wrapping series, like { 0, 1, 2, 3, 0, 1, 2, 3 }
+	 would be for 2-bit elements.  We'll have treated them as
+	 duplicates in the loop above.  */
+      if (m_nelts_per_pattern == 1
+	  && this->length () >= m_full_nelts
+	  && (m_npatterns & 3) == 0
+	  && stepped_sequence_p (m_npatterns / 4, m_full_nelts,
+				 m_npatterns / 4))
+	{
+	  reshape (m_npatterns / 4, 3);
+	  while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
+	    continue;
+	}
+    }
+  else
+    /* For the non-power-of-2 case, do a simple search up from 1.  */
+    for (unsigned int i = 1; i <= m_npatterns / 2; ++i)
+      if (m_npatterns % i == 0 && try_npatterns (i))
+	break;
+}
+
+#endif
Index: gcc/tree-vector-builder.h
===================================================================
--- /dev/null	2017-12-05 14:21:55.753572108 +0000
+++ gcc/tree-vector-builder.h	2017-12-06 14:46:14.131599903 +0000
@@ -0,0 +1,135 @@
+/* A class for building vector tree constants.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_TREE_VECTOR_BUILDER_H
+#define GCC_TREE_VECTOR_BUILDER_H
+
+#include "vector-builder.h"
+
+/* This class is used to build VECTOR_CSTs from a sequence of elements.
+   See vector_builder for more details.  */
+class tree_vector_builder : public vector_builder<tree, tree_vector_builder>
+{
+  typedef vector_builder<tree, tree_vector_builder> parent;
+  friend class vector_builder<tree, tree_vector_builder>;
+
+public:
+  tree_vector_builder () : m_type (0) {}
+  tree_vector_builder (tree, unsigned int, unsigned int);
+  tree build ();
+
+  tree type () const { return m_type; }
+
+  void new_vector (tree, unsigned int, unsigned int);
+  bool new_unary_operation (tree, tree, bool);
+
+private:
+  bool equal_p (const_tree, const_tree) const;
+  bool allow_steps_p () const;
+  bool integral_p (const_tree) const;
+  wide_int step (const_tree, const_tree) const;
+  bool can_elide_p (const_tree) const;
+  void note_representative (tree *, tree);
+
+  tree m_type;
+};
+
+/* Create a new builder for a vector of type TYPE.  Initially encode the
+   value as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements
+   each.  */
+
+inline
+tree_vector_builder::tree_vector_builder (tree type, unsigned int npatterns,
+					  unsigned int nelts_per_pattern)
+{
+  new_vector (type, npatterns, nelts_per_pattern);
+}
+
+/* Start building a new vector of type TYPE.  Initially encode the value
+   as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.  */
+
+inline void
+tree_vector_builder::new_vector (tree type, unsigned int npatterns,
+				 unsigned int nelts_per_pattern)
+{
+  m_type = type;
+  parent::new_vector (TYPE_VECTOR_SUBPARTS (type), npatterns,
+		      nelts_per_pattern);
+}
+
+/* Return true if elements I1 and I2 are equal.  */
+
+inline bool
+tree_vector_builder::equal_p (const_tree elt1, const_tree elt2) const
+{
+  return operand_equal_p (elt1, elt2, 0);
+}
+
+/* Return true if a stepped representation is OK.  We don't allow
+   linear series for anything other than integers, to avoid problems
+   with rounding.  */
+
+inline bool
+tree_vector_builder::allow_steps_p () const
+{
+  return INTEGRAL_TYPE_P (TREE_TYPE (m_type));
+}
+
+/* Return true if ELT can be interpreted as an integer.  */
+
+inline bool
+tree_vector_builder::integral_p (const_tree elt) const
+{
+  return TREE_CODE (elt) == INTEGER_CST;
+}
+
+/* Return the value of element ELT2 minus the value of element ELT1.
+   Both elements are known to be INTEGER_CSTs.  */
+
+inline wide_int
+tree_vector_builder::step (const_tree elt1, const_tree elt2) const
+{
+  return wi::to_wide (elt2) - wi::to_wide (elt1);
+}
+
+/* Return true if we can drop element ELT, even if the retained elements
+   are different.  Return false if this would mean losing overflow
+   information.  */
+
+inline bool
+tree_vector_builder::can_elide_p (const_tree elt) const
+{
+  return !CONSTANT_CLASS_P (elt) || !TREE_OVERFLOW (elt);
+}
+
+/* Record that ELT2 is being elided, given that ELT1_PTR points to the last
+   encoded element for the containing pattern.  */
+
+inline void
+tree_vector_builder::note_representative (tree *elt1_ptr, tree elt2)
+{
+  if (CONSTANT_CLASS_P (elt2) && TREE_OVERFLOW (elt2))
+    {
+      gcc_assert (operand_equal_p (*elt1_ptr, elt2, 0));
+      if (!TREE_OVERFLOW (elt2))
+	*elt1_ptr = elt2;
+    }
+}
+
+#endif
Index: gcc/tree-vector-builder.c
===================================================================
--- /dev/null	2017-12-05 14:21:55.753572108 +0000
+++ gcc/tree-vector-builder.c	2017-12-06 14:46:14.131599903 +0000
@@ -0,0 +1,64 @@
+/* A class for building vector tree constants.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "fold-const.h"
+#include "tree-vector-builder.h"
+
+/* Try to start building a new vector of type TYPE that holds the result of
+   a unary operation on VECTOR_CST T.  ALLOW_STEPPED_P is true if the
+   operation can handle stepped encodings directly, without having to
+   expand the full sequence.
+
+   Return true if the operation is possible, which it always is when
+   ALLOW_STEPPED_P is true.  Leave the builder unchanged otherwise.  */
+
+bool
+tree_vector_builder::new_unary_operation (tree type, tree t,
+					  bool allow_stepped_p)
+{
+  unsigned int full_nelts = TYPE_VECTOR_SUBPARTS (type);
+  gcc_assert (full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t)));
+  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+  unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (t);
+  if (!allow_stepped_p && nelts_per_pattern > 2)
+    {
+      npatterns = full_nelts;
+      nelts_per_pattern = 1;
+    }
+  new_vector (type, npatterns, nelts_per_pattern);
+  return true;
+}
+
+/* Return a VECTOR_CST for the current constant.  */
+
+tree
+tree_vector_builder::build ()
+{
+  finalize ();
+  gcc_assert (pow2p_hwi (npatterns ()));
+  tree v = make_vector (exact_log2 (npatterns ()), nelts_per_pattern ());
+  TREE_TYPE (v) = m_type;
+  memcpy (VECTOR_CST_ENCODED_ELTS (v), address (),
+	  encoded_nelts () * sizeof (tree));
+  return v;
+}
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	2017-12-06 14:46:13.860611208 +0000
+++ gcc/Makefile.in	2017-12-06 14:46:14.128600028 +0000
@@ -1572,6 +1572,7 @@ OBJS = \
 	tree-vect-loop-manip.o \
 	tree-vect-slp.o \
 	tree-vectorizer.o \
+	tree-vector-builder.o \
 	tree-vrp.o \
 	tree.o \
 	typed-splay-tree.o \
Index: gcc/tree.def
===================================================================
--- gcc/tree.def	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree.def	2017-12-06 14:46:14.132599861 +0000
@@ -301,7 +301,7 @@ DEFTREECODE (FIXED_CST, "fixed_cst", tcc
    whose contents are other constant nodes.  */
 DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
 
-/* Contents are in VECTOR_CST_ELTS field.  */
+/* See generic.texi for details.  */
 DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
 
 /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
Index: gcc/tree-core.h
===================================================================
--- gcc/tree-core.h	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree-core.h	2017-12-06 14:46:14.130599945 +0000
@@ -972,8 +972,17 @@ struct GTY(()) tree_base {
     /* VEC length.  This field is only used with TREE_VEC.  */
     int length;
 
-    /* Number of elements.  This field is only used with VECTOR_CST.  */
-    unsigned int nelts;
+    /* This field is only used with VECTOR_CST.  */
+    struct {
+      /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
+      unsigned int log2_npatterns : 8;
+
+      /* The value of VECTOR_CST_NELTS_PER_PATTERN.  */
+      unsigned int nelts_per_pattern : 8;
+
+      /* For future expansion.  */
+      unsigned int unused : 16;
+    } vector_cst;
 
     /* SSA version number.  This field is only used with SSA_NAME.  */
     unsigned int version;
@@ -1325,7 +1334,7 @@ struct GTY(()) tree_complex {
 
 struct GTY(()) tree_vector {
   struct tree_typed typed;
-  tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
+  tree GTY ((length ("vector_cst_encoded_nelts ((tree) &%h)"))) elts[1];
 };
 
 struct GTY(()) tree_identifier {
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree.h	2017-12-06 14:46:14.133599820 +0000
@@ -1008,10 +1008,24 @@ #define TREE_STRING_POINTER(NODE) \
 #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
 #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
 
-/* In a VECTOR_CST node.  */
-#define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
-#define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
-#define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
+/* In a VECTOR_CST node.  See generic.texi for details.  */
+#define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
+#define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
+
+#define VECTOR_CST_LOG2_NPATTERNS(NODE) \
+  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
+#define VECTOR_CST_NPATTERNS(NODE) \
+  (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
+#define VECTOR_CST_NELTS_PER_PATTERN(NODE) \
+  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.nelts_per_pattern)
+#define VECTOR_CST_DUPLICATE_P(NODE) \
+  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 1)
+#define VECTOR_CST_STEPPED_P(NODE) \
+  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 3)
+#define VECTOR_CST_ENCODED_ELTS(NODE) \
+  (VECTOR_CST_CHECK (NODE)->vector.elts)
+#define VECTOR_CST_ENCODED_ELT(NODE, ELT) \
+  (VECTOR_CST_CHECK (NODE)->vector.elts[ELT])
 
 /* Define fields and accessors for some special-purpose tree nodes.  */
 
@@ -3882,6 +3896,14 @@ #define error_operand_p(NODE)					\
   ((NODE) == error_mark_node					\
    || ((NODE) && TREE_TYPE ((NODE)) == error_mark_node))
 
+/* Return the number of elements encoded directly in a VECTOR_CST.  */
+
+inline unsigned int
+vector_cst_encoded_nelts (const_tree t)
+{
+  return VECTOR_CST_NPATTERNS (t) * VECTOR_CST_NELTS_PER_PATTERN (t);
+}
+
 extern tree decl_assembler_name (tree);
 extern void overwrite_decl_assembler_name (tree decl, tree name);
 extern tree decl_comdat_group (const_tree);
@@ -4021,7 +4043,7 @@ extern tree force_fit_type (tree, const
 extern tree build_int_cst (tree, HOST_WIDE_INT);
 extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
 extern tree build_int_cst_type (tree, HOST_WIDE_INT);
-extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
+extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
 extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
 extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
 extern tree build_vector_from_val (tree, tree);
@@ -4268,6 +4290,9 @@ extern tree first_field (const_tree);
 
 extern bool initializer_zerop (const_tree);
 
+extern wide_int vector_cst_int_elt (const_tree, unsigned int);
+extern tree vector_cst_elt (const_tree, unsigned int);
+
 /* Given a vector VEC, return its first element if all elements are
    the same.  Otherwise return NULL_TREE.  */
 
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree.c	2017-12-06 14:46:14.132599861 +0000
@@ -66,6 +66,7 @@ Software Foundation; either version 3, o
 #include "attribs.h"
 #include "rtl.h"
 #include "regs.h"
+#include "tree-vector-builder.h"
 
 /* Tree code classes.  */
 
@@ -837,7 +838,7 @@ tree_code_size (enum tree_code code)
 	case REAL_CST:		return sizeof (tree_real_cst);
 	case FIXED_CST:		return sizeof (tree_fixed_cst);
 	case COMPLEX_CST:	return sizeof (tree_complex);
-	case VECTOR_CST:	return sizeof (tree_vector);
+	case VECTOR_CST:	gcc_unreachable ();
 	case STRING_CST:	gcc_unreachable ();
 	default:
 	  gcc_checking_assert (code >= NUM_TREE_CODES);
@@ -897,7 +898,7 @@ tree_size (const_tree node)
 
     case VECTOR_CST:
       return (sizeof (struct tree_vector)
-	      + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
+	      + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
 
     case STRING_CST:
       return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
@@ -1708,13 +1709,19 @@ cst_and_fits_in_hwi (const_tree x)
 	  && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
 }
 
-/* Build a newly constructed VECTOR_CST node of length LEN.  */
+/* Build a newly constructed VECTOR_CST with the given values of
+   (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN.  */
 
 tree
-make_vector (unsigned len MEM_STAT_DECL)
+make_vector (unsigned log2_npatterns,
+	     unsigned int nelts_per_pattern MEM_STAT_DECL)
 {
+  gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
   tree t;
-  unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
+  unsigned npatterns = 1 << log2_npatterns;
+  unsigned encoded_nelts = npatterns * nelts_per_pattern;
+  unsigned length = (sizeof (struct tree_vector)
+		     + (encoded_nelts - 1) * sizeof (tree));
 
   record_node_allocation_statistics (VECTOR_CST, length);
 
@@ -1722,7 +1729,8 @@ make_vector (unsigned len MEM_STAT_DECL)
 
   TREE_SET_CODE (t, VECTOR_CST);
   TREE_CONSTANT (t) = 1;
-  VECTOR_CST_NELTS (t) = len;
+  VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
+  VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
 
   return t;
 }
@@ -1733,29 +1741,10 @@ make_vector (unsigned len MEM_STAT_DECL)
 tree
 build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
 {
-  unsigned int nelts = vals.length ();
-  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
-  int over = 0;
-  unsigned cnt = 0;
-  tree v = make_vector (nelts);
-  TREE_TYPE (v) = type;
-
-  /* Iterate through elements and check for overflow.  */
-  for (cnt = 0; cnt < nelts; ++cnt)
-    {
-      tree value = vals[cnt];
-
-      VECTOR_CST_ELT (v, cnt) = value;
-
-      /* Don't crash if we get an address constant.  */
-      if (!CONSTANT_CLASS_P (value))
-	continue;
-
-      over |= TREE_OVERFLOW (value);
-    }
-
-  TREE_OVERFLOW (v) = over;
-  return v;
+  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
+  tree_vector_builder builder (type, vals.length (), 1);
+  builder.splice (vals);
+  return builder.build ();
 }
 
 /* Return a new VECTOR_CST node whose type is TYPE and whose values
@@ -10370,6 +10359,59 @@ build_opaque_vector_type (tree innertype
   return cand;
 }
 
+/* Return the value of element I of VECTOR_CST T as a wide_int.  */
+
+wide_int
+vector_cst_int_elt (const_tree t, unsigned int i)
+{
+  /* First handle elements that are directly encoded.  */
+  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
+  if (i < encoded_nelts)
+    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
+
+  /* Identify the pattern that contains element I and work out the index of
+     the last encoded element for that pattern.  */
+  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+  unsigned int pattern = i % npatterns;
+  unsigned int count = i / npatterns;
+  unsigned int final_i = encoded_nelts - npatterns + pattern;
+
+  /* If there are no steps, the final encoded value is the right one.  */
+  if (!VECTOR_CST_STEPPED_P (t))
+    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
+
+  /* Otherwise work out the value from the last two encoded elements.  */
+  tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
+  tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
+  wide_int diff = wi::to_wide (v2) - wi::to_wide (v1);
+  return wi::to_wide (v2) + (count - 2) * diff;
+}
+
+/* Return the value of element I of VECTOR_CST T.  */
+
+tree
+vector_cst_elt (const_tree t, unsigned int i)
+{
+  /* First handle elements that are directly encoded.  */
+  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
+  if (i < encoded_nelts)
+    return VECTOR_CST_ENCODED_ELT (t, i);
+
+  /* If there are no steps, the final encoded value is the right one.  */
+  if (!VECTOR_CST_STEPPED_P (t))
+    {
+      /* Identify the pattern that contains element I and work out the index of
+	 the last encoded element for that pattern.  */
+      unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
+      unsigned int pattern = i % npatterns;
+      unsigned int final_i = encoded_nelts - npatterns + pattern;
+      return VECTOR_CST_ENCODED_ELT (t, final_i);
+    }
+
+  /* Otherwise work out the value from the last two encoded elements.  */
+  return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
+			   vector_cst_int_elt (t, i));
+}
 
 /* Given an initializer INIT, return TRUE if INIT is zero or some
    aggregate of zeros.  Otherwise return FALSE.  */
@@ -12451,6 +12493,23 @@ drop_tree_overflow (tree t)
   if (TREE_CODE (t) == INTEGER_CST)
     return wide_int_to_tree (TREE_TYPE (t), wi::to_wide (t));
 
+  /* For VECTOR_CST, remove the overflow bits from the encoded elements
+     and canonicalize the result.  */
+  if (TREE_CODE (t) == VECTOR_CST)
+    {
+      tree_vector_builder builder;
+      builder.new_unary_operation (TREE_TYPE (t), t, true);
+      unsigned int count = builder.encoded_nelts ();
+      for (unsigned int i = 0; i < count; ++i)
+	{
+	  tree elt = VECTOR_CST_ELT (t, i);
+	  if (TREE_OVERFLOW (elt))
+	    elt = drop_tree_overflow (elt);
+	  builder.quick_push (elt);
+	}
+      return builder.build ();
+    }
+
   /* Otherwise, as all tcc_constants are possibly shared, copy the node
      and drop the flag.  */
   t = copy_node (t);
@@ -12465,15 +12524,7 @@ drop_tree_overflow (tree t)
       if (TREE_OVERFLOW (TREE_IMAGPART (t)))
 	TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
     }
-  if (TREE_CODE (t) == VECTOR_CST)
-    {
-      for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
-	{
-	  tree& elt = VECTOR_CST_ELT (t, i);
-	  if (TREE_OVERFLOW (elt))
-	    elt = drop_tree_overflow (elt);
-	}
-    }
+
   return t;
 }
 
@@ -14016,6 +14067,139 @@ test_labels ()
   ASSERT_FALSE (FORCED_LABEL (label_decl));
 }
 
+/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
+
+static void
+check_vector_cst (vec<tree> expected, tree actual)
+{
+  ASSERT_EQ (expected.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
+  for (unsigned int i = 0; i < expected.length (); ++i)
+    ASSERT_EQ (wi::to_wide (expected[i]),
+	       wi::to_wide (vector_cst_elt (actual, i)));
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
+   and that its elements match EXPECTED.  */
+
+static void
+check_vector_cst_duplicate (vec<tree> expected, tree actual,
+			    unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
+   and NPATTERNS background elements, and that its elements match
+   EXPECTED.  */
+
+static void
+check_vector_cst_fill (vec<tree> expected, tree actual,
+		       unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
+   and that its elements match EXPECTED.  */
+
+static void
+check_vector_cst_stepped (vec<tree> expected, tree actual,
+			  unsigned int npatterns)
+{
+  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
+  ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
+  ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
+  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
+  ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
+  check_vector_cst (expected, actual);
+}
+
+/* Test the creation of VECTOR_CSTs.  */
+
+static void
+test_vector_cst_patterns ()
+{
+  auto_vec<tree, 8> elements (8);
+  elements.quick_grow (8);
+  tree element_type = build_nonstandard_integer_type (16, true);
+  tree vector_type = build_vector_type (element_type, 8);
+
+  /* Test a simple linear series with a base of 0 and a step of 1:
+     { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
+  for (unsigned int i = 0; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, i);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try the same with the first element replaced by 100:
+     { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
+  elements[0] = build_int_cst (element_type, 100);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try a series that wraps around.
+     { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try a downward series:
+     { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = build_int_cst (element_type, 80 - i);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
+
+  /* Try two interleaved series with different bases and steps:
+     { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
+  elements[1] = build_int_cst (element_type, 53);
+  for (unsigned int i = 2; i < 8; i += 2)
+    {
+      elements[i] = build_int_cst (element_type, 70 - i * 2);
+      elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
+    }
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
+
+  /* Try a duplicated value:
+     { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
+  for (unsigned int i = 1; i < 8; ++i)
+    elements[i] = elements[0];
+  check_vector_cst_duplicate (elements,
+			      build_vector (vector_type, elements), 1);
+
+  /* Try an interleaved duplicated value:
+     { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
+  elements[1] = build_int_cst (element_type, 55);
+  for (unsigned int i = 2; i < 8; ++i)
+    elements[i] = elements[i - 2];
+  check_vector_cst_duplicate (elements,
+			      build_vector (vector_type, elements), 2);
+
+  /* Try a duplicated value with 2 exceptions
+     { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
+  elements[0] = build_int_cst (element_type, 41);
+  elements[1] = build_int_cst (element_type, 97);
+  check_vector_cst_fill (elements, build_vector (vector_type, elements), 2);
+
+  /* Try with and without a step
+     { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
+  for (unsigned int i = 3; i < 8; i += 2)
+    elements[i] = build_int_cst (element_type, i * 7);
+  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
+
+  /* Try a fully-general constant:
+     { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
+  elements[5] = build_int_cst (element_type, 9990);
+  check_vector_cst_fill (elements, build_vector (vector_type, elements), 4);
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -14024,6 +14208,7 @@ tree_c_tests ()
   test_integer_constants ();
   test_identifiers ();
   test_labels ();
+  test_vector_cst_patterns ();
 }
 
 } // namespace selftest
Index: gcc/lto-streamer-out.c
===================================================================
--- gcc/lto-streamer-out.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/lto-streamer-out.c	2017-12-06 14:46:14.130599945 +0000
@@ -747,8 +747,9 @@ #define DFS_follow_tree_edge(DEST) \
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
+      unsigned int count = vector_cst_encoded_nelts (expr);
+      for (unsigned int i = 0; i < count; ++i)
+	DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
@@ -1195,8 +1196,11 @@ #define visit(SIBLING) \
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
-      visit (VECTOR_CST_ELT (t, i));
+    {
+      unsigned int count = vector_cst_encoded_nelts (t);
+      for (unsigned int i = 0; i < count; ++i)
+	visit (VECTOR_CST_ENCODED_ELT (t, i));
+    }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
     {
Index: gcc/tree-streamer-out.c
===================================================================
--- gcc/tree-streamer-out.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree-streamer-out.c	2017-12-06 14:46:14.131599903 +0000
@@ -533,11 +533,11 @@ write_ts_common_tree_pointers (struct ou
 static void
 write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
 {
-  unsigned i;
   /* Note that the number of elements for EXPR has already been emitted
      in EXPR's header (see streamer_write_tree_header).  */
-  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-    stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
+  unsigned int count = vector_cst_encoded_nelts (expr);
+  for (unsigned int i = 0; i < count; ++i)
+    stream_write_tree (ob, VECTOR_CST_ENCODED_ELT (expr, i), ref_p);
 }
 
 
@@ -960,7 +960,12 @@ streamer_write_tree_header (struct outpu
   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
     write_identifier (ob, ob->main_stream, expr);
   else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
-    streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
+    {
+      bitpack_d bp = bitpack_create (ob->main_stream);
+      bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 8);
+      bp_pack_value (&bp, VECTOR_CST_NELTS_PER_PATTERN (expr), 8);
+      streamer_write_bitpack (&bp);
+    }
   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
     streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
Index: gcc/tree-streamer-in.c
===================================================================
--- gcc/tree-streamer-in.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/tree-streamer-in.c	2017-12-06 14:46:14.130599945 +0000
@@ -592,8 +592,10 @@ streamer_alloc_tree (struct lto_input_bl
     }
   else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      HOST_WIDE_INT len = streamer_read_hwi (ib);
-      result = make_vector (len);
+      bitpack_d bp = streamer_read_bitpack (ib);
+      unsigned int log2_npatterns = bp_unpack_value (&bp, 8);
+      unsigned int nelts_per_pattern = bp_unpack_value (&bp, 8);
+      result = make_vector (log2_npatterns, nelts_per_pattern);
     }
   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
     {
@@ -650,9 +652,9 @@ lto_input_ts_common_tree_pointers (struc
 lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
 				   struct data_in *data_in, tree expr)
 {
-  unsigned i;
-  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-    VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
+  unsigned int count = vector_cst_encoded_nelts (expr);
+  for (unsigned int i = 0; i < count; ++i)
+    VECTOR_CST_ENCODED_ELT (expr, i) = stream_read_tree (ib, data_in);
 }
 
 
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/fold-const.c	2017-12-06 14:46:14.129599986 +0000
@@ -11610,9 +11610,8 @@ fold_ternary_loc (location_t loc, enum t
 		  unsigned int nelts = VECTOR_CST_NELTS (arg0);
 		  auto_vec<tree, 32> elts (nelts);
 		  elts.quick_grow (nelts);
-		  memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
-			  sizeof (tree) * nelts);
-		  elts[k] = arg1;
+		  for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
+		    elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
 		  return build_vector (type, elts);
 		}
 	    }
Index: gcc/lto/lto.c
===================================================================
--- gcc/lto/lto.c	2017-12-06 14:46:13.860611208 +0000
+++ gcc/lto/lto.c	2017-12-06 14:46:14.130599945 +0000
@@ -1065,6 +1065,12 @@ #define compare_values(X) \
 			TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
       return false;
 
+  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
+    {
+      compare_values (VECTOR_CST_LOG2_NPATTERNS);
+      compare_values (VECTOR_CST_NELTS_PER_PATTERN);
+    }
+
   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
     {
       compare_values (DECL_MODE);
@@ -1281,11 +1287,12 @@ #define compare_tree_edges(E1, E2) \
 
   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
     {
-      unsigned i;
       /* Note that the number of elements for EXPR has already been emitted
 	 in EXPR's header (see streamer_write_tree_header).  */
-      for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
-	compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
+      unsigned int count = vector_cst_encoded_nelts (t1);
+      for (unsigned int i = 0; i < count; ++i)
+	compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
+			    VECTOR_CST_ENCODED_ELT (t2, i));
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Use tree_vector_builder instead of build_vector
  2017-12-06 15:12         ` Richard Sandiford
@ 2017-12-06 15:22           ` Richard Sandiford
  2017-12-07 11:06             ` Richard Biener
  2017-12-06 15:23           ` Use tree_vector_builder::new_unary_operation for folding Richard Sandiford
                             ` (5 subsequent siblings)
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:22 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

This patch switches most build_vector calls over to tree_vector_builder,
using explicit encodings where appropriate.  Later patches handle
the remaining uses of build_vector.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard


2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* config/sparc/sparc.c: Include tree-vector-builder.h.
	(sparc_fold_builtin): Use tree_vector_builder instead of build_vector.
	* expmed.c: Include tree-vector-builder.h.
	(make_tree): Use tree_vector_builder instead of build_vector.
	* fold-const.c: Include tree-vector-builder.h.
	(const_binop): Use tree_vector_builder instead of build_vector.
	(const_unop): Likewise.
	(native_interpret_vector): Likewise.
	(fold_vec_perm): Likewise.
	(fold_ternary_loc): Likewise.
	* gimple-fold.c: Include tree-vector-builder.h.
	(gimple_fold_stmt_to_constant_1): Use tree_vector_builder instead
	of build_vector.
	* tree-ssa-forwprop.c: Include tree-vector-builder.h.
	(simplify_vector_constructor): Use tree_vector_builder instead
	of build_vector.
	* tree-vect-generic.c: Include tree-vector-builder.h.
	(add_rshift): Use tree_vector_builder instead of build_vector.
	(expand_vector_divmod): Likewise.
	(optimize_vector_constructor): Likewise.
	* tree-vect-loop.c: Include tree-vector-builder.h.
	(vect_create_epilog_for_reduction): Use tree_vector_builder instead
	of build_vector.  Explicitly use a stepped encoding for
	{ 1, 2, 3, ... }.
	* tree-vect-slp.c: Include tree-vector-builder.h.
	(vect_get_constant_vectors): Use tree_vector_builder instead
	of build_vector.
	(vect_transform_slp_perm_load): Likewise.
	(vect_schedule_slp_instance): Likewise.
	* tree-vect-stmts.c: Include tree-vector-builder.h.
	(vectorizable_bswap): Use tree_vector_builder instead of build_vector.
	(vect_gen_perm_mask_any): Likewise.
	(vectorizable_call): Likewise.  Explicitly use a stepped encoding.
	* tree.c: (build_vector_from_ctor): Use tree_vector_builder instead
	of build_vector.
	(build_vector_from_val): Likewise.  Explicitly use a duplicate
	encoding.

Index: gcc/config/sparc/sparc.c
===================================================================
--- gcc/config/sparc/sparc.c	2017-12-05 14:24:52.587013199 +0000
+++ gcc/config/sparc/sparc.c	2017-12-06 14:48:52.885162299 +0000
@@ -57,6 +57,7 @@ the Free Software Foundation; either ver
 #include "tree-pass.h"
 #include "context.h"
 #include "builtins.h"
+#include "tree-vector-builder.h"
 
 /* This file should be included last.  */
 #include "target-def.h"
@@ -11752,14 +11753,14 @@ sparc_fold_builtin (tree fndecl, int n_a
 	  tree inner_type = TREE_TYPE (rtype);
 	  unsigned i;
 
-	  auto_vec<tree, 32> n_elts (VECTOR_CST_NELTS (arg0));
+	  tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
 	  for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
 	    {
 	      unsigned HOST_WIDE_INT val
 		= TREE_INT_CST_LOW (VECTOR_CST_ELT (arg0, i));
 	      n_elts.quick_push (build_int_cst (inner_type, val << 4));
 	    }
-	  return build_vector (rtype, n_elts);
+	  return n_elts.build ();
 	}
       break;
 
@@ -11774,9 +11775,9 @@ sparc_fold_builtin (tree fndecl, int n_a
       if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
 	{
 	  tree inner_type = TREE_TYPE (rtype);
-	  auto_vec<tree, 32> n_elts (VECTOR_CST_NELTS (arg0));
+	  tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
 	  sparc_handle_vis_mul8x16 (&n_elts, code, inner_type, arg0, arg1);
-	  return build_vector (rtype, n_elts);
+	  return n_elts.build ();
 	}
       break;
 
@@ -11788,7 +11789,7 @@ sparc_fold_builtin (tree fndecl, int n_a
 
       if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
 	{
-	  auto_vec<tree, 32> n_elts (2 * VECTOR_CST_NELTS (arg0));
+	  tree_vector_builder n_elts (rtype, 2 * VECTOR_CST_NELTS (arg0), 1);
 	  unsigned i;
 	  for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
 	    {
@@ -11796,7 +11797,7 @@ sparc_fold_builtin (tree fndecl, int n_a
 	      n_elts.quick_push (VECTOR_CST_ELT (arg1, i));
 	    }
 
-	  return build_vector (rtype, n_elts);
+	  return n_elts.build ();
 	}
       break;
 
Index: gcc/expmed.c
===================================================================
--- gcc/expmed.c	2017-11-29 11:06:34.698180685 +0000
+++ gcc/expmed.c	2017-12-06 14:48:52.886162258 +0000
@@ -40,6 +40,7 @@ Software Foundation; either version 3, o
 #include "explow.h"
 #include "expr.h"
 #include "langhooks.h"
+#include "tree-vector-builder.h"
 
 struct target_expmed default_target_expmed;
 #if SWITCHABLE_TARGET
@@ -5184,14 +5185,14 @@ make_tree (tree type, rtx x)
 	int i;
 
 	/* Build a tree with vector elements.  */
-	auto_vec<tree, 32> elts (units);
+	tree_vector_builder elts (type, units, 1);
 	for (i = 0; i < units; ++i)
 	  {
 	    rtx elt = CONST_VECTOR_ELT (x, i);
 	    elts.quick_push (make_tree (itype, elt));
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     case PLUS:
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-12-06 14:46:14.129599986 +0000
+++ gcc/fold-const.c	2017-12-06 14:48:52.887162217 +0000
@@ -81,6 +81,7 @@ Software Foundation; either version 3, o
 #include "selftest.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "tree-vector-builder.h"
 
 /* Nonzero if we are folding constants inside an initializer; zero
    otherwise.  */
@@ -1507,7 +1508,7 @@ const_binop (enum tree_code code, tree t
 	gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
 		    && out_nelts == TYPE_VECTOR_SUBPARTS (type));
 
-	auto_vec<tree, 32> elts (out_nelts);
+	tree_vector_builder elts (type, out_nelts, 1);
 	for (i = 0; i < out_nelts; i++)
 	  {
 	    tree elt = (i < in_nelts
@@ -1521,7 +1522,7 @@ const_binop (enum tree_code code, tree t
 	    elts.quick_push (elt);
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     case VEC_WIDEN_MULT_LO_EXPR:
@@ -1548,7 +1549,7 @@ const_binop (enum tree_code code, tree t
 	else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
 	  scale = 1, ofs = 1;
 
-	auto_vec<tree, 32> elts (out_nelts);
+	tree_vector_builder elts (type, out_nelts, 1);
 	for (out = 0; out < out_nelts; out++)
 	  {
 	    unsigned int in = (out << scale) + ofs;
@@ -1565,7 +1566,7 @@ const_binop (enum tree_code code, tree t
 	    elts.quick_push (elt);
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     default:;
@@ -1704,7 +1705,7 @@ const_unop (enum tree_code code, tree ty
 	else
 	  subcode = FLOAT_EXPR;
 
-	auto_vec<tree, 32> elts (out_nelts);
+	tree_vector_builder elts (type, out_nelts, 1);
 	for (i = 0; i < out_nelts; i++)
 	  {
 	    tree elt = fold_convert_const (subcode, TREE_TYPE (type),
@@ -1714,7 +1715,7 @@ const_unop (enum tree_code code, tree ty
 	    elts.quick_push (elt);
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     default:
@@ -7377,7 +7378,7 @@ native_interpret_vector (tree type, cons
   if (size * count > len)
     return NULL_TREE;
 
-  auto_vec<tree, 32> elements (count);
+  tree_vector_builder elements (type, count, 1);
   for (i = 0; i < count; ++i)
     {
       elem = native_interpret_expr (etype, ptr+(i*size), size);
@@ -7385,7 +7386,7 @@ native_interpret_vector (tree type, cons
 	return NULL_TREE;
       elements.quick_push (elem);
     }
-  return build_vector (type, elements);
+  return elements.build ();
 }
 
 
@@ -8755,7 +8756,7 @@ fold_vec_perm (tree type, tree arg0, tre
       || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
     return NULL_TREE;
 
-  auto_vec<tree, 32> out_elts (nelts);
+  tree_vector_builder out_elts (type, nelts, 1);
   for (i = 0; i < nelts; i++)
     {
       if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
@@ -8772,7 +8773,7 @@ fold_vec_perm (tree type, tree arg0, tre
       return build_constructor (type, v);
     }
   else
-    return build_vector (type, out_elts);
+    return out_elts.build ();
 }
 
 /* Try to fold a pointer difference of type TYPE two address expressions of
@@ -11426,10 +11427,10 @@ fold_ternary_loc (location_t loc, enum t
 		  if (n == 1)
 		    return VECTOR_CST_ELT (arg0, idx);
 
-		  auto_vec<tree, 32> vals (n);
+		  tree_vector_builder vals (type, n, 1);
 		  for (unsigned i = 0; i < n; ++i)
 		    vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
-		  return build_vector (type, vals);
+		  return vals.build ();
 		}
 	    }
 	}
@@ -11565,10 +11566,10 @@ fold_ternary_loc (location_t loc, enum t
 	  if (need_mask_canon && arg2 == op2)
 	    {
 	      tree eltype = TREE_TYPE (TREE_TYPE (arg2));
-	      auto_vec<tree, 32> tsel (nelts);
+	      tree_vector_builder tsel (TREE_TYPE (arg2), nelts, 1);
 	      for (i = 0; i < nelts; i++)
 		tsel.quick_push (build_int_cst (eltype, sel[i]));
-	      op2 = build_vector (TREE_TYPE (arg2), tsel);
+	      op2 = tsel.build ();
 	      changed = true;
 	    }
 
@@ -11608,11 +11609,11 @@ fold_ternary_loc (location_t loc, enum t
 	      else
 		{
 		  unsigned int nelts = VECTOR_CST_NELTS (arg0);
-		  auto_vec<tree, 32> elts (nelts);
+		  tree_vector_builder elts (type, nelts, 1);
 		  elts.quick_grow (nelts);
-		  for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
+		  for (unsigned int i = 0; i < nelts; ++i)
 		    elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
-		  return build_vector (type, elts);
+		  return elts.build ();
 		}
 	    }
 	}
Index: gcc/gimple-fold.c
===================================================================
--- gcc/gimple-fold.c	2017-12-05 14:24:56.014949665 +0000
+++ gcc/gimple-fold.c	2017-12-06 14:48:52.888162175 +0000
@@ -63,6 +63,7 @@ Free Software Foundation; either version
 #include "diagnostic-core.h"
 #include "intl.h"
 #include "calls.h"
+#include "tree-vector-builder.h"
 
 /* Return true when DECL can be referenced from current unit.
    FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
@@ -6022,7 +6023,7 @@ gimple_fold_stmt_to_constant_1 (gimple *
 		  tree val;
 
 		  nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs));
-		  auto_vec<tree, 32> vec (nelts);
+		  tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
 		  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
 		    {
 		      val = (*valueize) (val);
@@ -6034,7 +6035,7 @@ gimple_fold_stmt_to_constant_1 (gimple *
 			return NULL_TREE;
 		    }
 
-		  return build_vector (TREE_TYPE (rhs), vec);
+		  return vec.build ();
 		}
 	      if (subcode == OBJ_TYPE_REF)
 		{
Index: gcc/tree-ssa-forwprop.c
===================================================================
--- gcc/tree-ssa-forwprop.c	2017-11-29 11:06:31.942215941 +0000
+++ gcc/tree-ssa-forwprop.c	2017-12-06 14:48:52.888162175 +0000
@@ -46,6 +46,7 @@ the Free Software Foundation; either ver
 #include "tree-cfgcleanup.h"
 #include "cfganal.h"
 #include "optabs-tree.h"
+#include "tree-vector-builder.h"
 
 /* This pass propagates the RHS of assignment statements into use
    sites of the LHS of the assignment.  It's basically a specialized
@@ -2116,10 +2117,10 @@ simplify_vector_constructor (gimple_stmt
 	  || GET_MODE_SIZE (TYPE_MODE (mask_type))
 	     != GET_MODE_SIZE (TYPE_MODE (type)))
 	return false;
-      auto_vec<tree, 32> mask_elts (nelts);
+      tree_vector_builder mask_elts (mask_type, nelts, 1);
       for (i = 0; i < nelts; i++)
 	mask_elts.quick_push (build_int_cst (TREE_TYPE (mask_type), sel[i]));
-      op2 = build_vector (mask_type, mask_elts);
+      op2 = mask_elts.build ();
       if (conv_code == ERROR_MARK)
 	gimple_assign_set_rhs_with_ops (gsi, VEC_PERM_EXPR, orig, orig, op2);
       else
Index: gcc/tree-vect-generic.c
===================================================================
--- gcc/tree-vect-generic.c	2017-11-29 11:06:34.810688336 +0000
+++ gcc/tree-vect-generic.c	2017-12-06 14:48:52.889162134 +0000
@@ -37,6 +37,7 @@ Free Software Foundation; either version
 #include "gimplify-me.h"
 #include "gimplify.h"
 #include "tree-cfg.h"
+#include "tree-vector-builder.h"
 
 
 static void expand_vector_operations_1 (gimple_stmt_iterator *);
@@ -398,11 +399,10 @@ add_rshift (gimple_stmt_iterator *gsi, t
   if (op != unknown_optab
       && optab_handler (op, TYPE_MODE (type)) != CODE_FOR_nothing)
     {
-      auto_vec<tree, 32> vec (nunits);
+      tree_vector_builder vec (type, nunits, 1);
       for (i = 0; i < nunits; i++)
 	vec.quick_push (build_int_cst (TREE_TYPE (type), shiftcnts[i]));
-      return gimplify_build2 (gsi, RSHIFT_EXPR, type, op0,
-			      build_vector (type, vec));
+      return gimplify_build2 (gsi, RSHIFT_EXPR, type, op0, vec.build ());
     }
 
   return NULL_TREE;
@@ -635,12 +635,12 @@ expand_vector_divmod (gimple_stmt_iterat
 	      mask_type = build_same_sized_truth_vector_type (type);
 	      zero = build_zero_cst (type);
 	      cond = build2 (LT_EXPR, mask_type, op0, zero);
-	      auto_vec<tree, 32> vec (nunits);
+	      tree_vector_builder vec (type, nunits, 1);
 	      for (i = 0; i < nunits; i++)
 		vec.quick_push (build_int_cst (TREE_TYPE (type),
 					       (HOST_WIDE_INT_1U
 						<< shifts[i]) - 1));
-	      cst = build_vector (type, vec);
+	      cst = vec.build ();
 	      addend = make_ssa_name (type);
 	      stmt = gimple_build_assign (addend, VEC_COND_EXPR, cond,
 					  cst, zero);
@@ -674,12 +674,12 @@ expand_vector_divmod (gimple_stmt_iterat
       else
 	{
 	  tree mask;
-	  auto_vec<tree, 32> vec (nunits);
+	  tree_vector_builder vec (type, nunits, 1);
 	  for (i = 0; i < nunits; i++)
 	    vec.quick_push (build_int_cst (TREE_TYPE (type),
 					   (HOST_WIDE_INT_1U
 					    << shifts[i]) - 1));
-	  mask = build_vector (type, vec);
+	  mask = vec.build ();
 	  op = optab_for_tree_code (BIT_AND_EXPR, type, optab_default);
 	  if (op != unknown_optab
 	      && optab_handler (op, TYPE_MODE (type)) != CODE_FOR_nothing)
@@ -753,10 +753,10 @@ expand_vector_divmod (gimple_stmt_iterat
       return NULL_TREE;
     }
 
-  auto_vec<tree, 32> vec (nunits);
+  tree_vector_builder vec (type, nunits, 1);
   for (i = 0; i < nunits; i++)
     vec.quick_push (build_int_cst (TREE_TYPE (type), mulc[i]));
-  mulcst = build_vector (type, vec);
+  mulcst = vec.build ();
 
   cur_op = gimplify_build2 (gsi, MULT_HIGHPART_EXPR, type, cur_op, mulcst);
 
@@ -1104,7 +1104,7 @@ optimize_vector_constructor (gimple_stmt
       }
   if (all_same)
     return;
-  auto_vec<tree, 32> cst (nelts);
+  tree_vector_builder cst (type, nelts, 1);
   for (i = 0; i < nelts; i++)
     {
       tree this_base = CONSTRUCTOR_ELT (rhs, i)->value;
@@ -1127,7 +1127,7 @@ optimize_vector_constructor (gimple_stmt
   g = gimple_build_assign (make_ssa_name (type), rhs);
   gsi_insert_before (gsi, g, GSI_SAME_STMT);
   g = gimple_build_assign (lhs, PLUS_EXPR, gimple_assign_lhs (g),
-			   build_vector (type, cst));
+			   cst.build ());
   gsi_replace (gsi, g, false);
 }
 \f
Index: gcc/tree-vect-loop.c
===================================================================
--- gcc/tree-vect-loop.c	2017-11-29 11:06:34.910905236 +0000
+++ gcc/tree-vect-loop.c	2017-12-06 14:48:52.889162134 +0000
@@ -51,6 +51,7 @@ Software Foundation; either version 3, o
 #include "tree-cfg.h"
 #include "tree-if-conv.h"
 #include "internal-fn.h"
+#include "tree-vector-builder.h"
 
 /* Loop Vectorization Pass.
 
@@ -4497,10 +4498,10 @@ vect_create_epilog_for_reduction (vec<tr
 	 vector size (STEP).  */
 
       /* Create a {1,2,3,...} vector.  */
-      auto_vec<tree, 32> vtemp (nunits_out);
-      for (k = 0; k < nunits_out; ++k)
+      tree_vector_builder vtemp (cr_index_vector_type, 1, 3);
+      for (k = 0; k < 3; ++k)
 	vtemp.quick_push (build_int_cst (cr_index_scalar_type, k + 1));
-      tree series_vect = build_vector (cr_index_vector_type, vtemp);
+      tree series_vect = vtemp.build ();
 
       /* Create a vector of the step value.  */
       tree step = build_int_cst (cr_index_scalar_type, nunits_out);
Index: gcc/tree-vect-slp.c
===================================================================
--- gcc/tree-vect-slp.c	2017-12-05 14:24:56.178946625 +0000
+++ gcc/tree-vect-slp.c	2017-12-06 14:48:52.890162093 +0000
@@ -41,6 +41,7 @@ Software Foundation; either version 3, o
 #include "langhooks.h"
 #include "gimple-walk.h"
 #include "dbgcnt.h"
+#include "tree-vector-builder.h"
 
 
 /* Recursively free the memory allocated for the SLP tree rooted at NODE.  */
@@ -3222,7 +3223,7 @@ vect_get_constant_vectors (tree op, slp_
 
   number_of_places_left_in_vector = nunits;
   constant_p = true;
-  auto_vec<tree, 32> elts (nunits);
+  tree_vector_builder elts (vector_type, nunits, 1);
   elts.quick_grow (nunits);
   bool place_after_defs = false;
   for (j = 0; j < number_of_copies; j++)
@@ -3340,7 +3341,7 @@ vect_get_constant_vectors (tree op, slp_
           if (number_of_places_left_in_vector == 0)
             {
 	      if (constant_p)
-		vec_cst = build_vector (vector_type, elts);
+		vec_cst = elts.build ();
 	      else
 		{
 		  vec<constructor_elt, va_gc> *v;
@@ -3371,6 +3372,8 @@ vect_get_constant_vectors (tree op, slp_
 	      place_after_defs = false;
               number_of_places_left_in_vector = nunits;
 	      constant_p = true;
+	      elts.new_vector (vector_type, nunits, 1);
+	      elts.quick_grow (nunits);
             }
         }
     }
@@ -3667,11 +3670,11 @@ vect_transform_slp_perm_load (slp_tree n
 		  
 		  if (! noop_p)
 		    {
-		      auto_vec<tree, 32> mask_elts (nunits);
+		      tree_vector_builder mask_elts (mask_type, nunits, 1);
 		      for (int l = 0; l < nunits; ++l)
 			mask_elts.quick_push (build_int_cst (mask_element_type,
 							     mask[l]));
-		      mask_vec = build_vector (mask_type, mask_elts);
+		      mask_vec = mask_elts.build ();
 		    }
 
 		  if (second_vec_index == -1)
@@ -3823,7 +3826,7 @@ vect_schedule_slp_instance (slp_tree nod
 	  for (j = 0; j < v0.length (); ++j)
 	    {
 	      unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
-	      auto_vec<tree, 32> melts (nunits);
+	      tree_vector_builder melts (mvectype, nunits, 1);
 	      for (l = 0; l < nunits; ++l)
 		{
 		  if (k >= group_size)
@@ -3831,7 +3834,7 @@ vect_schedule_slp_instance (slp_tree nod
 		  tree t = build_int_cst (meltype, mask[k++] * nunits + l);
 		  melts.quick_push (t);
 		}
-	      tmask = build_vector (mvectype, melts);
+	      tmask = melts.build ();
 
 	      /* ???  Not all targets support a VEC_PERM_EXPR with a
 	         constant mask that would translate to a vec_merge RTX
Index: gcc/tree-vect-stmts.c
===================================================================
--- gcc/tree-vect-stmts.c	2017-11-29 11:06:29.970567408 +0000
+++ gcc/tree-vect-stmts.c	2017-12-06 14:48:52.890162093 +0000
@@ -48,6 +48,7 @@ Software Foundation; either version 3, o
 #include "tree-vectorizer.h"
 #include "builtins.h"
 #include "internal-fn.h"
+#include "tree-vector-builder.h"
 
 /* For lang_hooks.types.type_for_mode.  */
 #include "langhooks.h"
@@ -2520,10 +2521,10 @@ vectorizable_bswap (gimple *stmt, gimple
       return true;
     }
 
-  auto_vec<tree, 32> telts (num_bytes);
+  tree_vector_builder telts (char_vectype, num_bytes, 1);
   for (unsigned i = 0; i < num_bytes; ++i)
     telts.quick_push (build_int_cst (char_type_node, elts[i]));
-  tree bswap_vconst = build_vector (char_vectype, telts);
+  tree bswap_vconst = telts.build ();
 
   /* Transform.  */
   vec<tree> vec_oprnds = vNULL;
@@ -2941,11 +2942,11 @@ vectorizable_call (gimple *gs, gimple_st
 	  if (gimple_call_internal_p (stmt)
 	      && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
 	    {
-	      auto_vec<tree, 32> v (nunits_out);
-	      for (int k = 0; k < nunits_out; ++k)
+	      tree_vector_builder v (vectype_out, 1, 3);
+	      for (int k = 0; k < 3; ++k)
 		v.quick_push (build_int_cst (unsigned_type_node,
 					     j * nunits_out + k));
-	      tree cst = build_vector (vectype_out, v);
+	      tree cst = v.build ();
 	      tree new_var
 		= vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
 	      gimple *init_stmt = gimple_build_assign (new_var, cst);
@@ -6507,7 +6508,7 @@ vectorizable_store (gimple *stmt, gimple
 tree
 vect_gen_perm_mask_any (tree vectype, vec_perm_indices sel)
 {
-  tree mask_elt_type, mask_type, mask_vec;
+  tree mask_elt_type, mask_type;
 
   unsigned int nunits = sel.length ();
   gcc_checking_assert (nunits == TYPE_VECTOR_SUBPARTS (vectype));
@@ -6516,12 +6517,10 @@ vect_gen_perm_mask_any (tree vectype, ve
     (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))).require (), 1);
   mask_type = get_vectype_for_scalar_type (mask_elt_type);
 
-  auto_vec<tree, 32> mask_elts (nunits);
+  tree_vector_builder mask_elts (mask_type, nunits, 1);
   for (unsigned int i = 0; i < nunits; ++i)
     mask_elts.quick_push (build_int_cst (mask_elt_type, sel[i]));
-  mask_vec = build_vector (mask_type, mask_elts);
-
-  return mask_vec;
+  return mask_elts.build ();
 }
 
 /* Checked version of vect_gen_perm_mask_any.  Asserts can_vec_perm_p,
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2017-12-06 14:46:14.132599861 +0000
+++ gcc/tree.c	2017-12-06 14:48:52.891162052 +0000
@@ -1757,7 +1757,7 @@ build_vector_from_ctor (tree type, vec<c
   unsigned HOST_WIDE_INT idx;
   tree value;
 
-  auto_vec<tree, 32> vec (nelts);
+  tree_vector_builder vec (type, nelts, 1);
   FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
     {
       if (TREE_CODE (value) == VECTOR_CST)
@@ -1769,7 +1769,7 @@ build_vector_from_ctor (tree type, vec<c
   while (vec.length () < nelts)
     vec.quick_push (build_zero_cst (TREE_TYPE (type)));
 
-  return build_vector (type, vec);
+  return vec.build ();
 }
 
 /* Build a vector of type VECTYPE where all the elements are SCs.  */
@@ -1792,10 +1792,9 @@ build_vector_from_val (tree vectype, tre
 
   if (CONSTANT_CLASS_P (sc))
     {
-      auto_vec<tree, 32> v (nunits);
-      for (i = 0; i < nunits; ++i)
-	v.quick_push (sc);
-      return build_vector (vectype, v);
+      tree_vector_builder v (vectype, 1, 1);
+      v.quick_push (sc);
+      return v.build ();
     }
   else
     {

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Use tree_vector_builder::new_unary_operation for folding
  2017-12-06 15:12         ` Richard Sandiford
  2017-12-06 15:22           ` Use tree_vector_builder instead of build_vector Richard Sandiford
@ 2017-12-06 15:23           ` Richard Sandiford
  2017-12-07 11:06             ` Richard Biener
  2017-12-06 15:24           ` Use tree_vector_builder::new_binary_operation " Richard Sandiford
                             ` (4 subsequent siblings)
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:23 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

This patch makes fold-const.c operate directly on the VECTOR_CST
encoding when folding an operation that has a single VECTOR_CST input.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard

2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* fold-const.c (fold_negate_expr_1): Use tree_vector_builder and
	new_unary_operation, operating only on the encoded elements.
	(const_unop): Likewise.
	(exact_inverse): Likewise.
	(distributes_over_addition_p): New function.
	(const_binop): Use tree_vector_builder and new_unary_operation
	for combinations of VECTOR_CST and INTEGER_CST.  Operate only
	on the encoded elements unless the encoding is strided and the
	operation does not distribute over addition.
	(fold_convert_const):  Use tree_vector_builder and
	new_unary_operation.  Operate only on the encoded elements
	for truncating integer conversions, or for non-stepped encodings.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-12-06 14:48:52.887162217 +0000
+++ gcc/fold-const.c	2017-12-06 14:48:56.997993407 +0000
@@ -566,10 +566,10 @@ fold_negate_expr_1 (location_t loc, tree
 
     case VECTOR_CST:
       {
-	int count = VECTOR_CST_NELTS (t), i;
-
-	auto_vec<tree, 32> elts (count);
-	for (i = 0; i < count; i++)
+	tree_vector_builder elts;
+	elts.new_unary_operation (type, t, true);
+	unsigned int count = elts.encoded_nelts ();
+	for (unsigned int i = 0; i < count; ++i)
 	  {
 	    tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
 	    if (elt == NULL_TREE)
@@ -577,7 +577,7 @@ fold_negate_expr_1 (location_t loc, tree
 	    elts.quick_push (elt);
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     case COMPLEX_EXPR:
@@ -1121,6 +1121,27 @@ int_const_binop (enum tree_code code, co
   return int_const_binop_1 (code, arg1, arg2, 1);
 }
 
+/* Return true if binary operation OP distributes over addition in operand
+   OPNO, with the other operand being held constant.  OPNO counts from 1.  */
+
+static bool
+distributes_over_addition_p (tree_code op, int opno)
+{
+  switch (op)
+    {
+    case PLUS_EXPR:
+    case MINUS_EXPR:
+    case MULT_EXPR:
+      return true;
+
+    case LSHIFT_EXPR:
+      return opno == 1;
+
+    default:
+      return false;
+    }
+}
+
 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
    constant.  We assume ARG1 and ARG2 have the same data type, or at least
    are the same kind of constant and the same machine mode.  Return zero if
@@ -1442,10 +1463,12 @@ const_binop (enum tree_code code, tree a
       && TREE_CODE (arg2) == INTEGER_CST)
     {
       tree type = TREE_TYPE (arg1);
-      int count = VECTOR_CST_NELTS (arg1), i;
-
-      auto_vec<tree, 32> elts (count);
-      for (i = 0; i < count; i++)
+      bool step_ok_p = distributes_over_addition_p (code, 1);
+      tree_vector_builder elts;
+      if (!elts.new_unary_operation (type, arg1, step_ok_p))
+	return NULL_TREE;
+      unsigned int count = elts.encoded_nelts ();
+      for (unsigned int i = 0; i < count; ++i)
 	{
 	  tree elem1 = VECTOR_CST_ELT (arg1, i);
 
@@ -1458,7 +1481,7 @@ const_binop (enum tree_code code, tree a
 	  elts.quick_push (elt);
 	}
 
-      return build_vector (type, elts);
+      return elts.build ();
     }
   return NULL_TREE;
 }
@@ -1649,10 +1672,12 @@ const_unop (enum tree_code code, tree ty
       else if (TREE_CODE (arg0) == VECTOR_CST)
 	{
 	  tree elem;
-	  unsigned count = VECTOR_CST_NELTS (arg0), i;
 
-	  auto_vec<tree, 32> elements (count);
-	  for (i = 0; i < count; i++)
+	  /* This can cope with stepped encodings because ~x == -1 - x.  */
+	  tree_vector_builder elements;
+	  elements.new_unary_operation (type, arg0, true);
+	  unsigned int i, count = elements.encoded_nelts ();
+	  for (i = 0; i < count; ++i)
 	    {
 	      elem = VECTOR_CST_ELT (arg0, i);
 	      elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
@@ -1661,7 +1686,7 @@ const_unop (enum tree_code code, tree ty
 	      elements.quick_push (elem);
 	    }
 	  if (i == count)
-	    return build_vector (type, elements);
+	    return elements.build ();
 	}
       break;
 
@@ -2135,10 +2160,19 @@ fold_convert_const (enum tree_code code,
       if (TREE_CODE (arg1) == VECTOR_CST
 	  && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
 	{
-	  int len = VECTOR_CST_NELTS (arg1);
 	  tree elttype = TREE_TYPE (type);
-	  auto_vec<tree, 32> v (len);
-	  for (int i = 0; i < len; ++i)
+	  tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
+	  /* We can't handle steps directly when extending, since the
+	     values need to wrap at the original precision first.  */
+	  bool step_ok_p
+	    = (INTEGRAL_TYPE_P (elttype)
+	       && INTEGRAL_TYPE_P (arg1_elttype)
+	       && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
+	  tree_vector_builder v;
+	  if (!v.new_unary_operation (type, arg1, step_ok_p))
+	    return NULL_TREE;
+	  unsigned int len = v.encoded_nelts ();
+	  for (unsigned int i = 0; i < len; ++i)
 	    {
 	      tree elt = VECTOR_CST_ELT (arg1, i);
 	      tree cvt = fold_convert_const (code, elttype, elt);
@@ -2146,7 +2180,7 @@ fold_convert_const (enum tree_code code,
 		return NULL_TREE;
 	      v.quick_push (cvt);
 	    }
-	  return build_vector (type, v);
+	  return v.build ();
 	}
     }
   return NULL_TREE;
@@ -8832,7 +8866,6 @@ exact_inverse (tree type, tree cst)
   REAL_VALUE_TYPE r;
   tree unit_type;
   machine_mode mode;
-  unsigned vec_nelts, i;
 
   switch (TREE_CODE (cst))
     {
@@ -8846,12 +8879,14 @@ exact_inverse (tree type, tree cst)
 
     case VECTOR_CST:
       {
-	vec_nelts = VECTOR_CST_NELTS (cst);
 	unit_type = TREE_TYPE (type);
 	mode = TYPE_MODE (unit_type);
 
-	auto_vec<tree, 32> elts (vec_nelts);
-	for (i = 0; i < vec_nelts; i++)
+	tree_vector_builder elts;
+	if (!elts.new_unary_operation (type, cst, false))
+	  return NULL_TREE;
+	unsigned int count = elts.encoded_nelts ();
+	for (unsigned int i = 0; i < count; ++i)
 	  {
 	    r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
 	    if (!exact_real_inverse (mode, &r))
@@ -8859,7 +8894,7 @@ exact_inverse (tree type, tree cst)
 	    elts.quick_push (build_real (unit_type, r));
 	  }
 
-	return build_vector (type, elts);
+	return elts.build ();
       }
 
     default:

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Use tree_vector_builder::new_binary_operation for folding
  2017-12-06 15:12         ` Richard Sandiford
  2017-12-06 15:22           ` Use tree_vector_builder instead of build_vector Richard Sandiford
  2017-12-06 15:23           ` Use tree_vector_builder::new_unary_operation for folding Richard Sandiford
@ 2017-12-06 15:24           ` Richard Sandiford
  2017-12-07 11:07             ` Richard Biener
  2017-12-06 15:25           ` Make gimple_build_vector take a tree_vector_builder Richard Sandiford
                             ` (3 subsequent siblings)
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:24 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

This patch makes fold-const.c operate directly on the VECTOR_CST
encoding when folding an operation that has two VECTOR_CST inputs.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard


2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* tree-vector-builder.h
	(tree_vector_builder::new_binary_operation): Declare.
	* tree-vector-builder.c
	(tree_vector_builder::new_binary_operation): New function.
	* fold-const.c (fold_relational_const): Use it.
	(const_binop): Likewise.  Check that both input vectors have
	the same number of elements, thus excluding things like WIDEN_SUM.
	Check whether it is possible to operate directly on the encodings
	of stepped inputs.

Index: gcc/tree-vector-builder.h
===================================================================
--- gcc/tree-vector-builder.h	2017-12-06 14:46:14.131599903 +0000
+++ gcc/tree-vector-builder.h	2017-12-06 14:49:00.386854068 +0000
@@ -38,6 +38,7 @@ #define GCC_TREE_VECTOR_BUILDER_H
 
   void new_vector (tree, unsigned int, unsigned int);
   bool new_unary_operation (tree, tree, bool);
+  bool new_binary_operation (tree, tree, tree, bool);
 
 private:
   bool equal_p (const_tree, const_tree) const;
Index: gcc/tree-vector-builder.c
===================================================================
--- gcc/tree-vector-builder.c	2017-12-06 14:46:14.131599903 +0000
+++ gcc/tree-vector-builder.c	2017-12-06 14:49:00.386854068 +0000
@@ -49,6 +49,53 @@ tree_vector_builder::new_unary_operation
   return true;
 }
 
+/* Try to start building a new vector of type TYPE that holds the result of
+   a binary operation on VECTOR_CSTs T1 and T2.  ALLOW_STEPPED_P is true if
+   the operation can handle stepped encodings directly, without having to
+   expand the full sequence.
+
+   Return true if the operation is possible.  Leave the builder unchanged
+   otherwise.  */
+
+bool
+tree_vector_builder::new_binary_operation (tree type, tree t1, tree t2,
+					   bool allow_stepped_p)
+{
+  unsigned int full_nelts = TYPE_VECTOR_SUBPARTS (type);
+  gcc_assert (full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
+	      && full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)));
+  /* Conceptually we split the patterns in T1 and T2 until we have
+     an equal number for both.  Each split pattern requires the same
+     number of elements per pattern as the original.  E.g. splitting:
+
+       { 1, 2, 3, ... }
+
+     into two gives:
+
+       { 1, 3, 5, ... }
+       { 2, 4, 6, ... }
+
+     while splitting:
+
+       { 1, 0, ... }
+
+     into two gives:
+
+       { 1, 0, ... }
+       { 0, 0, ... }.  */
+  unsigned int npatterns = least_common_multiple (VECTOR_CST_NPATTERNS (t1),
+						  VECTOR_CST_NPATTERNS (t2));
+  unsigned int nelts_per_pattern = MAX (VECTOR_CST_NELTS_PER_PATTERN (t1),
+					VECTOR_CST_NELTS_PER_PATTERN (t2));
+  if (!allow_stepped_p && nelts_per_pattern > 2)
+    {
+      npatterns = full_nelts;
+      nelts_per_pattern = 1;
+    }
+  new_vector (type, npatterns, nelts_per_pattern);
+  return true;
+}
+
 /* Return a VECTOR_CST for the current constant.  */
 
 tree
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-12-06 14:48:56.997993407 +0000
+++ gcc/fold-const.c	2017-12-06 14:49:00.386854068 +0000
@@ -1435,13 +1435,40 @@ const_binop (enum tree_code code, tree a
     }
 
   if (TREE_CODE (arg1) == VECTOR_CST
-      && TREE_CODE (arg2) == VECTOR_CST)
+      && TREE_CODE (arg2) == VECTOR_CST
+      && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))
+	  == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
     {
       tree type = TREE_TYPE (arg1);
-      int count = VECTOR_CST_NELTS (arg1), i;
+      bool step_ok_p;
+      if (VECTOR_CST_STEPPED_P (arg1)
+	  && VECTOR_CST_STEPPED_P (arg2))
+	/* We can operate directly on the encoding if:
+
+	      a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
+	    implies
+	      (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
+
+	   Addition and subtraction are the supported operators
+	   for which this is true.  */
+	step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
+      else if (VECTOR_CST_STEPPED_P (arg1))
+	/* We can operate directly on stepped encodings if:
+
+	     a3 - a2 == a2 - a1
+	   implies:
+	     (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
 
-      auto_vec<tree, 32> elts (count);
-      for (i = 0; i < count; i++)
+	   which is true if (x -> x op c) distributes over addition.  */
+	step_ok_p = distributes_over_addition_p (code, 1);
+      else
+	/* Similarly in reverse.  */
+	step_ok_p = distributes_over_addition_p (code, 2);
+      tree_vector_builder elts;
+      if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
+	return NULL_TREE;
+      unsigned int count = elts.encoded_nelts ();
+      for (unsigned int i = 0; i < count; ++i)
 	{
 	  tree elem1 = VECTOR_CST_ELT (arg1, i);
 	  tree elem2 = VECTOR_CST_ELT (arg2, i);
@@ -1455,7 +1482,7 @@ const_binop (enum tree_code code, tree a
 	  elts.quick_push (elt);
 	}
 
-      return build_vector (type, elts);
+      return elts.build ();
     }
 
   /* Shifts allow a scalar offset for a vector.  */
@@ -13770,11 +13797,10 @@ fold_relational_const (enum tree_code co
 	    }
 	  return constant_boolean_node (true, type);
 	}
-      unsigned count = VECTOR_CST_NELTS (op0);
-      gcc_assert (VECTOR_CST_NELTS (op1) == count
-		  && TYPE_VECTOR_SUBPARTS (type) == count);
-
-      auto_vec<tree, 32> elts (count);
+      tree_vector_builder elts;
+      if (!elts.new_binary_operation (type, op0, op1, false))
+	return NULL_TREE;
+      unsigned int count = elts.encoded_nelts ();
       for (unsigned i = 0; i < count; i++)
 	{
 	  tree elem_type = TREE_TYPE (type);
@@ -13791,7 +13817,7 @@ fold_relational_const (enum tree_code co
 					  integer_zerop (tem) ? 0 : -1));
 	}
 
-      return build_vector (type, elts);
+      return elts.build ();
     }
 
   /* From here on we only handle LT, LE, GT, GE, EQ and NE.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Make gimple_build_vector take a tree_vector_builder
  2017-12-06 15:12         ` Richard Sandiford
                             ` (2 preceding siblings ...)
  2017-12-06 15:24           ` Use tree_vector_builder::new_binary_operation " Richard Sandiford
@ 2017-12-06 15:25           ` Richard Sandiford
  2017-12-07 11:08             ` Richard Biener
  2017-12-06 15:26           ` Make build_vector static Richard Sandiford
                             ` (2 subsequent siblings)
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:25 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

This patch changes gimple_build_vector so that it takes a
tree_vector_builder instead of a size and a vector of trees.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard


2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* vector-builder.h (vector_builder::derived): New const overload.
	(vector_builder::elt): New function.
	* tree-vector-builder.h (tree_vector_builder::type): New function.
	(tree_vector_builder::apply_step): Declare.
	* tree-vector-builder.c (tree_vector_builder::apply_step): New
	function.
	* gimple-fold.h (tree_vector_builder): Declare.
	(gimple_build_vector): Take a tree_vector_builder instead of a
	type and vector of elements.
	* gimple-fold.c (gimple_build_vector): Likewise.
	* tree-vect-loop.c (get_initial_def_for_reduction): Update call
	accordingly.
	(get_initial_defs_for_reduction): Likewise.
	(vectorizable_induction): Likewise.

Index: gcc/vector-builder.h
===================================================================
--- gcc/vector-builder.h	2017-12-06 14:46:14.133599820 +0000
+++ gcc/vector-builder.h	2017-12-06 14:49:04.289693414 +0000
@@ -68,6 +68,10 @@ #define GCC_VECTOR_BUILDER_H
 	  given integral_p (ELT1) && integral_p (ELT2).  There is no fixed
 	  choice of StepType.
 
+      T apply_step (T base, unsigned int factor, StepType step) const;
+
+	  Return a vector element with the value BASE + FACTOR * STEP.
+
       bool can_elide_p (T elt) const;
 
 	  Return true if we can drop element ELT, even if the retained
@@ -91,6 +95,7 @@ #define GCC_VECTOR_BUILDER_H
   unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
   unsigned int encoded_nelts () const;
   bool encoded_full_vector_p () const;
+  T elt (unsigned int) const;
 
   void finalize ();
 
@@ -163,6 +168,38 @@ vector_builder<T, Derived>::new_vector (
   this->truncate (0);
 }
 
+/* Return the value of vector element I, which might or might not be
+   encoded explicitly.  */
+
+template<typename T, typename Derived>
+T
+vector_builder<T, Derived>::elt (unsigned int i) const
+{
+  /* This only makes sense if the encoding has been fully populated.  */
+  gcc_checking_assert (encoded_nelts () <= this->length ());
+
+  /* First handle elements that are already present in the underlying
+     vector, regardless of whether they're part of the encoding or not.  */
+  if (i < this->length ())
+    return (*this)[i];
+
+  /* Identify the pattern that contains element I and work out the index of
+     the last encoded element for that pattern.  */
+  unsigned int pattern = i % m_npatterns;
+  unsigned int count = i / m_npatterns;
+  unsigned int final_i = encoded_nelts () - m_npatterns + pattern;
+  T final = (*this)[final_i];
+
+  /* If there are no steps, the final encoded value is the right one.  */
+  if (m_nelts_per_pattern <= 2)
+    return final;
+
+  /* Otherwise work out the value from the last two encoded elements.  */
+  T prev = (*this)[final_i - m_npatterns];
+  return derived ()->apply_step (final, count - 2,
+				 derived ()->step (prev, final));
+}
+
 /* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
    but without changing the underlying vector.  */
 
Index: gcc/tree-vector-builder.h
===================================================================
--- gcc/tree-vector-builder.h	2017-12-06 14:49:00.386854068 +0000
+++ gcc/tree-vector-builder.h	2017-12-06 14:49:04.289693414 +0000
@@ -45,6 +45,7 @@ #define GCC_TREE_VECTOR_BUILDER_H
   bool allow_steps_p () const;
   bool integral_p (const_tree) const;
   wide_int step (const_tree, const_tree) const;
+  tree apply_step (tree, unsigned int, const wide_int &) const;
   bool can_elide_p (const_tree) const;
   void note_representative (tree *, tree);
 
Index: gcc/tree-vector-builder.c
===================================================================
--- gcc/tree-vector-builder.c	2017-12-06 14:49:00.386854068 +0000
+++ gcc/tree-vector-builder.c	2017-12-06 14:49:04.289693414 +0000
@@ -96,6 +96,16 @@ tree_vector_builder::new_binary_operatio
   return true;
 }
 
+/* Return a vector element with the value BASE + FACTOR * STEP.  */
+
+tree
+tree_vector_builder::apply_step (tree base, unsigned int factor,
+				 const wide_int &step) const
+{
+  return wide_int_to_tree (TREE_TYPE (base),
+			   wi::to_wide (base) + factor * step);
+}
+
 /* Return a VECTOR_CST for the current constant.  */
 
 tree
Index: gcc/gimple-fold.h
===================================================================
--- gcc/gimple-fold.h	2017-11-29 11:06:33.003002371 +0000
+++ gcc/gimple-fold.h	2017-12-06 14:49:04.288693455 +0000
@@ -135,11 +135,13 @@ gimple_build_vector_from_val (gimple_seq
   return gimple_build_vector_from_val (seq, UNKNOWN_LOCATION, type, op);
 }
 
-extern tree gimple_build_vector (gimple_seq *, location_t, tree, vec<tree>);
+class tree_vector_builder;
+extern tree gimple_build_vector (gimple_seq *, location_t,
+				 tree_vector_builder *);
 inline tree
-gimple_build_vector (gimple_seq *seq, tree type, vec<tree> elts)
+gimple_build_vector (gimple_seq *seq, tree_vector_builder *builder)
 {
-  return gimple_build_vector (seq, UNKNOWN_LOCATION, type, elts);
+  return gimple_build_vector (seq, UNKNOWN_LOCATION, builder);
 }
 
 extern bool gimple_stmt_nonnegative_warnv_p (gimple *, bool *, int = 0);
Index: gcc/gimple-fold.c
===================================================================
--- gcc/gimple-fold.c	2017-12-06 14:48:52.888162175 +0000
+++ gcc/gimple-fold.c	2017-12-06 14:49:04.288693455 +0000
@@ -7178,23 +7178,30 @@ gimple_build_vector_from_val (gimple_seq
   return res;
 }
 
-/* Build a vector of type TYPE in which the elements have the values
-   given by ELTS.  Return a gimple value for the result, appending any
-   new instructions to SEQ.  */
+/* Build a vector from BUILDER, handling the case in which some elements
+   are non-constant.  Return a gimple value for the result, appending any
+   new instructions to SEQ.
+
+   BUILDER must not have a stepped encoding on entry.  This is because
+   the function is not geared up to handle the arithmetic that would
+   be needed in the variable case, and any code building a vector that
+   is known to be constant should use BUILDER->build () directly.  */
 
 tree
-gimple_build_vector (gimple_seq *seq, location_t loc, tree type,
-		     vec<tree> elts)
+gimple_build_vector (gimple_seq *seq, location_t loc,
+		     tree_vector_builder *builder)
 {
-  unsigned int nelts = elts.length ();
-  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
-  for (unsigned int i = 0; i < nelts; ++i)
-    if (!TREE_CONSTANT (elts[i]))
+  gcc_assert (builder->nelts_per_pattern () <= 2);
+  unsigned int encoded_nelts = builder->encoded_nelts ();
+  for (unsigned int i = 0; i < encoded_nelts; ++i)
+    if (!TREE_CONSTANT ((*builder)[i]))
       {
+	tree type = builder->type ();
+	unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
 	vec<constructor_elt, va_gc> *v;
 	vec_alloc (v, nelts);
 	for (i = 0; i < nelts; ++i)
-	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
+	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
 
 	tree res;
 	if (gimple_in_ssa_p (cfun))
@@ -7206,7 +7213,7 @@ gimple_build_vector (gimple_seq *seq, lo
 	gimple_seq_add_stmt_without_update (seq, stmt);
 	return res;
       }
-  return build_vector (type, elts);
+  return builder->build ();
 }
 
 /* Return true if the result of assignment STMT is known to be non-negative.
Index: gcc/tree-vect-loop.c
===================================================================
--- gcc/tree-vect-loop.c	2017-12-06 14:48:52.889162134 +0000
+++ gcc/tree-vect-loop.c	2017-12-06 14:49:04.289693414 +0000
@@ -3968,11 +3968,9 @@ get_initial_def_for_reduction (gimple *s
   struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
   tree scalar_type = TREE_TYPE (init_val);
   tree vectype = get_vectype_for_scalar_type (scalar_type);
-  int nunits;
   enum tree_code code = gimple_assign_rhs_code (stmt);
   tree def_for_init;
   tree init_def;
-  int i;
   bool nested_in_vect_loop = false;
   REAL_VALUE_TYPE real_init_val = dconst0;
   int int_init_val = 0;
@@ -3980,7 +3978,6 @@ get_initial_def_for_reduction (gimple *s
   gimple_seq stmts = NULL;
 
   gcc_assert (vectype);
-  nunits = TYPE_VECTOR_SUBPARTS (vectype);
 
   gcc_assert (POINTER_TYPE_P (scalar_type) || INTEGRAL_TYPE_P (scalar_type)
 	      || SCALAR_FLOAT_TYPE_P (scalar_type));
@@ -4053,11 +4050,10 @@ get_initial_def_for_reduction (gimple *s
 	else
 	  {
 	    /* Option2: the first element is INIT_VAL.  */
-	    auto_vec<tree, 32> elts (nunits);
+	    tree_vector_builder elts (vectype, 1, 2);
 	    elts.quick_push (init_val);
-	    for (i = 1; i < nunits; ++i)
-	      elts.quick_push (def_for_init);
-	    init_def = gimple_build_vector (&stmts, vectype, elts);
+	    elts.quick_push (def_for_init);
+	    init_def = gimple_build_vector (&stmts, &elts);
 	  }
       }
       break;
@@ -4182,7 +4178,7 @@ get_initial_defs_for_reduction (slp_tree
   number_of_copies = nunits * number_of_vectors / group_size;
 
   number_of_places_left_in_vector = nunits;
-  auto_vec<tree, 32> elts (nunits);
+  tree_vector_builder elts (vector_type, nunits, 1);
   elts.quick_grow (nunits);
   for (j = 0; j < number_of_copies; j++)
     {
@@ -4205,12 +4201,14 @@ get_initial_defs_for_reduction (slp_tree
           if (number_of_places_left_in_vector == 0)
             {
 	      gimple_seq ctor_seq = NULL;
-	      tree init = gimple_build_vector (&ctor_seq, vector_type, elts);
+	      tree init = gimple_build_vector (&ctor_seq, &elts);
 	      if (ctor_seq != NULL)
 		gsi_insert_seq_on_edge_immediate (pe, ctor_seq);
 	      voprnds.quick_push (init);
 
               number_of_places_left_in_vector = nunits;
+	      elts.new_vector (vector_type, nunits, 1);
+	      elts.quick_grow (nunits);
             }
         }
     }
@@ -6698,7 +6696,7 @@ vectorizable_induction (gimple *phi,
       unsigned ivn;
       for (ivn = 0; ivn < nivs; ++ivn)
 	{
-	  auto_vec<tree, 32> elts (nunits);
+	  tree_vector_builder elts (vectype, nunits, 1);
 	  stmts = NULL;
 	  for (unsigned eltn = 0; eltn < nunits; ++eltn)
 	    {
@@ -6708,7 +6706,7 @@ vectorizable_induction (gimple *phi,
 				    elt, step_expr);
 	      elts.quick_push (elt);
 	    }
-	  vec_init = gimple_build_vector (&stmts, vectype, elts);
+	  vec_init = gimple_build_vector (&stmts, &elts);
 	  if (stmts)
 	    {
 	      new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
@@ -6815,7 +6813,7 @@ vectorizable_induction (gimple *phi,
       stmts = NULL;
       new_name = gimple_convert (&stmts, TREE_TYPE (vectype), init_expr);
 
-      auto_vec<tree, 32> elts (nunits);
+      tree_vector_builder elts (vectype, nunits, 1);
       elts.quick_push (new_name);
       for (i = 1; i < nunits; i++)
 	{
@@ -6826,7 +6824,7 @@ vectorizable_induction (gimple *phi,
 	}
       /* Create a vector from [new_name_0, new_name_1, ...,
 	 new_name_nunits-1]  */
-      vec_init = gimple_build_vector (&stmts, vectype, elts);
+      vec_init = gimple_build_vector (&stmts, &elts);
       if (stmts)
 	{
 	  new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Make build_vector static
  2017-12-06 15:12         ` Richard Sandiford
                             ` (3 preceding siblings ...)
  2017-12-06 15:25           ` Make gimple_build_vector take a tree_vector_builder Richard Sandiford
@ 2017-12-06 15:26           ` Richard Sandiford
  2017-12-07 11:09             ` Richard Biener
  2017-12-06 15:28           ` Make more use of VECTOR_CST_ENCODED_ELT Richard Sandiford
  2017-12-07 11:05           ` RFC: Variable-length VECTOR_CSTs Richard Biener
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:26 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

After the previous patches, the only remaining uses of build_vector are
in the selftests in tree.c.  This patch makes it static and moves it to
the selftest part of the file.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard


2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* tree.c (build_vector): Delete.
	* tree.h (build_vector): Make static and move into the self-testing
	block.

Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2017-12-06 14:48:52.891162052 +0000
+++ gcc/tree.c	2017-12-06 14:49:10.295445836 +0000
@@ -1736,18 +1736,6 @@ make_vector (unsigned log2_npatterns,
 }
 
 /* Return a new VECTOR_CST node whose type is TYPE and whose values
-   are given by VALS.  */
-
-tree
-build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
-{
-  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
-  tree_vector_builder builder (type, vals.length (), 1);
-  builder.splice (vals);
-  return builder.build ();
-}
-
-/* Return a new VECTOR_CST node whose type is TYPE and whose values
    are extracted from V, a vector of CONSTRUCTOR_ELT.  */
 
 tree
@@ -14066,6 +14054,18 @@ test_labels ()
   ASSERT_FALSE (FORCED_LABEL (label_decl));
 }
 
+/* Return a new VECTOR_CST node whose type is TYPE and whose values
+   are given by VALS.  */
+
+static tree
+build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
+{
+  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
+  tree_vector_builder builder (type, vals.length (), 1);
+  builder.splice (vals);
+  return builder.build ();
+}
+
 /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
 
 static void
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	2017-12-06 14:46:14.133599820 +0000
+++ gcc/tree.h	2017-12-06 14:49:10.296445794 +0000
@@ -4044,7 +4044,6 @@ extern tree build_int_cst (tree, HOST_WI
 extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
 extern tree build_int_cst_type (tree, HOST_WIDE_INT);
 extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
-extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
 extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
 extern tree build_vector_from_val (tree, tree);
 extern void recompute_constructor_flags (tree);

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Make more use of VECTOR_CST_ENCODED_ELT
  2017-12-06 15:12         ` Richard Sandiford
                             ` (4 preceding siblings ...)
  2017-12-06 15:26           ` Make build_vector static Richard Sandiford
@ 2017-12-06 15:28           ` Richard Sandiford
  2017-12-07 11:12             ` Richard Biener
  2017-12-07 11:05           ` RFC: Variable-length VECTOR_CSTs Richard Biener
  6 siblings, 1 reply; 24+ messages in thread
From: Richard Sandiford @ 2017-12-06 15:28 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

This patch makes various bits of code operate directly on the new
VECTOR_CST encoding, instead of using VECTOR_CST_ELT on all elements
of the vector.

Previous patches handled operations that produce a new VECTOR_CST,
while this patch handles things like predicates.  It also makes
print_node dump the encoding instead of the full vector that
the encoding represents.

Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
Also spot-checked on sparc64-linux-gnu.  OK to install?

Thanks,
Richard


2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>

gcc/
	* tree-vector-builder.h
	(tree_vector_builder::binary_encoded_nelts): Declare.
	* tree-vector-builder.c
	(tree_vector_builder::binary_encoded_nelts): New function.
	* fold-const.c (negate_expr_p): Likewise.
	(operand_equal_p, fold_checksum_tree): Likewise.
	* tree-loop-distribution.c (const_with_all_bytes_same): Likewise.
	* tree.c (integer_zerop, integer_onep, integer_all_onesp, real_zerop)
	(real_onep, real_minus_onep, add_expr, initializer_zerop): Likewise.
	(uniform_vector_p): Likewise.
	* varasm.c (const_hash_1, compare_constant): Likewise.
	* tree-ssa-ccp.c: Include tree-vector-builder.h.
	(valid_lattice_transition): Operate directly on the VECTOR_CST
	encoding.
	* ipa-icf.c: Include tree-vector-builder.h.
	(sem_variable::equals): Operate directly on the VECTOR_CST encoding.
	* print-tree.c (print_node): Print encoding of VECTOR_CSTs.

Index: gcc/tree-vector-builder.h
===================================================================
--- gcc/tree-vector-builder.h	2017-12-06 14:49:04.289693414 +0000
+++ gcc/tree-vector-builder.h	2017-12-06 14:50:45.559564436 +0000
@@ -40,6 +40,8 @@ #define GCC_TREE_VECTOR_BUILDER_H
   bool new_unary_operation (tree, tree, bool);
   bool new_binary_operation (tree, tree, tree, bool);
 
+  static unsigned int binary_encoded_nelts (tree, tree);
+
 private:
   bool equal_p (const_tree, const_tree) const;
   bool allow_steps_p () const;
Index: gcc/tree-vector-builder.c
===================================================================
--- gcc/tree-vector-builder.c	2017-12-06 14:49:04.289693414 +0000
+++ gcc/tree-vector-builder.c	2017-12-06 14:50:45.558564477 +0000
@@ -96,6 +96,24 @@ tree_vector_builder::new_binary_operatio
   return true;
 }
 
+/* Return the number of elements that the caller needs to operate on in
+   order to handle a binary operation on VECTOR_CSTs T1 and T2.  This static
+   function is used instead of new_binary_operation if the result of the
+   operation is not a VECTOR_CST.  */
+
+unsigned int
+tree_vector_builder::binary_encoded_nelts (tree t1, tree t2)
+{
+  unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1));
+  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)));
+  /* See new_binary_operation for details.  */
+  unsigned int npatterns = least_common_multiple (VECTOR_CST_NPATTERNS (t1),
+						  VECTOR_CST_NPATTERNS (t2));
+  unsigned int nelts_per_pattern = MAX (VECTOR_CST_NELTS_PER_PATTERN (t1),
+					VECTOR_CST_NELTS_PER_PATTERN (t2));
+  return MIN (npatterns * nelts_per_pattern, nelts);
+}
+
 /* Return a vector element with the value BASE + FACTOR * STEP.  */
 
 tree
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2017-12-06 14:49:00.386854068 +0000
+++ gcc/fold-const.c	2017-12-06 14:50:45.557564518 +0000
@@ -410,10 +410,10 @@ negate_expr_p (tree t)
 	if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
 	  return true;
 
-	int count = VECTOR_CST_NELTS (t), i;
-
-	for (i = 0; i < count; i++)
-	  if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
+	/* Steps don't prevent negation.  */
+	unsigned int count = vector_cst_encoded_nelts (t);
+	for (unsigned int i = 0; i < count; ++i)
+	  if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
 	    return false;
 
 	return true;
@@ -2981,17 +2981,19 @@ operand_equal_p (const_tree arg0, const_
 
       case VECTOR_CST:
 	{
-	  unsigned i;
+	  if (VECTOR_CST_LOG2_NPATTERNS (arg0)
+	      != VECTOR_CST_LOG2_NPATTERNS (arg1))
+	    return 0;
 
-	  if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
+	  if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
+	      != VECTOR_CST_NELTS_PER_PATTERN (arg1))
 	    return 0;
 
-	  for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
-	    {
-	      if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
-				    VECTOR_CST_ELT (arg1, i), flags))
-		return 0;
-	    }
+	  unsigned int count = vector_cst_encoded_nelts (arg0);
+	  for (unsigned int i = 0; i < count; ++i)
+	    if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
+				  VECTOR_CST_ENCODED_ELT (arg1, i), flags))
+	      return 0;
 	  return 1;
 	}
 
@@ -11992,8 +11994,9 @@ fold_checksum_tree (const_tree expr, str
 	  fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
 	  break;
 	case VECTOR_CST:
-	  for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
-	    fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
+	  len = vector_cst_encoded_nelts (expr);
+	  for (i = 0; i < len; ++i)
+	    fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
 	  break;
 	default:
 	  break;
Index: gcc/tree-loop-distribution.c
===================================================================
--- gcc/tree-loop-distribution.c	2017-11-29 11:06:34.810688336 +0000
+++ gcc/tree-loop-distribution.c	2017-12-06 14:50:45.558564477 +0000
@@ -944,13 +944,16 @@ const_with_all_bytes_same (tree val)
 	    return 0;
 	  break;
 	case VECTOR_CST:
-	  unsigned int j;
-	  for (j = 0; j < VECTOR_CST_NELTS (val); ++j)
-	    if (const_with_all_bytes_same (VECTOR_CST_ELT (val, j)))
-	      break;
-	  if (j == VECTOR_CST_NELTS (val))
-	    return 0;
-	  break;
+	  {
+	    unsigned int count = vector_cst_encoded_nelts (val);
+	    unsigned int j;
+	    for (j = 0; j < count; ++j)
+	      if (const_with_all_bytes_same (VECTOR_CST_ENCODED_ELT (val, j)))
+		break;
+	    if (j == count)
+	      return 0;
+	    break;
+	  }
 	default:
 	  break;
 	}
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	2017-12-06 14:49:10.295445836 +0000
+++ gcc/tree.c	2017-12-06 14:50:45.560564396 +0000
@@ -2338,13 +2338,9 @@ integer_zerop (const_tree expr)
       return (integer_zerop (TREE_REALPART (expr))
 	      && integer_zerop (TREE_IMAGPART (expr)));
     case VECTOR_CST:
-      {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	  if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
-	    return false;
-	return true;
-      }
+      return (VECTOR_CST_NPATTERNS (expr) == 1
+	      && VECTOR_CST_DUPLICATE_P (expr)
+	      && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
     default:
       return false;
     }
@@ -2364,13 +2360,9 @@ integer_onep (const_tree expr)
       return (integer_onep (TREE_REALPART (expr))
 	      && integer_zerop (TREE_IMAGPART (expr)));
     case VECTOR_CST:
-      {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	  if (!integer_onep (VECTOR_CST_ELT (expr, i)))
-	    return false;
-	return true;
-      }
+      return (VECTOR_CST_NPATTERNS (expr) == 1
+	      && VECTOR_CST_DUPLICATE_P (expr)
+	      && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
     default:
       return false;
     }
@@ -2401,13 +2393,9 @@ integer_all_onesp (const_tree expr)
     return 1;
 
   else if (TREE_CODE (expr) == VECTOR_CST)
-    {
-      unsigned i;
-      for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
-	  return 0;
-      return 1;
-    }
+    return (VECTOR_CST_NPATTERNS (expr) == 1
+	    && VECTOR_CST_DUPLICATE_P (expr)
+	    && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
 
   else if (TREE_CODE (expr) != INTEGER_CST)
     return 0;
@@ -2630,9 +2618,11 @@ real_zerop (const_tree expr)
 	     && real_zerop (TREE_IMAGPART (expr));
     case VECTOR_CST:
       {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	  if (!real_zerop (VECTOR_CST_ELT (expr, i)))
+	/* Don't simply check for a duplicate because the predicate
+	   accepts both +0.0 and -0.0.  */
+	unsigned count = vector_cst_encoded_nelts (expr);
+	for (unsigned int i = 0; i < count; ++i)
+	  if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
 	    return false;
 	return true;
       }
@@ -2657,13 +2647,9 @@ real_onep (const_tree expr)
       return real_onep (TREE_REALPART (expr))
 	     && real_zerop (TREE_IMAGPART (expr));
     case VECTOR_CST:
-      {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	  if (!real_onep (VECTOR_CST_ELT (expr, i)))
-	    return false;
-	return true;
-      }
+      return (VECTOR_CST_NPATTERNS (expr) == 1
+	      && VECTOR_CST_DUPLICATE_P (expr)
+	      && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
     default:
       return false;
     }
@@ -2684,13 +2670,9 @@ real_minus_onep (const_tree expr)
       return real_minus_onep (TREE_REALPART (expr))
 	     && real_zerop (TREE_IMAGPART (expr));
     case VECTOR_CST:
-      {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
-	  if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
-	    return false;
-	return true;
-      }
+      return (VECTOR_CST_NPATTERNS (expr) == 1
+	      && VECTOR_CST_DUPLICATE_P (expr)
+	      && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
     default:
       return false;
     }
@@ -7102,9 +7084,11 @@ add_expr (const_tree t, inchash::hash &h
       return;
     case VECTOR_CST:
       {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
-	  inchash::add_expr (VECTOR_CST_ELT (t, i), hstate, flags);
+	hstate.add_int (VECTOR_CST_NPATTERNS (t));
+	hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
+	unsigned int count = vector_cst_encoded_nelts (t);
+	for (unsigned int i = 0; i < count; ++i)
+	  inchash::add_expr (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
 	return;
       }
     case SSA_NAME:
@@ -10431,13 +10415,9 @@ initializer_zerop (const_tree init)
 	    && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
 
     case VECTOR_CST:
-      {
-	unsigned i;
-	for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
-	  if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
-	    return false;
-	return true;
-      }
+      return (VECTOR_CST_NPATTERNS (init) == 1
+	      && VECTOR_CST_DUPLICATE_P (init)
+	      && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)));
 
     case CONSTRUCTOR:
       {
@@ -10486,12 +10466,9 @@ uniform_vector_p (const_tree vec)
 
   if (TREE_CODE (vec) == VECTOR_CST)
     {
-      first = VECTOR_CST_ELT (vec, 0);
-      for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
-	if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
-	  return NULL_TREE;
-
-      return first;
+      if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
+	return VECTOR_CST_ENCODED_ELT (vec, 0);
+      return NULL_TREE;
     }
 
   else if (TREE_CODE (vec) == CONSTRUCTOR)
Index: gcc/varasm.c
===================================================================
--- gcc/varasm.c	2017-12-05 14:24:56.163946903 +0000
+++ gcc/varasm.c	2017-12-06 14:50:45.560564396 +0000
@@ -3007,13 +3007,11 @@ const_hash_1 (const tree exp)
 
     case VECTOR_CST:
       {
-	unsigned i;
-
-	hi = 7 + VECTOR_CST_NELTS (exp);
-
-	for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
-	  hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
-
+	hi = 7 + VECTOR_CST_NPATTERNS (exp);
+	hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
+	unsigned int count = vector_cst_encoded_nelts (exp);
+	for (unsigned int i = 0; i < count; ++i)
+	  hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
 	return hi;
       }
 
@@ -3151,14 +3149,18 @@ compare_constant (const tree t1, const t
 
     case VECTOR_CST:
       {
-	unsigned i;
+	if (VECTOR_CST_NPATTERNS (t1)
+	    != VECTOR_CST_NPATTERNS (t2))
+	  return 0;
 
-        if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
+	if (VECTOR_CST_NELTS_PER_PATTERN (t1)
+	    != VECTOR_CST_NELTS_PER_PATTERN (t2))
 	  return 0;
 
-	for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
-	  if (!compare_constant (VECTOR_CST_ELT (t1, i),
-				 VECTOR_CST_ELT (t2, i)))
+	unsigned int count = vector_cst_encoded_nelts (t1);
+	for (unsigned int i = 0; i < count; ++i)
+	  if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
+				 VECTOR_CST_ENCODED_ELT (t2, i)))
 	    return 0;
 
 	return 1;
Index: gcc/tree-ssa-ccp.c
===================================================================
--- gcc/tree-ssa-ccp.c	2017-12-05 14:24:55.073967105 +0000
+++ gcc/tree-ssa-ccp.c	2017-12-06 14:50:45.558564477 +0000
@@ -147,6 +147,7 @@ Free Software Foundation; either version
 #include "diagnostic-core.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "tree-vector-builder.h"
 
 /* Possible lattice values.  */
 typedef enum
@@ -465,11 +466,14 @@ valid_lattice_transition (ccp_prop_value
   else if (VECTOR_FLOAT_TYPE_P (type)
 	   && !HONOR_NANS (type))
     {
-      for (unsigned i = 0; i < VECTOR_CST_NELTS (old_val.value); ++i)
+      unsigned int count
+	= tree_vector_builder::binary_encoded_nelts (old_val.value,
+						     new_val.value);
+      for (unsigned int i = 0; i < count; ++i)
 	if (!REAL_VALUE_ISNAN
-	       (TREE_REAL_CST (VECTOR_CST_ELT (old_val.value, i)))
-	    && !operand_equal_p (VECTOR_CST_ELT (old_val.value, i),
-				 VECTOR_CST_ELT (new_val.value, i), 0))
+	       (TREE_REAL_CST (VECTOR_CST_ENCODED_ELT (old_val.value, i)))
+	    && !operand_equal_p (VECTOR_CST_ENCODED_ELT (old_val.value, i),
+				 VECTOR_CST_ENCODED_ELT (new_val.value, i), 0))
 	  return false;
       return true;
     }
Index: gcc/ipa-icf.c
===================================================================
--- gcc/ipa-icf.c	2017-11-29 11:06:34.810688336 +0000
+++ gcc/ipa-icf.c	2017-12-06 14:50:45.557564518 +0000
@@ -83,6 +83,7 @@ #define INCLUDE_LIST
 #include "ipa-icf.h"
 #include "stor-layout.h"
 #include "dbgcnt.h"
+#include "tree-vector-builder.h"
 
 using namespace ipa_icf_gimple;
 
@@ -2024,17 +2025,17 @@ sem_variable::equals (tree t1, tree t2)
 						&TREE_REAL_CST (t2)));
     case VECTOR_CST:
       {
-	unsigned i;
-
         if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
           return return_false_with_msg ("VECTOR_CST nelts mismatch");
 
-	for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
-	  if (!sem_variable::equals (VECTOR_CST_ELT (t1, i),
-				     VECTOR_CST_ELT (t2, i)))
-	    return 0;
+	unsigned int count
+	  = tree_vector_builder::binary_encoded_nelts (t1, t2);
+	for (unsigned int i = 0; i < count; ++i)
+	  if (!sem_variable::equals (VECTOR_CST_ENCODED_ELT (t1, i),
+				     VECTOR_CST_ENCODED_ELT (t2, i)))
+	    return false;
 
-	return 1;
+	return true;
       }
     case ARRAY_REF:
     case ARRAY_RANGE_REF:
Index: gcc/print-tree.c
===================================================================
--- gcc/print-tree.c	2017-11-29 11:06:34.810688336 +0000
+++ gcc/print-tree.c	2017-12-06 14:50:45.558564477 +0000
@@ -761,24 +761,18 @@ print_node (FILE *file, const char *pref
 
 	case VECTOR_CST:
 	  {
-	    /* Big enough for 2 UINT_MAX plus the string below.  */
+	    /* Big enough for UINT_MAX plus the string below.  */
 	    char buf[32];
-	    unsigned i;
 
-	    for (i = 0; i < VECTOR_CST_NELTS (node); ++i)
+	    fprintf (file, " npatterns:%u nelts-per-pattern:%u",
+		     VECTOR_CST_NPATTERNS (node),
+		     VECTOR_CST_NELTS_PER_PATTERN (node));
+	    unsigned int count = vector_cst_encoded_nelts (node);
+	    for (unsigned int i = 0; i < count; ++i)
 	      {
-		unsigned j;
-		/* Coalesce the output of identical consecutive elements.  */
-		for (j = i + 1; j < VECTOR_CST_NELTS (node); j++)
-		  if (VECTOR_CST_ELT (node, j) != VECTOR_CST_ELT (node, i))
-		    break;
-		j--;
-		if (i == j)
-		  sprintf (buf, "elt:%u: ", i);
-		else
-		  sprintf (buf, "elt:%u...%u: ", i, j);
-		print_node (file, buf, VECTOR_CST_ELT (node, i), indent + 4);
-		i = j;
+		sprintf (buf, "elt:%u: ", i);
+		print_node (file, buf, VECTOR_CST_ENCODED_ELT (node, i),
+			    indent + 4);
 	      }
 	  }
 	  break;

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: RFC: Variable-length VECTOR_CSTs
  2017-12-06 15:12         ` Richard Sandiford
                             ` (5 preceding siblings ...)
  2017-12-06 15:28           ` Make more use of VECTOR_CST_ENCODED_ELT Richard Sandiford
@ 2017-12-07 11:05           ` Richard Biener
  6 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:05 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:11 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> Richard Biener <richard.guenther@gmail.com> writes:
>> On Thu, Nov 30, 2017 at 2:18 PM, Richard Sandiford
>> <richard.sandiford@linaro.org> wrote:
>>> Richard Sandiford <richard.sandiford@linaro.org> writes:
>>>> Richard Biener <richard.guenther@gmail.com> writes:
>>>>> On Wed, Nov 29, 2017 at 12:57 PM, Richard Sandiford
>>>>> <richard.sandiford@linaro.org> wrote:
>>>>>> It was clear from the SVE reviews that people were unhappy with how
>>>>>> "special" the variable-length case was.  One particular concern was
>>>>>> the use of VEC_DUPLICATE_CST and VEC_SERIES_CST, and the way that
>>>>>> that would in turn lead to different representations of VEC_PERM_EXPRs
>>>>>> with constant permute vectors, and difficulties in invoking
>>>>>> vec_perm_const_ok.
>>>>>>
>>>>>> This is an RFC for a VECTOR_CST representation that treats each
>>>>>> specific constant as one instance of an arbitrary-length sequence.
>>>>>> The reprensentation then extends to variable-length vectors in a
>>>>>> natural way.
>>>>>>
>>>>>> As discussed on IRC, if a vector contains X*N elements for some
>>>>>> constant N and integer X>0, the main features we need are:
>>>>>>
>>>>>> 1) the ability to represent a sequence that duplicates N values
>>>>>>
>>>>>>    This is useful for SLP invariants.
>>>>>>
>>>>>> 2) the ability to represent a sequence that starts with N values and
>>>>>>    is followed by zeros
>>>>>>
>>>>>>    This is useful for the initial value in a double or SLP reduction
>>>>>>
>>>>>> 3) the ability to represent N interleaved series
>>>>>>
>>>>>>    This is useful for SLP inductions and for VEC_PERM_EXPRs.
>>>>>>
>>>>>> For (2), zero isn't necessarily special, since vectors used in an AND
>>>>>> reduction might need to fill with ones.  Also, we might need up to N
>>>>>> different fill values with mixed SLP operations; it isn't necessarily
>>>>>> safe to assume that a single fill value will always be enough.
>>>>>>
>>>>>> The same goes for (3): there's no reason in principle why the
>>>>>> steps in an SLP induction should all be the same (although they
>>>>>> do need to be at the moment).  E.g. once we support SLP on:
>>>>>>
>>>>>>   for (unsigned int i = 0; i < n; i += 2)
>>>>>>     {
>>>>>>       x[i] += 4 + i;
>>>>>>       x[i + 1] += 11 + i * 3;
>>>>>>     }
>>>>>>
>>>>>> we'll need {[4, 14], +, [2, 6]}.
>>>>>>
>>>>>> So the idea is to represent vectors as P interleaved patterns of the form:
>>>>>>
>>>>>>   [BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, ...]
>>>>>>
>>>>>> where the STEP is always zero (actually null) for non-integer vectors.
>>>>>> This is effectively projecting a "foreground" value of P elements
>>>>>> onto an arbitrary-length "background" sequenece, where the background
>>>>>> sequence contains P parallel linear series.
>>>>>>
>>>>>> E.g. to pick an extreme and unlikely example,
>>>>>>
>>>>>>   [42, 99, 2, 20, 3, 30, 4, 40, ...]
>>>>>>
>>>>>> has 2 patterns:
>>>>>>
>>>>>>   BASE0 = 42, BASE1 = 2, STEP = 1
>>>>>>   BASE0 = 99, BASE1 = 20, STEP = 10
>>>>>>
>>>>>> The more useful cases are degenerate versions of this general case.
>>>>>>
>>>>>> As far as memory consumption goes: the number of patterns needed for a
>>>>>> fixed-length vector with 2*N elements is always at most N; in the worst
>>>>>> case, we simply interleave the first N elements with the second N elements.
>>>>>> The worst-case increase in footprint is therefore N trees for the steps.
>>>>>> In practice the footprint is usually smaller than it was before, since
>>>>>> most constants do have a pattern.
>>>>>>
>>>>>> The patch below implements this for trees.  I have patches to use the
>>>>>> same style of encoding for CONST_VECTOR and vec_perm_indices, but the
>>>>>> tree one is probably easiest to read.
>>>>>>
>>>>>> The patch only adds the representation.  Follow-on patches make more
>>>>>> use of it (and usually make things simpler; e.g. integer_zerop is no
>>>>>> longer a looping operation).
>>>>>>
>>>>>> Does this look better?
>>>>>
>>>>> Yes, the overall design looks good.  I wonder why you chose to have
>>>>> the number of patterns being a power of two?  I suppose this is
>>>>> to have the same number of elements from all patterns in the final
>>>>> vector (which is power-of-two sized)?
>>>>
>>>> Right.  The rtl and vec_perm_indices parts don't have this restriction,
>>>> since some ports do define non-power-of-2 vectors for internal use.
>>>> The problem is that, since VECTOR_CSTs are used by the FE, we need
>>>> to support all valid vector lengths without blowing the 16-bit field.
>>>> Using the same style of representation as TYPE_VECTOR_SUBPARTS seemed
>>>> like the safest way of doing that.
>>>>
>>>>> I wonder if there exists a vector where say a three-pattern
>>>>> interleaving would be smaller than a four-pattern one?
>>>>
>>>> Only in the non-power-of-2 case.
>>>>
>>>>> Given you add flags for various purposes would it make sense to
>>>>> overload 'step' with a regular element to avoid the storage increase
>>>>> in case step is unnecessary?  This makes it have three elements
>>>>> which is of course awkward :/
>>>>
>>>> I wondered about keeping it as an array of trees and tacking the
>>>> steps onto the end as an optional addition.  But the idea is that
>>>> tree_vector_pattern becomes the preferred way of handling constant
>>>> vectors, if it can be used, so it seemed neater to use in the tree
>>>> node too.
>>>
>>> In the end it seemed better to encode the first NPATTERNS * N
>>> elements of the vector, where:
>>>
>>> - N==3 if at least one pattern needs a step; otherwise
>>> - N==2 if at least one pattern has different BASE0s and BASE1s; otherwise
>>> - N==1 (i.e. the vector is a repeated sequence of NPATTERNS elements)
>>>
>>> So effectively all we're doing for the constant-length case is
>>> reducing the number of elements that need to be stored and processed
>>> (once code is converted to use the new routines).
>>>
>>> The patch below does this.  It also introduces a new class
>>> vector_builder<T> for building vectors of Ts, with a derived
>>> tree_vector_builder specifically for trees.  This allows the
>>> compression code to be shared between representations and also
>>> avoids hard-coding the auto_vec<> count everywhere.
>>>
>>> I've also fixed the selftests to get the actual and expected values
>>> the right way round (thanks David for noticing that).
>>>
>>> What do you think?  Does this look better?
>>
>> Yes!  This is ok for trunk.
>
> Thanks :-)  I've since tidied up the documentation and vector-builder.h
> stuff and tested the patch properly with the rest of the SVE series
> (which seems to work well with it).  The encoding is the same as before.
>
> Sorry for the delay in getting the update out, but I wanted to be
> certain that no further changes were needed.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu
> against current trunk.  Still OK?

Ok.

Thanks,
Richard.

> Richard
>
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@arm.com>
>
> gcc/
>         * doc/generic.texi (VECTOR_CST): Describe new representation of
>         vector constants.
>         * vector-builder.h: New file.
>         * tree-vector-builder.h: Likewise.
>         * tree-vector-builder.c: Likewise.
>         * Makefile.in (OBJS): Add tree-vector-builder.o.
>         * tree.def (VECTOR_CST): Update comment to refer to generic.texi.
>         * tree-core.h (tree_base): Add a vector_cst field to the u union.
>         (tree_vector): Change the number of elements to
>         vector_cst_encoded_nelts.
>         * tree.h (VECTOR_CST_NELTS): Redefine using TYPE_VECTOR_SUBPARTS.
>         (VECTOR_CST_ELTS): Delete.
>         (VECTOR_CST_ELT): Redefine using vector_cst_elt.
>         (VECTOR_CST_LOG2_NPATTERNS, VECTOR_CST_NPATTERNS): New macros.
>         (VECTOR_CST_NELTS_PER_PATTERN, VECTOR_CST_DUPLICATE_P): Likewise.
>         (VECTOR_CST_STEPPED_P, VECTOR_CST_ENCODED_ELTS): Likewise.
>         (VECTOR_CST_ENCODED_ELT): Likewise.
>         (vector_cst_encoded_nelts): New function.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
>         VECTOR_CST_NELTS_PER_PATTERN as arguments.
>         (vector_cst_int_elt, vector_cst_elt): Declare.
>         * tree.c: Include tree-vector-builder.h.
>         (tree_code_size): Abort if passed VECTOR_CST.
>         (tree_size): Update for new VECTOR_CST layout.
>         (make_vector): Take the values of VECTOR_CST_LOG2_NPATTERNS and
>         VECTOR_CST_NELTS_PER_PATTERN as arguments.
>         (build_vector): Use tree_vector_builder.
>         (vector_cst_int_elt, vector_cst_elt): New functions.
>         (drop_tree_overflow): For VECTOR_CST, drop the TREE_OVERFLOW from the
>         encoded elements and then create the vector in the canonical form.
>         (check_vector_cst, check_vector_cst_duplicate, check_vector_cst_fill)
>         (check_vector_cst_stepped, test_vector_cst_patterns): New functions.
>         (tree_c_tests): Call test_vector_cst_patterns.
>         * lto-streamer-out.c (DFS::DFS_write_tree_body): Handle the new
>         VECTOR_CST fields.
>         (hash_tree): Likewise.
>         * tree-streamer-out.c (write_ts_vector_tree_pointers): Likewise.
>         (streamer_write_tree_header): Likewise.
>         * tree-streamer-in.c (lto_input_ts_vector_tree_pointers): Likewise.
>         (streamer_alloc_tree): Likewise.  Update call to make_vector.
>         * fold-const.c (fold_ternary_loc): Avoid using VECTOR_CST_ELTS.
>
> gcc/lto/
>         * lto.c (compare_tree_sccs_1): Compare the new VECTOR_CST flags.
>
> Index: gcc/doc/generic.texi
> ===================================================================
> --- gcc/doc/generic.texi        2017-12-06 14:46:13.860611208 +0000
> +++ gcc/doc/generic.texi        2017-12-06 14:46:14.128600028 +0000
> @@ -1084,10 +1084,77 @@ These nodes are used to represent comple
>  imaginary parts respectively.
>
>  @item VECTOR_CST
> -These nodes are used to represent vector constants, whose parts are
> -constant nodes.  Each individual constant node is either an integer or a
> -double constant node.  The first operand is a @code{TREE_LIST} of the
> -constant nodes and is accessed through @code{TREE_VECTOR_CST_ELTS}.
> +These nodes are used to represent vector constants.  Each vector
> +constant @var{v} is treated as a specific instance of an arbitrary-length
> +sequence that itself contains @samp{VECTOR_CST_NPATTERNS (@var{v})}
> +interleaved patterns.  Each pattern has the form:
> +
> +@smallexample
> +@{ @var{base0}, @var{base1}, @var{base1} + @var{step}, @var{base1} + @var{step} * 2, @dots{} @}
> +@end smallexample
> +
> +The first three elements in each pattern are enough to determine the
> +values of the other elements.  However, if all @var{step}s are zero,
> +only the first two elements are needed.  If in addition each @var{base1}
> +is equal to the corresponding @var{base0}, only the first element in
> +each pattern is needed.  The number of encoded elements per pattern
> +is given by @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v})}.
> +
> +For example, the constant:
> +
> +@smallexample
> +@{ 0, 1, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18 @}
> +@end smallexample
> +
> +is interpreted as an interleaving of the sequences:
> +
> +@smallexample
> +@{ 0, 2, 3, 4, 5, 6, 7, 8 @}
> +@{ 1, 6, 8, 10, 12, 14, 16, 18 @}
> +@end smallexample
> +
> +where the sequences are represented by the following patterns:
> +
> +@smallexample
> +@var{base0} == 0, @var{base1} == 2, @var{step} == 1
> +@var{base0} == 1, @var{base1} == 6, @var{step} == 2
> +@end smallexample
> +
> +In this case:
> +
> +@smallexample
> +VECTOR_CST_NPATTERNS (@var{v}) == 2
> +VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3
> +@end smallexample
> +
> +The vector is therefore encoded using the first 6 elements
> +(@samp{@{ 0, 1, 2, 6, 3, 8 @}}), with the remaining 10 elements
> +being implicit extensions of them.
> +
> +Sometimes this scheme can create two possible encodings of the same
> +vector.  For example @{ 0, 1 @} could be seen as two patterns with
> +one element each or one pattern with two elements (@var{base0} and
> +@var{base1}).  The canonical encoding is always the one with the
> +fewest patterns or (if both encodings have the same number of
> +petterns) the one with the fewest encoded elements.
> +
> +@samp{vector_cst_encoding_nelts (@var{v})} gives the total number of
> +encoded elements in @var{v}, which is 6 in the example above.
> +@code{VECTOR_CST_ENCODED_ELTS (@var{v})} gives a pointer to the elements
> +encoded in @var{v} and @code{VECTOR_CST_ENCODED_ELT (@var{v}, @var{i})}
> +accesses the value of encoded element @var{i}.
> +
> +@samp{VECTOR_CST_DUPLICATE_P (@var{v})} is true if @var{v} simply contains
> +repeated instances of @samp{VECTOR_CST_NPATTERNS (@var{v})} values.  This is
> +a shorthand for testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 1}.
> +
> +@samp{VECTOR_CST_STEPPED_P (@var{v})} is true if at least one
> +pattern in @var{v} has a nonzero step.  This is a shorthand for
> +testing @samp{VECTOR_CST_NELTS_PER_PATTERN (@var{v}) == 3}.
> +
> +The utility function @code{vector_cst_elt} gives the value of an
> +arbitrary index as a @code{tree}.  @code{vector_cst_int_elt} gives
> +the same value as a @code{wide_int}.
>
>  @item STRING_CST
>  These nodes represent string-constants.  The @code{TREE_STRING_LENGTH}
> Index: gcc/vector-builder.h
> ===================================================================
> --- /dev/null   2017-12-05 14:21:55.753572108 +0000
> +++ gcc/vector-builder.h        2017-12-06 14:46:14.133599820 +0000
> @@ -0,0 +1,394 @@
> +/* A class for building vector constant patterns.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GCC_VECTOR_BUILDER_H
> +#define GCC_VECTOR_BUILDER_H
> +
> +/* This class is a wrapper around auto_vec<T> for building vectors of T.
> +   It aims to encode each vector as npatterns interleaved patterns,
> +   where each pattern represents a sequence:
> +
> +     { BASE0, BASE1, BASE1 + STEP, BASE1 + STEP*2, BASE1 + STEP*3, ... }
> +
> +   The first three elements in each pattern provide enough information
> +   to derive the other elements.  If all patterns have a STEP of zero,
> +   we only need to encode the first two elements in each pattern.
> +   If BASE1 is also equal to BASE0 for all patterns, we only need to
> +   encode the first element in each pattern.  The number of encoded
> +   elements per pattern is given by nelts_per_pattern.
> +
> +   The class can be used in two ways:
> +
> +   1. It can be used to build a full image of the vector, which is then
> +      canonicalized by finalize ().  In this case npatterns is initially
> +      the number of elements in the vector and nelts_per_pattern is
> +      initially 1.
> +
> +   2. It can be used to build a vector that already has a known encoding.
> +      This is preferred since it is more efficient and copes with
> +      variable-length vectors.  finalize () then canonicalizes the encoding
> +      to a simpler form if possible.
> +
> +   The derived class Derived provides this functionality for specific Ts.
> +   Derived needs to provide the following interface:
> +
> +      bool equal_p (T elt1, T elt2) const;
> +
> +         Return true if elements ELT1 and ELT2 are equal.
> +
> +      bool allow_steps_p () const;
> +
> +         Return true if a stepped representation is OK.  We don't allow
> +         linear series for anything other than integers, to avoid problems
> +         with rounding.
> +
> +      bool integral_p (T elt) const;
> +
> +         Return true if element ELT can be interpreted as an integer.
> +
> +      StepType step (T elt1, T elt2) const;
> +
> +         Return the value of element ELT2 minus the value of element ELT1,
> +         given integral_p (ELT1) && integral_p (ELT2).  There is no fixed
> +         choice of StepType.
> +
> +      bool can_elide_p (T elt) const;
> +
> +         Return true if we can drop element ELT, even if the retained
> +         elements are different.  This is provided for TREE_OVERFLOW
> +         handling.
> +
> +      void note_representative (T *elt1_ptr, T elt2);
> +
> +         Record that ELT2 is being elided, given that ELT1_PTR points to
> +         the last encoded element for the containing pattern.  This is
> +         again provided for TREE_OVERFLOW handling.  */
> +
> +template<typename T, typename Derived>
> +class vector_builder : public auto_vec<T, 32>
> +{
> +public:
> +  vector_builder ();
> +
> +  unsigned int full_nelts () const { return m_full_nelts; }
> +  unsigned int npatterns () const { return m_npatterns; }
> +  unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
> +  unsigned int encoded_nelts () const;
> +  bool encoded_full_vector_p () const;
> +
> +  void finalize ();
> +
> +protected:
> +  void new_vector (unsigned int, unsigned int, unsigned int);
> +  void reshape (unsigned int, unsigned int);
> +  bool repeating_sequence_p (unsigned int, unsigned int, unsigned int);
> +  bool stepped_sequence_p (unsigned int, unsigned int, unsigned int);
> +  bool try_npatterns (unsigned int);
> +
> +private:
> +  vector_builder (const vector_builder &);
> +  vector_builder &operator= (const vector_builder &);
> +  Derived *derived () { return static_cast<Derived *> (this); }
> +  const Derived *derived () const;
> +
> +  unsigned int m_full_nelts;
> +  unsigned int m_npatterns;
> +  unsigned int m_nelts_per_pattern;
> +};
> +
> +template<typename T, typename Derived>
> +inline const Derived *
> +vector_builder<T, Derived>::derived () const
> +{
> +  return static_cast<const Derived *> (this);
> +}
> +
> +template<typename T, typename Derived>
> +inline
> +vector_builder<T, Derived>::vector_builder ()
> +  : m_full_nelts (0),
> +    m_npatterns (0),
> +    m_nelts_per_pattern (0)
> +{}
> +
> +/* Return the number of elements that are explicitly encoded.  The vec
> +   starts with these explicitly-encoded elements and may contain additional
> +   elided elements.  */
> +
> +template<typename T, typename Derived>
> +inline unsigned int
> +vector_builder<T, Derived>::encoded_nelts () const
> +{
> +  return m_npatterns * m_nelts_per_pattern;
> +}
> +
> +/* Return true if every element of the vector is explicitly encoded.  */
> +
> +template<typename T, typename Derived>
> +inline bool
> +vector_builder<T, Derived>::encoded_full_vector_p () const
> +{
> +  return m_npatterns * m_nelts_per_pattern == m_full_nelts;
> +}
> +
> +/* Start building a vector that has FULL_NELTS elements.  Initially
> +   encode it using NPATTERNS patterns with NELTS_PER_PATTERN each.  */
> +
> +template<typename T, typename Derived>
> +void
> +vector_builder<T, Derived>::new_vector (unsigned int full_nelts,
> +                                       unsigned int npatterns,
> +                                       unsigned int nelts_per_pattern)
> +{
> +  m_full_nelts = full_nelts;
> +  m_npatterns = npatterns;
> +  m_nelts_per_pattern = nelts_per_pattern;
> +  this->reserve (encoded_nelts ());
> +  this->truncate (0);
> +}
> +
> +/* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
> +   but without changing the underlying vector.  */
> +
> +template<typename T, typename Derived>
> +void
> +vector_builder<T, Derived>::reshape (unsigned int npatterns,
> +                                    unsigned int nelts_per_pattern)
> +{
> +  unsigned int old_encoded_nelts = encoded_nelts ();
> +  unsigned int new_encoded_nelts = npatterns * nelts_per_pattern;
> +  gcc_checking_assert (new_encoded_nelts <= old_encoded_nelts);
> +  unsigned int next = new_encoded_nelts - npatterns;
> +  for (unsigned int i = new_encoded_nelts; i < old_encoded_nelts; ++i)
> +    {
> +      derived ()->note_representative (&(*this)[next], (*this)[i]);
> +      next += 1;
> +      if (next == new_encoded_nelts)
> +       next -= npatterns;
> +    }
> +  m_npatterns = npatterns;
> +  m_nelts_per_pattern = nelts_per_pattern;
> +}
> +
> +/* Return true if elements [START, END) contain a repeating sequence of
> +   STEP elements.  */
> +
> +template<typename T, typename Derived>
> +bool
> +vector_builder<T, Derived>::repeating_sequence_p (unsigned int start,
> +                                                 unsigned int end,
> +                                                 unsigned int step)
> +{
> +  for (unsigned int i = start; i < end - step; ++i)
> +    if (!derived ()->equal_p ((*this)[i], (*this)[i + step]))
> +      return false;
> +  return true;
> +}
> +
> +/* Return true if elements [START, END) contain STEP interleaved linear
> +   series.  */
> +
> +template<typename T, typename Derived>
> +bool
> +vector_builder<T, Derived>::stepped_sequence_p (unsigned int start,
> +                                               unsigned int end,
> +                                               unsigned int step)
> +{
> +  if (!derived ()->allow_steps_p ())
> +    return false;
> +
> +  for (unsigned int i = start + step * 2; i < end; ++i)
> +    {
> +      T elt1 = (*this)[i - step * 2];
> +      T elt2 = (*this)[i - step];
> +      T elt3 = (*this)[i];
> +
> +      if (!derived ()->integral_p (elt1)
> +         || !derived ()->integral_p (elt2)
> +         || !derived ()->integral_p (elt3))
> +       return false;
> +
> +      if (derived ()->step (elt1, elt2) != derived ()->step (elt2, elt3))
> +       return false;
> +
> +      if (!derived ()->can_elide_p (elt3))
> +       return false;
> +    }
> +  return true;
> +}
> +
> +/* Try to change the number of encoded patterns to NPATTERNS, returning
> +   true on success.  */
> +
> +template<typename T, typename Derived>
> +bool
> +vector_builder<T, Derived>::try_npatterns (unsigned int npatterns)
> +{
> +  if (m_nelts_per_pattern == 1)
> +    {
> +      /* See whether NPATTERNS is valid with the current 1-element-per-pattern
> +        encoding.  */
> +      if (repeating_sequence_p (0, encoded_nelts (), npatterns))
> +       {
> +         reshape (npatterns, 1);
> +         return true;
> +       }
> +
> +      /* We can only increase the number of elements per pattern if all
> +        elements are still encoded explicitly.  */
> +      if (!encoded_full_vector_p ())
> +       return false;
> +    }
> +
> +  if (m_nelts_per_pattern <= 2)
> +    {
> +      /* See whether NPATTERNS is valid with a 2-element-per-pattern
> +        encoding.  */
> +      if (repeating_sequence_p (npatterns, encoded_nelts (), npatterns))
> +       {
> +         reshape (npatterns, 2);
> +         return true;
> +       }
> +
> +      /* We can only increase the number of elements per pattern if all
> +        elements are still encoded explicitly.  */
> +      if (!encoded_full_vector_p ())
> +       return false;
> +    }
> +
> +  if (m_nelts_per_pattern <= 3)
> +    {
> +      /* See whether we have NPATTERNS interleaved linear series,
> +        giving a 3-element-per-pattern encoding.  */
> +      if (stepped_sequence_p (npatterns, encoded_nelts (), npatterns))
> +       {
> +         reshape (npatterns, 3);
> +         return true;
> +       }
> +      return false;
> +    }
> +
> +  gcc_unreachable ();
> +}
> +
> +/* Replace the current encoding with the canonical form.  */
> +
> +template<typename T, typename Derived>
> +void
> +vector_builder<T, Derived>::finalize ()
> +{
> +  /* The encoding requires the same number of elements to come from each
> +     pattern.  */
> +  gcc_assert (m_full_nelts % m_npatterns == 0);
> +
> +  /* Allow the caller to build more elements than necessary.  For example,
> +     it's often convenient to build a stepped vector from the natural
> +     encoding of three elements even if the vector itself only has two.  */
> +  if (m_full_nelts <= encoded_nelts ())
> +    {
> +      m_npatterns = m_full_nelts;
> +      m_nelts_per_pattern = 1;
> +    }
> +
> +  /* Try to whittle down the number of elements per pattern.  That is:
> +
> +     1. If we have stepped patterns whose steps are all 0, reduce the
> +        number of elements per pattern from 3 to 2.
> +
> +     2. If we have background fill values that are the same as the
> +        foreground values, reduce the number of elements per pattern
> +        from 2 to 1.  */
> +  while (m_nelts_per_pattern > 1
> +        && repeating_sequence_p (encoded_nelts () - m_npatterns * 2,
> +                                 encoded_nelts (), m_npatterns))
> +    /* The last two sequences of M_NPATTERNS elements are equal,
> +       so remove the last one.  */
> +    reshape (m_npatterns, m_nelts_per_pattern - 1);
> +
> +  if (pow2p_hwi (m_npatterns))
> +    {
> +      /* Try to halve the number of patterns while doing so gives a
> +        valid pattern.  This approach is linear in the number of
> +        elements, whereas searcing from 1 up would be O(n*log(n)).
> +
> +        Each halving step tries to keep the number of elements per pattern
> +        the same.  If that isn't possible, and if all elements are still
> +        explicitly encoded, the halving step can instead increase the number
> +        of elements per pattern.
> +
> +        E.g. for:
> +
> +            { 0, 2, 3, 4, 5, 6, 7, 8 }  npatterns == 8  full_nelts == 8
> +
> +        we first realize that the second half of the sequence is not
> +        equal to the first, so we cannot maintain 1 element per pattern
> +        for npatterns == 4.  Instead we halve the number of patterns
> +        and double the number of elements per pattern, treating this
> +        as a "foreground" { 0, 2, 3, 4 } against a "background" of
> +        { 5, 6, 7, 8 | 5, 6, 7, 8 ... }:
> +
> +            { 0, 2, 3, 4 | 5, 6, 7, 8 }  npatterns == 4
> +
> +        Next we realize that this is *not* a foreround of { 0, 2 }
> +        against a background of { 3, 4 | 3, 4 ... }, so the only
> +        remaining option for reducing the number of patterns is
> +        to use a foreground of { 0, 2 } against a stepped background
> +        of { 1, 2 | 3, 4 | 5, 6 ... }.  This is valid because we still
> +        haven't elided any elements:
> +
> +            { 0, 2 | 3, 4 | 5, 6 }  npatterns == 2
> +
> +        This in turn can be reduced to a foreground of { 0 } against a
> +        stepped background of { 1 | 2 | 3 ... }:
> +
> +            { 0 | 2 | 3 }  npatterns == 1
> +
> +        This last step would not have been possible for:
> +
> +            { 0, 0 | 3, 4 | 5, 6 }  npatterns == 2.  */
> +      while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
> +       continue;
> +
> +      /* Builders of arbitrary fixed-length vectors can use:
> +
> +            new_vector (x, x, 1)
> +
> +        so that every element is specified explicitly.  Handle cases
> +        that are actually wrapping series, like { 0, 1, 2, 3, 0, 1, 2, 3 }
> +        would be for 2-bit elements.  We'll have treated them as
> +        duplicates in the loop above.  */
> +      if (m_nelts_per_pattern == 1
> +         && this->length () >= m_full_nelts
> +         && (m_npatterns & 3) == 0
> +         && stepped_sequence_p (m_npatterns / 4, m_full_nelts,
> +                                m_npatterns / 4))
> +       {
> +         reshape (m_npatterns / 4, 3);
> +         while ((m_npatterns & 1) == 0 && try_npatterns (m_npatterns / 2))
> +           continue;
> +       }
> +    }
> +  else
> +    /* For the non-power-of-2 case, do a simple search up from 1.  */
> +    for (unsigned int i = 1; i <= m_npatterns / 2; ++i)
> +      if (m_npatterns % i == 0 && try_npatterns (i))
> +       break;
> +}
> +
> +#endif
> Index: gcc/tree-vector-builder.h
> ===================================================================
> --- /dev/null   2017-12-05 14:21:55.753572108 +0000
> +++ gcc/tree-vector-builder.h   2017-12-06 14:46:14.131599903 +0000
> @@ -0,0 +1,135 @@
> +/* A class for building vector tree constants.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GCC_TREE_VECTOR_BUILDER_H
> +#define GCC_TREE_VECTOR_BUILDER_H
> +
> +#include "vector-builder.h"
> +
> +/* This class is used to build VECTOR_CSTs from a sequence of elements.
> +   See vector_builder for more details.  */
> +class tree_vector_builder : public vector_builder<tree, tree_vector_builder>
> +{
> +  typedef vector_builder<tree, tree_vector_builder> parent;
> +  friend class vector_builder<tree, tree_vector_builder>;
> +
> +public:
> +  tree_vector_builder () : m_type (0) {}
> +  tree_vector_builder (tree, unsigned int, unsigned int);
> +  tree build ();
> +
> +  tree type () const { return m_type; }
> +
> +  void new_vector (tree, unsigned int, unsigned int);
> +  bool new_unary_operation (tree, tree, bool);
> +
> +private:
> +  bool equal_p (const_tree, const_tree) const;
> +  bool allow_steps_p () const;
> +  bool integral_p (const_tree) const;
> +  wide_int step (const_tree, const_tree) const;
> +  bool can_elide_p (const_tree) const;
> +  void note_representative (tree *, tree);
> +
> +  tree m_type;
> +};
> +
> +/* Create a new builder for a vector of type TYPE.  Initially encode the
> +   value as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements
> +   each.  */
> +
> +inline
> +tree_vector_builder::tree_vector_builder (tree type, unsigned int npatterns,
> +                                         unsigned int nelts_per_pattern)
> +{
> +  new_vector (type, npatterns, nelts_per_pattern);
> +}
> +
> +/* Start building a new vector of type TYPE.  Initially encode the value
> +   as NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.  */
> +
> +inline void
> +tree_vector_builder::new_vector (tree type, unsigned int npatterns,
> +                                unsigned int nelts_per_pattern)
> +{
> +  m_type = type;
> +  parent::new_vector (TYPE_VECTOR_SUBPARTS (type), npatterns,
> +                     nelts_per_pattern);
> +}
> +
> +/* Return true if elements I1 and I2 are equal.  */
> +
> +inline bool
> +tree_vector_builder::equal_p (const_tree elt1, const_tree elt2) const
> +{
> +  return operand_equal_p (elt1, elt2, 0);
> +}
> +
> +/* Return true if a stepped representation is OK.  We don't allow
> +   linear series for anything other than integers, to avoid problems
> +   with rounding.  */
> +
> +inline bool
> +tree_vector_builder::allow_steps_p () const
> +{
> +  return INTEGRAL_TYPE_P (TREE_TYPE (m_type));
> +}
> +
> +/* Return true if ELT can be interpreted as an integer.  */
> +
> +inline bool
> +tree_vector_builder::integral_p (const_tree elt) const
> +{
> +  return TREE_CODE (elt) == INTEGER_CST;
> +}
> +
> +/* Return the value of element ELT2 minus the value of element ELT1.
> +   Both elements are known to be INTEGER_CSTs.  */
> +
> +inline wide_int
> +tree_vector_builder::step (const_tree elt1, const_tree elt2) const
> +{
> +  return wi::to_wide (elt2) - wi::to_wide (elt1);
> +}
> +
> +/* Return true if we can drop element ELT, even if the retained elements
> +   are different.  Return false if this would mean losing overflow
> +   information.  */
> +
> +inline bool
> +tree_vector_builder::can_elide_p (const_tree elt) const
> +{
> +  return !CONSTANT_CLASS_P (elt) || !TREE_OVERFLOW (elt);
> +}
> +
> +/* Record that ELT2 is being elided, given that ELT1_PTR points to the last
> +   encoded element for the containing pattern.  */
> +
> +inline void
> +tree_vector_builder::note_representative (tree *elt1_ptr, tree elt2)
> +{
> +  if (CONSTANT_CLASS_P (elt2) && TREE_OVERFLOW (elt2))
> +    {
> +      gcc_assert (operand_equal_p (*elt1_ptr, elt2, 0));
> +      if (!TREE_OVERFLOW (elt2))
> +       *elt1_ptr = elt2;
> +    }
> +}
> +
> +#endif
> Index: gcc/tree-vector-builder.c
> ===================================================================
> --- /dev/null   2017-12-05 14:21:55.753572108 +0000
> +++ gcc/tree-vector-builder.c   2017-12-06 14:46:14.131599903 +0000
> @@ -0,0 +1,64 @@
> +/* A class for building vector tree constants.
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#include "config.h"
> +#include "system.h"
> +#include "coretypes.h"
> +#include "tree.h"
> +#include "fold-const.h"
> +#include "tree-vector-builder.h"
> +
> +/* Try to start building a new vector of type TYPE that holds the result of
> +   a unary operation on VECTOR_CST T.  ALLOW_STEPPED_P is true if the
> +   operation can handle stepped encodings directly, without having to
> +   expand the full sequence.
> +
> +   Return true if the operation is possible, which it always is when
> +   ALLOW_STEPPED_P is true.  Leave the builder unchanged otherwise.  */
> +
> +bool
> +tree_vector_builder::new_unary_operation (tree type, tree t,
> +                                         bool allow_stepped_p)
> +{
> +  unsigned int full_nelts = TYPE_VECTOR_SUBPARTS (type);
> +  gcc_assert (full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t)));
> +  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +  unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (t);
> +  if (!allow_stepped_p && nelts_per_pattern > 2)
> +    {
> +      npatterns = full_nelts;
> +      nelts_per_pattern = 1;
> +    }
> +  new_vector (type, npatterns, nelts_per_pattern);
> +  return true;
> +}
> +
> +/* Return a VECTOR_CST for the current constant.  */
> +
> +tree
> +tree_vector_builder::build ()
> +{
> +  finalize ();
> +  gcc_assert (pow2p_hwi (npatterns ()));
> +  tree v = make_vector (exact_log2 (npatterns ()), nelts_per_pattern ());
> +  TREE_TYPE (v) = m_type;
> +  memcpy (VECTOR_CST_ENCODED_ELTS (v), address (),
> +         encoded_nelts () * sizeof (tree));
> +  return v;
> +}
> Index: gcc/Makefile.in
> ===================================================================
> --- gcc/Makefile.in     2017-12-06 14:46:13.860611208 +0000
> +++ gcc/Makefile.in     2017-12-06 14:46:14.128600028 +0000
> @@ -1572,6 +1572,7 @@ OBJS = \
>         tree-vect-loop-manip.o \
>         tree-vect-slp.o \
>         tree-vectorizer.o \
> +       tree-vector-builder.o \
>         tree-vrp.o \
>         tree.o \
>         typed-splay-tree.o \
> Index: gcc/tree.def
> ===================================================================
> --- gcc/tree.def        2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree.def        2017-12-06 14:46:14.132599861 +0000
> @@ -301,7 +301,7 @@ DEFTREECODE (FIXED_CST, "fixed_cst", tcc
>     whose contents are other constant nodes.  */
>  DEFTREECODE (COMPLEX_CST, "complex_cst", tcc_constant, 0)
>
> -/* Contents are in VECTOR_CST_ELTS field.  */
> +/* See generic.texi for details.  */
>  DEFTREECODE (VECTOR_CST, "vector_cst", tcc_constant, 0)
>
>  /* Contents are TREE_STRING_LENGTH and the actual contents of the string.  */
> Index: gcc/tree-core.h
> ===================================================================
> --- gcc/tree-core.h     2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree-core.h     2017-12-06 14:46:14.130599945 +0000
> @@ -972,8 +972,17 @@ struct GTY(()) tree_base {
>      /* VEC length.  This field is only used with TREE_VEC.  */
>      int length;
>
> -    /* Number of elements.  This field is only used with VECTOR_CST.  */
> -    unsigned int nelts;
> +    /* This field is only used with VECTOR_CST.  */
> +    struct {
> +      /* The value of VECTOR_CST_LOG2_NPATTERNS.  */
> +      unsigned int log2_npatterns : 8;
> +
> +      /* The value of VECTOR_CST_NELTS_PER_PATTERN.  */
> +      unsigned int nelts_per_pattern : 8;
> +
> +      /* For future expansion.  */
> +      unsigned int unused : 16;
> +    } vector_cst;
>
>      /* SSA version number.  This field is only used with SSA_NAME.  */
>      unsigned int version;
> @@ -1325,7 +1334,7 @@ struct GTY(()) tree_complex {
>
>  struct GTY(()) tree_vector {
>    struct tree_typed typed;
> -  tree GTY ((length ("VECTOR_CST_NELTS ((tree) &%h)"))) elts[1];
> +  tree GTY ((length ("vector_cst_encoded_nelts ((tree) &%h)"))) elts[1];
>  };
>
>  struct GTY(()) tree_identifier {
> Index: gcc/tree.h
> ===================================================================
> --- gcc/tree.h  2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree.h  2017-12-06 14:46:14.133599820 +0000
> @@ -1008,10 +1008,24 @@ #define TREE_STRING_POINTER(NODE) \
>  #define TREE_REALPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.real)
>  #define TREE_IMAGPART(NODE) (COMPLEX_CST_CHECK (NODE)->complex.imag)
>
> -/* In a VECTOR_CST node.  */
> -#define VECTOR_CST_NELTS(NODE) (VECTOR_CST_CHECK (NODE)->base.u.nelts)
> -#define VECTOR_CST_ELTS(NODE) (VECTOR_CST_CHECK (NODE)->vector.elts)
> -#define VECTOR_CST_ELT(NODE,IDX) (VECTOR_CST_CHECK (NODE)->vector.elts[IDX])
> +/* In a VECTOR_CST node.  See generic.texi for details.  */
> +#define VECTOR_CST_NELTS(NODE) (TYPE_VECTOR_SUBPARTS (TREE_TYPE (NODE)))
> +#define VECTOR_CST_ELT(NODE,IDX) vector_cst_elt (NODE, IDX)
> +
> +#define VECTOR_CST_LOG2_NPATTERNS(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.log2_npatterns)
> +#define VECTOR_CST_NPATTERNS(NODE) \
> +  (1U << VECTOR_CST_LOG2_NPATTERNS (NODE))
> +#define VECTOR_CST_NELTS_PER_PATTERN(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->base.u.vector_cst.nelts_per_pattern)
> +#define VECTOR_CST_DUPLICATE_P(NODE) \
> +  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 1)
> +#define VECTOR_CST_STEPPED_P(NODE) \
> +  (VECTOR_CST_NELTS_PER_PATTERN (NODE) == 3)
> +#define VECTOR_CST_ENCODED_ELTS(NODE) \
> +  (VECTOR_CST_CHECK (NODE)->vector.elts)
> +#define VECTOR_CST_ENCODED_ELT(NODE, ELT) \
> +  (VECTOR_CST_CHECK (NODE)->vector.elts[ELT])
>
>  /* Define fields and accessors for some special-purpose tree nodes.  */
>
> @@ -3882,6 +3896,14 @@ #define error_operand_p(NODE)                                    \
>    ((NODE) == error_mark_node                                   \
>     || ((NODE) && TREE_TYPE ((NODE)) == error_mark_node))
>
> +/* Return the number of elements encoded directly in a VECTOR_CST.  */
> +
> +inline unsigned int
> +vector_cst_encoded_nelts (const_tree t)
> +{
> +  return VECTOR_CST_NPATTERNS (t) * VECTOR_CST_NELTS_PER_PATTERN (t);
> +}
> +
>  extern tree decl_assembler_name (tree);
>  extern void overwrite_decl_assembler_name (tree decl, tree name);
>  extern tree decl_comdat_group (const_tree);
> @@ -4021,7 +4043,7 @@ extern tree force_fit_type (tree, const
>  extern tree build_int_cst (tree, HOST_WIDE_INT);
>  extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
>  extern tree build_int_cst_type (tree, HOST_WIDE_INT);
> -extern tree make_vector (unsigned CXX_MEM_STAT_INFO);
> +extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
>  extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
>  extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
>  extern tree build_vector_from_val (tree, tree);
> @@ -4268,6 +4290,9 @@ extern tree first_field (const_tree);
>
>  extern bool initializer_zerop (const_tree);
>
> +extern wide_int vector_cst_int_elt (const_tree, unsigned int);
> +extern tree vector_cst_elt (const_tree, unsigned int);
> +
>  /* Given a vector VEC, return its first element if all elements are
>     the same.  Otherwise return NULL_TREE.  */
>
> Index: gcc/tree.c
> ===================================================================
> --- gcc/tree.c  2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree.c  2017-12-06 14:46:14.132599861 +0000
> @@ -66,6 +66,7 @@ Software Foundation; either version 3, o
>  #include "attribs.h"
>  #include "rtl.h"
>  #include "regs.h"
> +#include "tree-vector-builder.h"
>
>  /* Tree code classes.  */
>
> @@ -837,7 +838,7 @@ tree_code_size (enum tree_code code)
>         case REAL_CST:          return sizeof (tree_real_cst);
>         case FIXED_CST:         return sizeof (tree_fixed_cst);
>         case COMPLEX_CST:       return sizeof (tree_complex);
> -       case VECTOR_CST:        return sizeof (tree_vector);
> +       case VECTOR_CST:        gcc_unreachable ();
>         case STRING_CST:        gcc_unreachable ();
>         default:
>           gcc_checking_assert (code >= NUM_TREE_CODES);
> @@ -897,7 +898,7 @@ tree_size (const_tree node)
>
>      case VECTOR_CST:
>        return (sizeof (struct tree_vector)
> -             + (VECTOR_CST_NELTS (node) - 1) * sizeof (tree));
> +             + (vector_cst_encoded_nelts (node) - 1) * sizeof (tree));
>
>      case STRING_CST:
>        return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
> @@ -1708,13 +1709,19 @@ cst_and_fits_in_hwi (const_tree x)
>           && (tree_fits_shwi_p (x) || tree_fits_uhwi_p (x)));
>  }
>
> -/* Build a newly constructed VECTOR_CST node of length LEN.  */
> +/* Build a newly constructed VECTOR_CST with the given values of
> +   (VECTOR_CST_)LOG2_NPATTERNS and (VECTOR_CST_)NELTS_PER_PATTERN.  */
>
>  tree
> -make_vector (unsigned len MEM_STAT_DECL)
> +make_vector (unsigned log2_npatterns,
> +            unsigned int nelts_per_pattern MEM_STAT_DECL)
>  {
> +  gcc_assert (IN_RANGE (nelts_per_pattern, 1, 3));
>    tree t;
> -  unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
> +  unsigned npatterns = 1 << log2_npatterns;
> +  unsigned encoded_nelts = npatterns * nelts_per_pattern;
> +  unsigned length = (sizeof (struct tree_vector)
> +                    + (encoded_nelts - 1) * sizeof (tree));
>
>    record_node_allocation_statistics (VECTOR_CST, length);
>
> @@ -1722,7 +1729,8 @@ make_vector (unsigned len MEM_STAT_DECL)
>
>    TREE_SET_CODE (t, VECTOR_CST);
>    TREE_CONSTANT (t) = 1;
> -  VECTOR_CST_NELTS (t) = len;
> +  VECTOR_CST_LOG2_NPATTERNS (t) = log2_npatterns;
> +  VECTOR_CST_NELTS_PER_PATTERN (t) = nelts_per_pattern;
>
>    return t;
>  }
> @@ -1733,29 +1741,10 @@ make_vector (unsigned len MEM_STAT_DECL)
>  tree
>  build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
>  {
> -  unsigned int nelts = vals.length ();
> -  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
> -  int over = 0;
> -  unsigned cnt = 0;
> -  tree v = make_vector (nelts);
> -  TREE_TYPE (v) = type;
> -
> -  /* Iterate through elements and check for overflow.  */
> -  for (cnt = 0; cnt < nelts; ++cnt)
> -    {
> -      tree value = vals[cnt];
> -
> -      VECTOR_CST_ELT (v, cnt) = value;
> -
> -      /* Don't crash if we get an address constant.  */
> -      if (!CONSTANT_CLASS_P (value))
> -       continue;
> -
> -      over |= TREE_OVERFLOW (value);
> -    }
> -
> -  TREE_OVERFLOW (v) = over;
> -  return v;
> +  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
> +  tree_vector_builder builder (type, vals.length (), 1);
> +  builder.splice (vals);
> +  return builder.build ();
>  }
>
>  /* Return a new VECTOR_CST node whose type is TYPE and whose values
> @@ -10370,6 +10359,59 @@ build_opaque_vector_type (tree innertype
>    return cand;
>  }
>
> +/* Return the value of element I of VECTOR_CST T as a wide_int.  */
> +
> +wide_int
> +vector_cst_int_elt (const_tree t, unsigned int i)
> +{
> +  /* First handle elements that are directly encoded.  */
> +  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
> +  if (i < encoded_nelts)
> +    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, i));
> +
> +  /* Identify the pattern that contains element I and work out the index of
> +     the last encoded element for that pattern.  */
> +  unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +  unsigned int pattern = i % npatterns;
> +  unsigned int count = i / npatterns;
> +  unsigned int final_i = encoded_nelts - npatterns + pattern;
> +
> +  /* If there are no steps, the final encoded value is the right one.  */
> +  if (!VECTOR_CST_STEPPED_P (t))
> +    return wi::to_wide (VECTOR_CST_ENCODED_ELT (t, final_i));
> +
> +  /* Otherwise work out the value from the last two encoded elements.  */
> +  tree v1 = VECTOR_CST_ENCODED_ELT (t, final_i - npatterns);
> +  tree v2 = VECTOR_CST_ENCODED_ELT (t, final_i);
> +  wide_int diff = wi::to_wide (v2) - wi::to_wide (v1);
> +  return wi::to_wide (v2) + (count - 2) * diff;
> +}
> +
> +/* Return the value of element I of VECTOR_CST T.  */
> +
> +tree
> +vector_cst_elt (const_tree t, unsigned int i)
> +{
> +  /* First handle elements that are directly encoded.  */
> +  unsigned int encoded_nelts = vector_cst_encoded_nelts (t);
> +  if (i < encoded_nelts)
> +    return VECTOR_CST_ENCODED_ELT (t, i);
> +
> +  /* If there are no steps, the final encoded value is the right one.  */
> +  if (!VECTOR_CST_STEPPED_P (t))
> +    {
> +      /* Identify the pattern that contains element I and work out the index of
> +        the last encoded element for that pattern.  */
> +      unsigned int npatterns = VECTOR_CST_NPATTERNS (t);
> +      unsigned int pattern = i % npatterns;
> +      unsigned int final_i = encoded_nelts - npatterns + pattern;
> +      return VECTOR_CST_ENCODED_ELT (t, final_i);
> +    }
> +
> +  /* Otherwise work out the value from the last two encoded elements.  */
> +  return wide_int_to_tree (TREE_TYPE (TREE_TYPE (t)),
> +                          vector_cst_int_elt (t, i));
> +}
>
>  /* Given an initializer INIT, return TRUE if INIT is zero or some
>     aggregate of zeros.  Otherwise return FALSE.  */
> @@ -12451,6 +12493,23 @@ drop_tree_overflow (tree t)
>    if (TREE_CODE (t) == INTEGER_CST)
>      return wide_int_to_tree (TREE_TYPE (t), wi::to_wide (t));
>
> +  /* For VECTOR_CST, remove the overflow bits from the encoded elements
> +     and canonicalize the result.  */
> +  if (TREE_CODE (t) == VECTOR_CST)
> +    {
> +      tree_vector_builder builder;
> +      builder.new_unary_operation (TREE_TYPE (t), t, true);
> +      unsigned int count = builder.encoded_nelts ();
> +      for (unsigned int i = 0; i < count; ++i)
> +       {
> +         tree elt = VECTOR_CST_ELT (t, i);
> +         if (TREE_OVERFLOW (elt))
> +           elt = drop_tree_overflow (elt);
> +         builder.quick_push (elt);
> +       }
> +      return builder.build ();
> +    }
> +
>    /* Otherwise, as all tcc_constants are possibly shared, copy the node
>       and drop the flag.  */
>    t = copy_node (t);
> @@ -12465,15 +12524,7 @@ drop_tree_overflow (tree t)
>        if (TREE_OVERFLOW (TREE_IMAGPART (t)))
>         TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
>      }
> -  if (TREE_CODE (t) == VECTOR_CST)
> -    {
> -      for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> -       {
> -         tree& elt = VECTOR_CST_ELT (t, i);
> -         if (TREE_OVERFLOW (elt))
> -           elt = drop_tree_overflow (elt);
> -       }
> -    }
> +
>    return t;
>  }
>
> @@ -14016,6 +14067,139 @@ test_labels ()
>    ASSERT_FALSE (FORCED_LABEL (label_decl));
>  }
>
> +/* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
> +
> +static void
> +check_vector_cst (vec<tree> expected, tree actual)
> +{
> +  ASSERT_EQ (expected.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (actual)));
> +  for (unsigned int i = 0; i < expected.length (); ++i)
> +    ASSERT_EQ (wi::to_wide (expected[i]),
> +              wi::to_wide (vector_cst_elt (actual, i)));
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS duplicated elements,
> +   and that its elements match EXPECTED.  */
> +
> +static void
> +check_vector_cst_duplicate (vec<tree> expected, tree actual,
> +                           unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_TRUE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS foreground elements
> +   and NPATTERNS background elements, and that its elements match
> +   EXPECTED.  */
> +
> +static void
> +check_vector_cst_fill (vec<tree> expected, tree actual,
> +                      unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (2 * npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_FALSE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Check that VECTOR_CST ACTUAL contains NPATTERNS stepped patterns,
> +   and that its elements match EXPECTED.  */
> +
> +static void
> +check_vector_cst_stepped (vec<tree> expected, tree actual,
> +                         unsigned int npatterns)
> +{
> +  ASSERT_EQ (npatterns, VECTOR_CST_NPATTERNS (actual));
> +  ASSERT_EQ (3, VECTOR_CST_NELTS_PER_PATTERN (actual));
> +  ASSERT_EQ (3 * npatterns, vector_cst_encoded_nelts (actual));
> +  ASSERT_FALSE (VECTOR_CST_DUPLICATE_P (actual));
> +  ASSERT_TRUE (VECTOR_CST_STEPPED_P (actual));
> +  check_vector_cst (expected, actual);
> +}
> +
> +/* Test the creation of VECTOR_CSTs.  */
> +
> +static void
> +test_vector_cst_patterns ()
> +{
> +  auto_vec<tree, 8> elements (8);
> +  elements.quick_grow (8);
> +  tree element_type = build_nonstandard_integer_type (16, true);
> +  tree vector_type = build_vector_type (element_type, 8);
> +
> +  /* Test a simple linear series with a base of 0 and a step of 1:
> +     { 0, 1, 2, 3, 4, 5, 6, 7 }.  */
> +  for (unsigned int i = 0; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, i);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try the same with the first element replaced by 100:
> +     { 100, 1, 2, 3, 4, 5, 6, 7 }.  */
> +  elements[0] = build_int_cst (element_type, 100);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try a series that wraps around.
> +     { 100, 65531, 65532, 65533, 65534, 65535, 0, 1 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, (65530 + i) & 0xffff);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try a downward series:
> +     { 100, 79, 78, 77, 76, 75, 75, 73 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = build_int_cst (element_type, 80 - i);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 1);
> +
> +  /* Try two interleaved series with different bases and steps:
> +     { 100, 53, 66, 206, 62, 212, 58, 218 }.  */
> +  elements[1] = build_int_cst (element_type, 53);
> +  for (unsigned int i = 2; i < 8; i += 2)
> +    {
> +      elements[i] = build_int_cst (element_type, 70 - i * 2);
> +      elements[i + 1] = build_int_cst (element_type, 200 + i * 3);
> +    }
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try a duplicated value:
> +     { 100, 100, 100, 100, 100, 100, 100, 100 }.  */
> +  for (unsigned int i = 1; i < 8; ++i)
> +    elements[i] = elements[0];
> +  check_vector_cst_duplicate (elements,
> +                             build_vector (vector_type, elements), 1);
> +
> +  /* Try an interleaved duplicated value:
> +     { 100, 55, 100, 55, 100, 55, 100, 55 }.  */
> +  elements[1] = build_int_cst (element_type, 55);
> +  for (unsigned int i = 2; i < 8; ++i)
> +    elements[i] = elements[i - 2];
> +  check_vector_cst_duplicate (elements,
> +                             build_vector (vector_type, elements), 2);
> +
> +  /* Try a duplicated value with 2 exceptions
> +     { 41, 97, 100, 55, 100, 55, 100, 55 }.  */
> +  elements[0] = build_int_cst (element_type, 41);
> +  elements[1] = build_int_cst (element_type, 97);
> +  check_vector_cst_fill (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try with and without a step
> +     { 41, 97, 100, 21, 100, 35, 100, 49 }.  */
> +  for (unsigned int i = 3; i < 8; i += 2)
> +    elements[i] = build_int_cst (element_type, i * 7);
> +  check_vector_cst_stepped (elements, build_vector (vector_type, elements), 2);
> +
> +  /* Try a fully-general constant:
> +     { 41, 97, 100, 21, 100, 9990, 100, 49 }.  */
> +  elements[5] = build_int_cst (element_type, 9990);
> +  check_vector_cst_fill (elements, build_vector (vector_type, elements), 4);
> +}
> +
>  /* Run all of the selftests within this file.  */
>
>  void
> @@ -14024,6 +14208,7 @@ tree_c_tests ()
>    test_integer_constants ();
>    test_identifiers ();
>    test_labels ();
> +  test_vector_cst_patterns ();
>  }
>
>  } // namespace selftest
> Index: gcc/lto-streamer-out.c
> ===================================================================
> --- gcc/lto-streamer-out.c      2017-12-06 14:46:13.860611208 +0000
> +++ gcc/lto-streamer-out.c      2017-12-06 14:46:14.130599945 +0000
> @@ -747,8 +747,9 @@ #define DFS_follow_tree_edge(DEST) \
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -       DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
> +      unsigned int count = vector_cst_encoded_nelts (expr);
> +      for (unsigned int i = 0; i < count; ++i)
> +       DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
> @@ -1195,8 +1196,11 @@ #define visit(SIBLING) \
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> -    for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
> -      visit (VECTOR_CST_ELT (t, i));
> +    {
> +      unsigned int count = vector_cst_encoded_nelts (t);
> +      for (unsigned int i = 0; i < count; ++i)
> +       visit (VECTOR_CST_ENCODED_ELT (t, i));
> +    }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
>      {
> Index: gcc/tree-streamer-out.c
> ===================================================================
> --- gcc/tree-streamer-out.c     2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree-streamer-out.c     2017-12-06 14:46:14.131599903 +0000
> @@ -533,11 +533,11 @@ write_ts_common_tree_pointers (struct ou
>  static void
>  write_ts_vector_tree_pointers (struct output_block *ob, tree expr, bool ref_p)
>  {
> -  unsigned i;
>    /* Note that the number of elements for EXPR has already been emitted
>       in EXPR's header (see streamer_write_tree_header).  */
> -  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -    stream_write_tree (ob, VECTOR_CST_ELT (expr, i), ref_p);
> +  unsigned int count = vector_cst_encoded_nelts (expr);
> +  for (unsigned int i = 0; i < count; ++i)
> +    stream_write_tree (ob, VECTOR_CST_ENCODED_ELT (expr, i), ref_p);
>  }
>
>
> @@ -960,7 +960,12 @@ streamer_write_tree_header (struct outpu
>    else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
>      write_identifier (ob, ob->main_stream, expr);
>    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> -    streamer_write_hwi (ob, VECTOR_CST_NELTS (expr));
> +    {
> +      bitpack_d bp = bitpack_create (ob->main_stream);
> +      bp_pack_value (&bp, VECTOR_CST_LOG2_NPATTERNS (expr), 8);
> +      bp_pack_value (&bp, VECTOR_CST_NELTS_PER_PATTERN (expr), 8);
> +      streamer_write_bitpack (&bp);
> +    }
>    else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
>      streamer_write_hwi (ob, TREE_VEC_LENGTH (expr));
>    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
> Index: gcc/tree-streamer-in.c
> ===================================================================
> --- gcc/tree-streamer-in.c      2017-12-06 14:46:13.860611208 +0000
> +++ gcc/tree-streamer-in.c      2017-12-06 14:46:14.130599945 +0000
> @@ -592,8 +592,10 @@ streamer_alloc_tree (struct lto_input_bl
>      }
>    else if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      HOST_WIDE_INT len = streamer_read_hwi (ib);
> -      result = make_vector (len);
> +      bitpack_d bp = streamer_read_bitpack (ib);
> +      unsigned int log2_npatterns = bp_unpack_value (&bp, 8);
> +      unsigned int nelts_per_pattern = bp_unpack_value (&bp, 8);
> +      result = make_vector (log2_npatterns, nelts_per_pattern);
>      }
>    else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
>      {
> @@ -650,9 +652,9 @@ lto_input_ts_common_tree_pointers (struc
>  lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
>                                    struct data_in *data_in, tree expr)
>  {
> -  unsigned i;
> -  for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -    VECTOR_CST_ELT (expr, i) = stream_read_tree (ib, data_in);
> +  unsigned int count = vector_cst_encoded_nelts (expr);
> +  for (unsigned int i = 0; i < count; ++i)
> +    VECTOR_CST_ENCODED_ELT (expr, i) = stream_read_tree (ib, data_in);
>  }
>
>
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-12-06 14:46:13.860611208 +0000
> +++ gcc/fold-const.c    2017-12-06 14:46:14.129599986 +0000
> @@ -11610,9 +11610,8 @@ fold_ternary_loc (location_t loc, enum t
>                   unsigned int nelts = VECTOR_CST_NELTS (arg0);
>                   auto_vec<tree, 32> elts (nelts);
>                   elts.quick_grow (nelts);
> -                 memcpy (&elts[0], VECTOR_CST_ELTS (arg0),
> -                         sizeof (tree) * nelts);
> -                 elts[k] = arg1;
> +                 for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
> +                   elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
>                   return build_vector (type, elts);
>                 }
>             }
> Index: gcc/lto/lto.c
> ===================================================================
> --- gcc/lto/lto.c       2017-12-06 14:46:13.860611208 +0000
> +++ gcc/lto/lto.c       2017-12-06 14:46:14.130599945 +0000
> @@ -1065,6 +1065,12 @@ #define compare_values(X) \
>                         TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
>        return false;
>
> +  if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
> +    {
> +      compare_values (VECTOR_CST_LOG2_NPATTERNS);
> +      compare_values (VECTOR_CST_NELTS_PER_PATTERN);
> +    }
> +
>    if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
>      {
>        compare_values (DECL_MODE);
> @@ -1281,11 +1287,12 @@ #define compare_tree_edges(E1, E2) \
>
>    if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
>      {
> -      unsigned i;
>        /* Note that the number of elements for EXPR has already been emitted
>          in EXPR's header (see streamer_write_tree_header).  */
> -      for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
> -       compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
> +      unsigned int count = vector_cst_encoded_nelts (t1);
> +      for (unsigned int i = 0; i < count; ++i)
> +       compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
> +                           VECTOR_CST_ENCODED_ELT (t2, i));
>      }
>
>    if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Use tree_vector_builder::new_unary_operation for folding
  2017-12-06 15:23           ` Use tree_vector_builder::new_unary_operation for folding Richard Sandiford
@ 2017-12-07 11:06             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:06 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:23 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> This patch makes fold-const.c operate directly on the VECTOR_CST
> encoding when folding an operation that has a single VECTOR_CST input.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * fold-const.c (fold_negate_expr_1): Use tree_vector_builder and
>         new_unary_operation, operating only on the encoded elements.
>         (const_unop): Likewise.
>         (exact_inverse): Likewise.
>         (distributes_over_addition_p): New function.
>         (const_binop): Use tree_vector_builder and new_unary_operation
>         for combinations of VECTOR_CST and INTEGER_CST.  Operate only
>         on the encoded elements unless the encoding is strided and the
>         operation does not distribute over addition.
>         (fold_convert_const):  Use tree_vector_builder and
>         new_unary_operation.  Operate only on the encoded elements
>         for truncating integer conversions, or for non-stepped encodings.
>
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-12-06 14:48:52.887162217 +0000
> +++ gcc/fold-const.c    2017-12-06 14:48:56.997993407 +0000
> @@ -566,10 +566,10 @@ fold_negate_expr_1 (location_t loc, tree
>
>      case VECTOR_CST:
>        {
> -       int count = VECTOR_CST_NELTS (t), i;
> -
> -       auto_vec<tree, 32> elts (count);
> -       for (i = 0; i < count; i++)
> +       tree_vector_builder elts;
> +       elts.new_unary_operation (type, t, true);
> +       unsigned int count = elts.encoded_nelts ();
> +       for (unsigned int i = 0; i < count; ++i)
>           {
>             tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
>             if (elt == NULL_TREE)
> @@ -577,7 +577,7 @@ fold_negate_expr_1 (location_t loc, tree
>             elts.quick_push (elt);
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      case COMPLEX_EXPR:
> @@ -1121,6 +1121,27 @@ int_const_binop (enum tree_code code, co
>    return int_const_binop_1 (code, arg1, arg2, 1);
>  }
>
> +/* Return true if binary operation OP distributes over addition in operand
> +   OPNO, with the other operand being held constant.  OPNO counts from 1.  */
> +
> +static bool
> +distributes_over_addition_p (tree_code op, int opno)
> +{
> +  switch (op)
> +    {
> +    case PLUS_EXPR:
> +    case MINUS_EXPR:
> +    case MULT_EXPR:
> +      return true;
> +
> +    case LSHIFT_EXPR:
> +      return opno == 1;
> +
> +    default:
> +      return false;
> +    }
> +}
> +
>  /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
>     constant.  We assume ARG1 and ARG2 have the same data type, or at least
>     are the same kind of constant and the same machine mode.  Return zero if
> @@ -1442,10 +1463,12 @@ const_binop (enum tree_code code, tree a
>        && TREE_CODE (arg2) == INTEGER_CST)
>      {
>        tree type = TREE_TYPE (arg1);
> -      int count = VECTOR_CST_NELTS (arg1), i;
> -
> -      auto_vec<tree, 32> elts (count);
> -      for (i = 0; i < count; i++)
> +      bool step_ok_p = distributes_over_addition_p (code, 1);
> +      tree_vector_builder elts;
> +      if (!elts.new_unary_operation (type, arg1, step_ok_p))
> +       return NULL_TREE;
> +      unsigned int count = elts.encoded_nelts ();
> +      for (unsigned int i = 0; i < count; ++i)
>         {
>           tree elem1 = VECTOR_CST_ELT (arg1, i);
>
> @@ -1458,7 +1481,7 @@ const_binop (enum tree_code code, tree a
>           elts.quick_push (elt);
>         }
>
> -      return build_vector (type, elts);
> +      return elts.build ();
>      }
>    return NULL_TREE;
>  }
> @@ -1649,10 +1672,12 @@ const_unop (enum tree_code code, tree ty
>        else if (TREE_CODE (arg0) == VECTOR_CST)
>         {
>           tree elem;
> -         unsigned count = VECTOR_CST_NELTS (arg0), i;
>
> -         auto_vec<tree, 32> elements (count);
> -         for (i = 0; i < count; i++)
> +         /* This can cope with stepped encodings because ~x == -1 - x.  */
> +         tree_vector_builder elements;
> +         elements.new_unary_operation (type, arg0, true);
> +         unsigned int i, count = elements.encoded_nelts ();
> +         for (i = 0; i < count; ++i)
>             {
>               elem = VECTOR_CST_ELT (arg0, i);
>               elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
> @@ -1661,7 +1686,7 @@ const_unop (enum tree_code code, tree ty
>               elements.quick_push (elem);
>             }
>           if (i == count)
> -           return build_vector (type, elements);
> +           return elements.build ();
>         }
>        break;
>
> @@ -2135,10 +2160,19 @@ fold_convert_const (enum tree_code code,
>        if (TREE_CODE (arg1) == VECTOR_CST
>           && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
>         {
> -         int len = VECTOR_CST_NELTS (arg1);
>           tree elttype = TREE_TYPE (type);
> -         auto_vec<tree, 32> v (len);
> -         for (int i = 0; i < len; ++i)
> +         tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
> +         /* We can't handle steps directly when extending, since the
> +            values need to wrap at the original precision first.  */
> +         bool step_ok_p
> +           = (INTEGRAL_TYPE_P (elttype)
> +              && INTEGRAL_TYPE_P (arg1_elttype)
> +              && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
> +         tree_vector_builder v;
> +         if (!v.new_unary_operation (type, arg1, step_ok_p))
> +           return NULL_TREE;
> +         unsigned int len = v.encoded_nelts ();
> +         for (unsigned int i = 0; i < len; ++i)
>             {
>               tree elt = VECTOR_CST_ELT (arg1, i);
>               tree cvt = fold_convert_const (code, elttype, elt);
> @@ -2146,7 +2180,7 @@ fold_convert_const (enum tree_code code,
>                 return NULL_TREE;
>               v.quick_push (cvt);
>             }
> -         return build_vector (type, v);
> +         return v.build ();
>         }
>      }
>    return NULL_TREE;
> @@ -8832,7 +8866,6 @@ exact_inverse (tree type, tree cst)
>    REAL_VALUE_TYPE r;
>    tree unit_type;
>    machine_mode mode;
> -  unsigned vec_nelts, i;
>
>    switch (TREE_CODE (cst))
>      {
> @@ -8846,12 +8879,14 @@ exact_inverse (tree type, tree cst)
>
>      case VECTOR_CST:
>        {
> -       vec_nelts = VECTOR_CST_NELTS (cst);
>         unit_type = TREE_TYPE (type);
>         mode = TYPE_MODE (unit_type);
>
> -       auto_vec<tree, 32> elts (vec_nelts);
> -       for (i = 0; i < vec_nelts; i++)
> +       tree_vector_builder elts;
> +       if (!elts.new_unary_operation (type, cst, false))
> +         return NULL_TREE;
> +       unsigned int count = elts.encoded_nelts ();
> +       for (unsigned int i = 0; i < count; ++i)
>           {
>             r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
>             if (!exact_real_inverse (mode, &r))
> @@ -8859,7 +8894,7 @@ exact_inverse (tree type, tree cst)
>             elts.quick_push (build_real (unit_type, r));
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      default:

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Use tree_vector_builder instead of build_vector
  2017-12-06 15:22           ` Use tree_vector_builder instead of build_vector Richard Sandiford
@ 2017-12-07 11:06             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:06 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:22 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> This patch switches most build_vector calls over to tree_vector_builder,
> using explicit encodings where appropriate.  Later patches handle
> the remaining uses of build_vector.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard

>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * config/sparc/sparc.c: Include tree-vector-builder.h.
>         (sparc_fold_builtin): Use tree_vector_builder instead of build_vector.
>         * expmed.c: Include tree-vector-builder.h.
>         (make_tree): Use tree_vector_builder instead of build_vector.
>         * fold-const.c: Include tree-vector-builder.h.
>         (const_binop): Use tree_vector_builder instead of build_vector.
>         (const_unop): Likewise.
>         (native_interpret_vector): Likewise.
>         (fold_vec_perm): Likewise.
>         (fold_ternary_loc): Likewise.
>         * gimple-fold.c: Include tree-vector-builder.h.
>         (gimple_fold_stmt_to_constant_1): Use tree_vector_builder instead
>         of build_vector.
>         * tree-ssa-forwprop.c: Include tree-vector-builder.h.
>         (simplify_vector_constructor): Use tree_vector_builder instead
>         of build_vector.
>         * tree-vect-generic.c: Include tree-vector-builder.h.
>         (add_rshift): Use tree_vector_builder instead of build_vector.
>         (expand_vector_divmod): Likewise.
>         (optimize_vector_constructor): Likewise.
>         * tree-vect-loop.c: Include tree-vector-builder.h.
>         (vect_create_epilog_for_reduction): Use tree_vector_builder instead
>         of build_vector.  Explicitly use a stepped encoding for
>         { 1, 2, 3, ... }.
>         * tree-vect-slp.c: Include tree-vector-builder.h.
>         (vect_get_constant_vectors): Use tree_vector_builder instead
>         of build_vector.
>         (vect_transform_slp_perm_load): Likewise.
>         (vect_schedule_slp_instance): Likewise.
>         * tree-vect-stmts.c: Include tree-vector-builder.h.
>         (vectorizable_bswap): Use tree_vector_builder instead of build_vector.
>         (vect_gen_perm_mask_any): Likewise.
>         (vectorizable_call): Likewise.  Explicitly use a stepped encoding.
>         * tree.c: (build_vector_from_ctor): Use tree_vector_builder instead
>         of build_vector.
>         (build_vector_from_val): Likewise.  Explicitly use a duplicate
>         encoding.
>
> Index: gcc/config/sparc/sparc.c
> ===================================================================
> --- gcc/config/sparc/sparc.c    2017-12-05 14:24:52.587013199 +0000
> +++ gcc/config/sparc/sparc.c    2017-12-06 14:48:52.885162299 +0000
> @@ -57,6 +57,7 @@ the Free Software Foundation; either ver
>  #include "tree-pass.h"
>  #include "context.h"
>  #include "builtins.h"
> +#include "tree-vector-builder.h"
>
>  /* This file should be included last.  */
>  #include "target-def.h"
> @@ -11752,14 +11753,14 @@ sparc_fold_builtin (tree fndecl, int n_a
>           tree inner_type = TREE_TYPE (rtype);
>           unsigned i;
>
> -         auto_vec<tree, 32> n_elts (VECTOR_CST_NELTS (arg0));
> +         tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
>           for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
>             {
>               unsigned HOST_WIDE_INT val
>                 = TREE_INT_CST_LOW (VECTOR_CST_ELT (arg0, i));
>               n_elts.quick_push (build_int_cst (inner_type, val << 4));
>             }
> -         return build_vector (rtype, n_elts);
> +         return n_elts.build ();
>         }
>        break;
>
> @@ -11774,9 +11775,9 @@ sparc_fold_builtin (tree fndecl, int n_a
>        if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
>         {
>           tree inner_type = TREE_TYPE (rtype);
> -         auto_vec<tree, 32> n_elts (VECTOR_CST_NELTS (arg0));
> +         tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
>           sparc_handle_vis_mul8x16 (&n_elts, code, inner_type, arg0, arg1);
> -         return build_vector (rtype, n_elts);
> +         return n_elts.build ();
>         }
>        break;
>
> @@ -11788,7 +11789,7 @@ sparc_fold_builtin (tree fndecl, int n_a
>
>        if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
>         {
> -         auto_vec<tree, 32> n_elts (2 * VECTOR_CST_NELTS (arg0));
> +         tree_vector_builder n_elts (rtype, 2 * VECTOR_CST_NELTS (arg0), 1);
>           unsigned i;
>           for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
>             {
> @@ -11796,7 +11797,7 @@ sparc_fold_builtin (tree fndecl, int n_a
>               n_elts.quick_push (VECTOR_CST_ELT (arg1, i));
>             }
>
> -         return build_vector (rtype, n_elts);
> +         return n_elts.build ();
>         }
>        break;
>
> Index: gcc/expmed.c
> ===================================================================
> --- gcc/expmed.c        2017-11-29 11:06:34.698180685 +0000
> +++ gcc/expmed.c        2017-12-06 14:48:52.886162258 +0000
> @@ -40,6 +40,7 @@ Software Foundation; either version 3, o
>  #include "explow.h"
>  #include "expr.h"
>  #include "langhooks.h"
> +#include "tree-vector-builder.h"
>
>  struct target_expmed default_target_expmed;
>  #if SWITCHABLE_TARGET
> @@ -5184,14 +5185,14 @@ make_tree (tree type, rtx x)
>         int i;
>
>         /* Build a tree with vector elements.  */
> -       auto_vec<tree, 32> elts (units);
> +       tree_vector_builder elts (type, units, 1);
>         for (i = 0; i < units; ++i)
>           {
>             rtx elt = CONST_VECTOR_ELT (x, i);
>             elts.quick_push (make_tree (itype, elt));
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      case PLUS:
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-12-06 14:46:14.129599986 +0000
> +++ gcc/fold-const.c    2017-12-06 14:48:52.887162217 +0000
> @@ -81,6 +81,7 @@ Software Foundation; either version 3, o
>  #include "selftest.h"
>  #include "stringpool.h"
>  #include "attribs.h"
> +#include "tree-vector-builder.h"
>
>  /* Nonzero if we are folding constants inside an initializer; zero
>     otherwise.  */
> @@ -1507,7 +1508,7 @@ const_binop (enum tree_code code, tree t
>         gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
>                     && out_nelts == TYPE_VECTOR_SUBPARTS (type));
>
> -       auto_vec<tree, 32> elts (out_nelts);
> +       tree_vector_builder elts (type, out_nelts, 1);
>         for (i = 0; i < out_nelts; i++)
>           {
>             tree elt = (i < in_nelts
> @@ -1521,7 +1522,7 @@ const_binop (enum tree_code code, tree t
>             elts.quick_push (elt);
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      case VEC_WIDEN_MULT_LO_EXPR:
> @@ -1548,7 +1549,7 @@ const_binop (enum tree_code code, tree t
>         else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
>           scale = 1, ofs = 1;
>
> -       auto_vec<tree, 32> elts (out_nelts);
> +       tree_vector_builder elts (type, out_nelts, 1);
>         for (out = 0; out < out_nelts; out++)
>           {
>             unsigned int in = (out << scale) + ofs;
> @@ -1565,7 +1566,7 @@ const_binop (enum tree_code code, tree t
>             elts.quick_push (elt);
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      default:;
> @@ -1704,7 +1705,7 @@ const_unop (enum tree_code code, tree ty
>         else
>           subcode = FLOAT_EXPR;
>
> -       auto_vec<tree, 32> elts (out_nelts);
> +       tree_vector_builder elts (type, out_nelts, 1);
>         for (i = 0; i < out_nelts; i++)
>           {
>             tree elt = fold_convert_const (subcode, TREE_TYPE (type),
> @@ -1714,7 +1715,7 @@ const_unop (enum tree_code code, tree ty
>             elts.quick_push (elt);
>           }
>
> -       return build_vector (type, elts);
> +       return elts.build ();
>        }
>
>      default:
> @@ -7377,7 +7378,7 @@ native_interpret_vector (tree type, cons
>    if (size * count > len)
>      return NULL_TREE;
>
> -  auto_vec<tree, 32> elements (count);
> +  tree_vector_builder elements (type, count, 1);
>    for (i = 0; i < count; ++i)
>      {
>        elem = native_interpret_expr (etype, ptr+(i*size), size);
> @@ -7385,7 +7386,7 @@ native_interpret_vector (tree type, cons
>         return NULL_TREE;
>        elements.quick_push (elem);
>      }
> -  return build_vector (type, elements);
> +  return elements.build ();
>  }
>
>
> @@ -8755,7 +8756,7 @@ fold_vec_perm (tree type, tree arg0, tre
>        || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
>      return NULL_TREE;
>
> -  auto_vec<tree, 32> out_elts (nelts);
> +  tree_vector_builder out_elts (type, nelts, 1);
>    for (i = 0; i < nelts; i++)
>      {
>        if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
> @@ -8772,7 +8773,7 @@ fold_vec_perm (tree type, tree arg0, tre
>        return build_constructor (type, v);
>      }
>    else
> -    return build_vector (type, out_elts);
> +    return out_elts.build ();
>  }
>
>  /* Try to fold a pointer difference of type TYPE two address expressions of
> @@ -11426,10 +11427,10 @@ fold_ternary_loc (location_t loc, enum t
>                   if (n == 1)
>                     return VECTOR_CST_ELT (arg0, idx);
>
> -                 auto_vec<tree, 32> vals (n);
> +                 tree_vector_builder vals (type, n, 1);
>                   for (unsigned i = 0; i < n; ++i)
>                     vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
> -                 return build_vector (type, vals);
> +                 return vals.build ();
>                 }
>             }
>         }
> @@ -11565,10 +11566,10 @@ fold_ternary_loc (location_t loc, enum t
>           if (need_mask_canon && arg2 == op2)
>             {
>               tree eltype = TREE_TYPE (TREE_TYPE (arg2));
> -             auto_vec<tree, 32> tsel (nelts);
> +             tree_vector_builder tsel (TREE_TYPE (arg2), nelts, 1);
>               for (i = 0; i < nelts; i++)
>                 tsel.quick_push (build_int_cst (eltype, sel[i]));
> -             op2 = build_vector (TREE_TYPE (arg2), tsel);
> +             op2 = tsel.build ();
>               changed = true;
>             }
>
> @@ -11608,11 +11609,11 @@ fold_ternary_loc (location_t loc, enum t
>               else
>                 {
>                   unsigned int nelts = VECTOR_CST_NELTS (arg0);
> -                 auto_vec<tree, 32> elts (nelts);
> +                 tree_vector_builder elts (type, nelts, 1);
>                   elts.quick_grow (nelts);
> -                 for (unsigned int i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
> +                 for (unsigned int i = 0; i < nelts; ++i)
>                     elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
> -                 return build_vector (type, elts);
> +                 return elts.build ();
>                 }
>             }
>         }
> Index: gcc/gimple-fold.c
> ===================================================================
> --- gcc/gimple-fold.c   2017-12-05 14:24:56.014949665 +0000
> +++ gcc/gimple-fold.c   2017-12-06 14:48:52.888162175 +0000
> @@ -63,6 +63,7 @@ Free Software Foundation; either version
>  #include "diagnostic-core.h"
>  #include "intl.h"
>  #include "calls.h"
> +#include "tree-vector-builder.h"
>
>  /* Return true when DECL can be referenced from current unit.
>     FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
> @@ -6022,7 +6023,7 @@ gimple_fold_stmt_to_constant_1 (gimple *
>                   tree val;
>
>                   nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs));
> -                 auto_vec<tree, 32> vec (nelts);
> +                 tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
>                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
>                     {
>                       val = (*valueize) (val);
> @@ -6034,7 +6035,7 @@ gimple_fold_stmt_to_constant_1 (gimple *
>                         return NULL_TREE;
>                     }
>
> -                 return build_vector (TREE_TYPE (rhs), vec);
> +                 return vec.build ();
>                 }
>               if (subcode == OBJ_TYPE_REF)
>                 {
> Index: gcc/tree-ssa-forwprop.c
> ===================================================================
> --- gcc/tree-ssa-forwprop.c     2017-11-29 11:06:31.942215941 +0000
> +++ gcc/tree-ssa-forwprop.c     2017-12-06 14:48:52.888162175 +0000
> @@ -46,6 +46,7 @@ the Free Software Foundation; either ver
>  #include "tree-cfgcleanup.h"
>  #include "cfganal.h"
>  #include "optabs-tree.h"
> +#include "tree-vector-builder.h"
>
>  /* This pass propagates the RHS of assignment statements into use
>     sites of the LHS of the assignment.  It's basically a specialized
> @@ -2116,10 +2117,10 @@ simplify_vector_constructor (gimple_stmt
>           || GET_MODE_SIZE (TYPE_MODE (mask_type))
>              != GET_MODE_SIZE (TYPE_MODE (type)))
>         return false;
> -      auto_vec<tree, 32> mask_elts (nelts);
> +      tree_vector_builder mask_elts (mask_type, nelts, 1);
>        for (i = 0; i < nelts; i++)
>         mask_elts.quick_push (build_int_cst (TREE_TYPE (mask_type), sel[i]));
> -      op2 = build_vector (mask_type, mask_elts);
> +      op2 = mask_elts.build ();
>        if (conv_code == ERROR_MARK)
>         gimple_assign_set_rhs_with_ops (gsi, VEC_PERM_EXPR, orig, orig, op2);
>        else
> Index: gcc/tree-vect-generic.c
> ===================================================================
> --- gcc/tree-vect-generic.c     2017-11-29 11:06:34.810688336 +0000
> +++ gcc/tree-vect-generic.c     2017-12-06 14:48:52.889162134 +0000
> @@ -37,6 +37,7 @@ Free Software Foundation; either version
>  #include "gimplify-me.h"
>  #include "gimplify.h"
>  #include "tree-cfg.h"
> +#include "tree-vector-builder.h"
>
>
>  static void expand_vector_operations_1 (gimple_stmt_iterator *);
> @@ -398,11 +399,10 @@ add_rshift (gimple_stmt_iterator *gsi, t
>    if (op != unknown_optab
>        && optab_handler (op, TYPE_MODE (type)) != CODE_FOR_nothing)
>      {
> -      auto_vec<tree, 32> vec (nunits);
> +      tree_vector_builder vec (type, nunits, 1);
>        for (i = 0; i < nunits; i++)
>         vec.quick_push (build_int_cst (TREE_TYPE (type), shiftcnts[i]));
> -      return gimplify_build2 (gsi, RSHIFT_EXPR, type, op0,
> -                             build_vector (type, vec));
> +      return gimplify_build2 (gsi, RSHIFT_EXPR, type, op0, vec.build ());
>      }
>
>    return NULL_TREE;
> @@ -635,12 +635,12 @@ expand_vector_divmod (gimple_stmt_iterat
>               mask_type = build_same_sized_truth_vector_type (type);
>               zero = build_zero_cst (type);
>               cond = build2 (LT_EXPR, mask_type, op0, zero);
> -             auto_vec<tree, 32> vec (nunits);
> +             tree_vector_builder vec (type, nunits, 1);
>               for (i = 0; i < nunits; i++)
>                 vec.quick_push (build_int_cst (TREE_TYPE (type),
>                                                (HOST_WIDE_INT_1U
>                                                 << shifts[i]) - 1));
> -             cst = build_vector (type, vec);
> +             cst = vec.build ();
>               addend = make_ssa_name (type);
>               stmt = gimple_build_assign (addend, VEC_COND_EXPR, cond,
>                                           cst, zero);
> @@ -674,12 +674,12 @@ expand_vector_divmod (gimple_stmt_iterat
>        else
>         {
>           tree mask;
> -         auto_vec<tree, 32> vec (nunits);
> +         tree_vector_builder vec (type, nunits, 1);
>           for (i = 0; i < nunits; i++)
>             vec.quick_push (build_int_cst (TREE_TYPE (type),
>                                            (HOST_WIDE_INT_1U
>                                             << shifts[i]) - 1));
> -         mask = build_vector (type, vec);
> +         mask = vec.build ();
>           op = optab_for_tree_code (BIT_AND_EXPR, type, optab_default);
>           if (op != unknown_optab
>               && optab_handler (op, TYPE_MODE (type)) != CODE_FOR_nothing)
> @@ -753,10 +753,10 @@ expand_vector_divmod (gimple_stmt_iterat
>        return NULL_TREE;
>      }
>
> -  auto_vec<tree, 32> vec (nunits);
> +  tree_vector_builder vec (type, nunits, 1);
>    for (i = 0; i < nunits; i++)
>      vec.quick_push (build_int_cst (TREE_TYPE (type), mulc[i]));
> -  mulcst = build_vector (type, vec);
> +  mulcst = vec.build ();
>
>    cur_op = gimplify_build2 (gsi, MULT_HIGHPART_EXPR, type, cur_op, mulcst);
>
> @@ -1104,7 +1104,7 @@ optimize_vector_constructor (gimple_stmt
>        }
>    if (all_same)
>      return;
> -  auto_vec<tree, 32> cst (nelts);
> +  tree_vector_builder cst (type, nelts, 1);
>    for (i = 0; i < nelts; i++)
>      {
>        tree this_base = CONSTRUCTOR_ELT (rhs, i)->value;
> @@ -1127,7 +1127,7 @@ optimize_vector_constructor (gimple_stmt
>    g = gimple_build_assign (make_ssa_name (type), rhs);
>    gsi_insert_before (gsi, g, GSI_SAME_STMT);
>    g = gimple_build_assign (lhs, PLUS_EXPR, gimple_assign_lhs (g),
> -                          build_vector (type, cst));
> +                          cst.build ());
>    gsi_replace (gsi, g, false);
>  }
>
> Index: gcc/tree-vect-loop.c
> ===================================================================
> --- gcc/tree-vect-loop.c        2017-11-29 11:06:34.910905236 +0000
> +++ gcc/tree-vect-loop.c        2017-12-06 14:48:52.889162134 +0000
> @@ -51,6 +51,7 @@ Software Foundation; either version 3, o
>  #include "tree-cfg.h"
>  #include "tree-if-conv.h"
>  #include "internal-fn.h"
> +#include "tree-vector-builder.h"
>
>  /* Loop Vectorization Pass.
>
> @@ -4497,10 +4498,10 @@ vect_create_epilog_for_reduction (vec<tr
>          vector size (STEP).  */
>
>        /* Create a {1,2,3,...} vector.  */
> -      auto_vec<tree, 32> vtemp (nunits_out);
> -      for (k = 0; k < nunits_out; ++k)
> +      tree_vector_builder vtemp (cr_index_vector_type, 1, 3);
> +      for (k = 0; k < 3; ++k)
>         vtemp.quick_push (build_int_cst (cr_index_scalar_type, k + 1));
> -      tree series_vect = build_vector (cr_index_vector_type, vtemp);
> +      tree series_vect = vtemp.build ();
>
>        /* Create a vector of the step value.  */
>        tree step = build_int_cst (cr_index_scalar_type, nunits_out);
> Index: gcc/tree-vect-slp.c
> ===================================================================
> --- gcc/tree-vect-slp.c 2017-12-05 14:24:56.178946625 +0000
> +++ gcc/tree-vect-slp.c 2017-12-06 14:48:52.890162093 +0000
> @@ -41,6 +41,7 @@ Software Foundation; either version 3, o
>  #include "langhooks.h"
>  #include "gimple-walk.h"
>  #include "dbgcnt.h"
> +#include "tree-vector-builder.h"
>
>
>  /* Recursively free the memory allocated for the SLP tree rooted at NODE.  */
> @@ -3222,7 +3223,7 @@ vect_get_constant_vectors (tree op, slp_
>
>    number_of_places_left_in_vector = nunits;
>    constant_p = true;
> -  auto_vec<tree, 32> elts (nunits);
> +  tree_vector_builder elts (vector_type, nunits, 1);
>    elts.quick_grow (nunits);
>    bool place_after_defs = false;
>    for (j = 0; j < number_of_copies; j++)
> @@ -3340,7 +3341,7 @@ vect_get_constant_vectors (tree op, slp_
>            if (number_of_places_left_in_vector == 0)
>              {
>               if (constant_p)
> -               vec_cst = build_vector (vector_type, elts);
> +               vec_cst = elts.build ();
>               else
>                 {
>                   vec<constructor_elt, va_gc> *v;
> @@ -3371,6 +3372,8 @@ vect_get_constant_vectors (tree op, slp_
>               place_after_defs = false;
>                number_of_places_left_in_vector = nunits;
>               constant_p = true;
> +             elts.new_vector (vector_type, nunits, 1);
> +             elts.quick_grow (nunits);
>              }
>          }
>      }
> @@ -3667,11 +3670,11 @@ vect_transform_slp_perm_load (slp_tree n
>
>                   if (! noop_p)
>                     {
> -                     auto_vec<tree, 32> mask_elts (nunits);
> +                     tree_vector_builder mask_elts (mask_type, nunits, 1);
>                       for (int l = 0; l < nunits; ++l)
>                         mask_elts.quick_push (build_int_cst (mask_element_type,
>                                                              mask[l]));
> -                     mask_vec = build_vector (mask_type, mask_elts);
> +                     mask_vec = mask_elts.build ();
>                     }
>
>                   if (second_vec_index == -1)
> @@ -3823,7 +3826,7 @@ vect_schedule_slp_instance (slp_tree nod
>           for (j = 0; j < v0.length (); ++j)
>             {
>               unsigned int nunits = TYPE_VECTOR_SUBPARTS (vectype);
> -             auto_vec<tree, 32> melts (nunits);
> +             tree_vector_builder melts (mvectype, nunits, 1);
>               for (l = 0; l < nunits; ++l)
>                 {
>                   if (k >= group_size)
> @@ -3831,7 +3834,7 @@ vect_schedule_slp_instance (slp_tree nod
>                   tree t = build_int_cst (meltype, mask[k++] * nunits + l);
>                   melts.quick_push (t);
>                 }
> -             tmask = build_vector (mvectype, melts);
> +             tmask = melts.build ();
>
>               /* ???  Not all targets support a VEC_PERM_EXPR with a
>                  constant mask that would translate to a vec_merge RTX
> Index: gcc/tree-vect-stmts.c
> ===================================================================
> --- gcc/tree-vect-stmts.c       2017-11-29 11:06:29.970567408 +0000
> +++ gcc/tree-vect-stmts.c       2017-12-06 14:48:52.890162093 +0000
> @@ -48,6 +48,7 @@ Software Foundation; either version 3, o
>  #include "tree-vectorizer.h"
>  #include "builtins.h"
>  #include "internal-fn.h"
> +#include "tree-vector-builder.h"
>
>  /* For lang_hooks.types.type_for_mode.  */
>  #include "langhooks.h"
> @@ -2520,10 +2521,10 @@ vectorizable_bswap (gimple *stmt, gimple
>        return true;
>      }
>
> -  auto_vec<tree, 32> telts (num_bytes);
> +  tree_vector_builder telts (char_vectype, num_bytes, 1);
>    for (unsigned i = 0; i < num_bytes; ++i)
>      telts.quick_push (build_int_cst (char_type_node, elts[i]));
> -  tree bswap_vconst = build_vector (char_vectype, telts);
> +  tree bswap_vconst = telts.build ();
>
>    /* Transform.  */
>    vec<tree> vec_oprnds = vNULL;
> @@ -2941,11 +2942,11 @@ vectorizable_call (gimple *gs, gimple_st
>           if (gimple_call_internal_p (stmt)
>               && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE)
>             {
> -             auto_vec<tree, 32> v (nunits_out);
> -             for (int k = 0; k < nunits_out; ++k)
> +             tree_vector_builder v (vectype_out, 1, 3);
> +             for (int k = 0; k < 3; ++k)
>                 v.quick_push (build_int_cst (unsigned_type_node,
>                                              j * nunits_out + k));
> -             tree cst = build_vector (vectype_out, v);
> +             tree cst = v.build ();
>               tree new_var
>                 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
>               gimple *init_stmt = gimple_build_assign (new_var, cst);
> @@ -6507,7 +6508,7 @@ vectorizable_store (gimple *stmt, gimple
>  tree
>  vect_gen_perm_mask_any (tree vectype, vec_perm_indices sel)
>  {
> -  tree mask_elt_type, mask_type, mask_vec;
> +  tree mask_elt_type, mask_type;
>
>    unsigned int nunits = sel.length ();
>    gcc_checking_assert (nunits == TYPE_VECTOR_SUBPARTS (vectype));
> @@ -6516,12 +6517,10 @@ vect_gen_perm_mask_any (tree vectype, ve
>      (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))).require (), 1);
>    mask_type = get_vectype_for_scalar_type (mask_elt_type);
>
> -  auto_vec<tree, 32> mask_elts (nunits);
> +  tree_vector_builder mask_elts (mask_type, nunits, 1);
>    for (unsigned int i = 0; i < nunits; ++i)
>      mask_elts.quick_push (build_int_cst (mask_elt_type, sel[i]));
> -  mask_vec = build_vector (mask_type, mask_elts);
> -
> -  return mask_vec;
> +  return mask_elts.build ();
>  }
>
>  /* Checked version of vect_gen_perm_mask_any.  Asserts can_vec_perm_p,
> Index: gcc/tree.c
> ===================================================================
> --- gcc/tree.c  2017-12-06 14:46:14.132599861 +0000
> +++ gcc/tree.c  2017-12-06 14:48:52.891162052 +0000
> @@ -1757,7 +1757,7 @@ build_vector_from_ctor (tree type, vec<c
>    unsigned HOST_WIDE_INT idx;
>    tree value;
>
> -  auto_vec<tree, 32> vec (nelts);
> +  tree_vector_builder vec (type, nelts, 1);
>    FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
>      {
>        if (TREE_CODE (value) == VECTOR_CST)
> @@ -1769,7 +1769,7 @@ build_vector_from_ctor (tree type, vec<c
>    while (vec.length () < nelts)
>      vec.quick_push (build_zero_cst (TREE_TYPE (type)));
>
> -  return build_vector (type, vec);
> +  return vec.build ();
>  }
>
>  /* Build a vector of type VECTYPE where all the elements are SCs.  */
> @@ -1792,10 +1792,9 @@ build_vector_from_val (tree vectype, tre
>
>    if (CONSTANT_CLASS_P (sc))
>      {
> -      auto_vec<tree, 32> v (nunits);
> -      for (i = 0; i < nunits; ++i)
> -       v.quick_push (sc);
> -      return build_vector (vectype, v);
> +      tree_vector_builder v (vectype, 1, 1);
> +      v.quick_push (sc);
> +      return v.build ();
>      }
>    else
>      {

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Use tree_vector_builder::new_binary_operation for folding
  2017-12-06 15:24           ` Use tree_vector_builder::new_binary_operation " Richard Sandiford
@ 2017-12-07 11:07             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:07 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:24 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> This patch makes fold-const.c operate directly on the VECTOR_CST
> encoding when folding an operation that has two VECTOR_CST inputs.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard
>
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * tree-vector-builder.h
>         (tree_vector_builder::new_binary_operation): Declare.
>         * tree-vector-builder.c
>         (tree_vector_builder::new_binary_operation): New function.
>         * fold-const.c (fold_relational_const): Use it.
>         (const_binop): Likewise.  Check that both input vectors have
>         the same number of elements, thus excluding things like WIDEN_SUM.
>         Check whether it is possible to operate directly on the encodings
>         of stepped inputs.
>
> Index: gcc/tree-vector-builder.h
> ===================================================================
> --- gcc/tree-vector-builder.h   2017-12-06 14:46:14.131599903 +0000
> +++ gcc/tree-vector-builder.h   2017-12-06 14:49:00.386854068 +0000
> @@ -38,6 +38,7 @@ #define GCC_TREE_VECTOR_BUILDER_H
>
>    void new_vector (tree, unsigned int, unsigned int);
>    bool new_unary_operation (tree, tree, bool);
> +  bool new_binary_operation (tree, tree, tree, bool);
>
>  private:
>    bool equal_p (const_tree, const_tree) const;
> Index: gcc/tree-vector-builder.c
> ===================================================================
> --- gcc/tree-vector-builder.c   2017-12-06 14:46:14.131599903 +0000
> +++ gcc/tree-vector-builder.c   2017-12-06 14:49:00.386854068 +0000
> @@ -49,6 +49,53 @@ tree_vector_builder::new_unary_operation
>    return true;
>  }
>
> +/* Try to start building a new vector of type TYPE that holds the result of
> +   a binary operation on VECTOR_CSTs T1 and T2.  ALLOW_STEPPED_P is true if
> +   the operation can handle stepped encodings directly, without having to
> +   expand the full sequence.
> +
> +   Return true if the operation is possible.  Leave the builder unchanged
> +   otherwise.  */
> +
> +bool
> +tree_vector_builder::new_binary_operation (tree type, tree t1, tree t2,
> +                                          bool allow_stepped_p)
> +{
> +  unsigned int full_nelts = TYPE_VECTOR_SUBPARTS (type);
> +  gcc_assert (full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
> +             && full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)));
> +  /* Conceptually we split the patterns in T1 and T2 until we have
> +     an equal number for both.  Each split pattern requires the same
> +     number of elements per pattern as the original.  E.g. splitting:
> +
> +       { 1, 2, 3, ... }
> +
> +     into two gives:
> +
> +       { 1, 3, 5, ... }
> +       { 2, 4, 6, ... }
> +
> +     while splitting:
> +
> +       { 1, 0, ... }
> +
> +     into two gives:
> +
> +       { 1, 0, ... }
> +       { 0, 0, ... }.  */
> +  unsigned int npatterns = least_common_multiple (VECTOR_CST_NPATTERNS (t1),
> +                                                 VECTOR_CST_NPATTERNS (t2));
> +  unsigned int nelts_per_pattern = MAX (VECTOR_CST_NELTS_PER_PATTERN (t1),
> +                                       VECTOR_CST_NELTS_PER_PATTERN (t2));
> +  if (!allow_stepped_p && nelts_per_pattern > 2)
> +    {
> +      npatterns = full_nelts;
> +      nelts_per_pattern = 1;
> +    }
> +  new_vector (type, npatterns, nelts_per_pattern);
> +  return true;
> +}
> +
>  /* Return a VECTOR_CST for the current constant.  */
>
>  tree
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-12-06 14:48:56.997993407 +0000
> +++ gcc/fold-const.c    2017-12-06 14:49:00.386854068 +0000
> @@ -1435,13 +1435,40 @@ const_binop (enum tree_code code, tree a
>      }
>
>    if (TREE_CODE (arg1) == VECTOR_CST
> -      && TREE_CODE (arg2) == VECTOR_CST)
> +      && TREE_CODE (arg2) == VECTOR_CST
> +      && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))
> +         == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
>      {
>        tree type = TREE_TYPE (arg1);
> -      int count = VECTOR_CST_NELTS (arg1), i;
> +      bool step_ok_p;
> +      if (VECTOR_CST_STEPPED_P (arg1)
> +         && VECTOR_CST_STEPPED_P (arg2))
> +       /* We can operate directly on the encoding if:
> +
> +             a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
> +           implies
> +             (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
> +
> +          Addition and subtraction are the supported operators
> +          for which this is true.  */
> +       step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
> +      else if (VECTOR_CST_STEPPED_P (arg1))
> +       /* We can operate directly on stepped encodings if:
> +
> +            a3 - a2 == a2 - a1
> +          implies:
> +            (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
>
> -      auto_vec<tree, 32> elts (count);
> -      for (i = 0; i < count; i++)
> +          which is true if (x -> x op c) distributes over addition.  */
> +       step_ok_p = distributes_over_addition_p (code, 1);
> +      else
> +       /* Similarly in reverse.  */
> +       step_ok_p = distributes_over_addition_p (code, 2);
> +      tree_vector_builder elts;
> +      if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
> +       return NULL_TREE;
> +      unsigned int count = elts.encoded_nelts ();
> +      for (unsigned int i = 0; i < count; ++i)
>         {
>           tree elem1 = VECTOR_CST_ELT (arg1, i);
>           tree elem2 = VECTOR_CST_ELT (arg2, i);
> @@ -1455,7 +1482,7 @@ const_binop (enum tree_code code, tree a
>           elts.quick_push (elt);
>         }
>
> -      return build_vector (type, elts);
> +      return elts.build ();
>      }
>
>    /* Shifts allow a scalar offset for a vector.  */
> @@ -13770,11 +13797,10 @@ fold_relational_const (enum tree_code co
>             }
>           return constant_boolean_node (true, type);
>         }
> -      unsigned count = VECTOR_CST_NELTS (op0);
> -      gcc_assert (VECTOR_CST_NELTS (op1) == count
> -                 && TYPE_VECTOR_SUBPARTS (type) == count);
> -
> -      auto_vec<tree, 32> elts (count);
> +      tree_vector_builder elts;
> +      if (!elts.new_binary_operation (type, op0, op1, false))
> +       return NULL_TREE;
> +      unsigned int count = elts.encoded_nelts ();
>        for (unsigned i = 0; i < count; i++)
>         {
>           tree elem_type = TREE_TYPE (type);
> @@ -13791,7 +13817,7 @@ fold_relational_const (enum tree_code co
>                                           integer_zerop (tem) ? 0 : -1));
>         }
>
> -      return build_vector (type, elts);
> +      return elts.build ();
>      }
>
>    /* From here on we only handle LT, LE, GT, GE, EQ and NE.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Make gimple_build_vector take a tree_vector_builder
  2017-12-06 15:25           ` Make gimple_build_vector take a tree_vector_builder Richard Sandiford
@ 2017-12-07 11:08             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:08 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:25 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> This patch changes gimple_build_vector so that it takes a
> tree_vector_builder instead of a size and a vector of trees.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard
>
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * vector-builder.h (vector_builder::derived): New const overload.
>         (vector_builder::elt): New function.
>         * tree-vector-builder.h (tree_vector_builder::type): New function.
>         (tree_vector_builder::apply_step): Declare.
>         * tree-vector-builder.c (tree_vector_builder::apply_step): New
>         function.
>         * gimple-fold.h (tree_vector_builder): Declare.
>         (gimple_build_vector): Take a tree_vector_builder instead of a
>         type and vector of elements.
>         * gimple-fold.c (gimple_build_vector): Likewise.
>         * tree-vect-loop.c (get_initial_def_for_reduction): Update call
>         accordingly.
>         (get_initial_defs_for_reduction): Likewise.
>         (vectorizable_induction): Likewise.
>
> Index: gcc/vector-builder.h
> ===================================================================
> --- gcc/vector-builder.h        2017-12-06 14:46:14.133599820 +0000
> +++ gcc/vector-builder.h        2017-12-06 14:49:04.289693414 +0000
> @@ -68,6 +68,10 @@ #define GCC_VECTOR_BUILDER_H
>           given integral_p (ELT1) && integral_p (ELT2).  There is no fixed
>           choice of StepType.
>
> +      T apply_step (T base, unsigned int factor, StepType step) const;
> +
> +         Return a vector element with the value BASE + FACTOR * STEP.
> +
>        bool can_elide_p (T elt) const;
>
>           Return true if we can drop element ELT, even if the retained
> @@ -91,6 +95,7 @@ #define GCC_VECTOR_BUILDER_H
>    unsigned int nelts_per_pattern () const { return m_nelts_per_pattern; }
>    unsigned int encoded_nelts () const;
>    bool encoded_full_vector_p () const;
> +  T elt (unsigned int) const;
>
>    void finalize ();
>
> @@ -163,6 +168,38 @@ vector_builder<T, Derived>::new_vector (
>    this->truncate (0);
>  }
>
> +/* Return the value of vector element I, which might or might not be
> +   encoded explicitly.  */
> +
> +template<typename T, typename Derived>
> +T
> +vector_builder<T, Derived>::elt (unsigned int i) const
> +{
> +  /* This only makes sense if the encoding has been fully populated.  */
> +  gcc_checking_assert (encoded_nelts () <= this->length ());
> +
> +  /* First handle elements that are already present in the underlying
> +     vector, regardless of whether they're part of the encoding or not.  */
> +  if (i < this->length ())
> +    return (*this)[i];
> +
> +  /* Identify the pattern that contains element I and work out the index of
> +     the last encoded element for that pattern.  */
> +  unsigned int pattern = i % m_npatterns;
> +  unsigned int count = i / m_npatterns;
> +  unsigned int final_i = encoded_nelts () - m_npatterns + pattern;
> +  T final = (*this)[final_i];
> +
> +  /* If there are no steps, the final encoded value is the right one.  */
> +  if (m_nelts_per_pattern <= 2)
> +    return final;
> +
> +  /* Otherwise work out the value from the last two encoded elements.  */
> +  T prev = (*this)[final_i - m_npatterns];
> +  return derived ()->apply_step (final, count - 2,
> +                                derived ()->step (prev, final));
> +}
> +
>  /* Change the encoding to NPATTERNS patterns of NELTS_PER_PATTERN each,
>     but without changing the underlying vector.  */
>
> Index: gcc/tree-vector-builder.h
> ===================================================================
> --- gcc/tree-vector-builder.h   2017-12-06 14:49:00.386854068 +0000
> +++ gcc/tree-vector-builder.h   2017-12-06 14:49:04.289693414 +0000
> @@ -45,6 +45,7 @@ #define GCC_TREE_VECTOR_BUILDER_H
>    bool allow_steps_p () const;
>    bool integral_p (const_tree) const;
>    wide_int step (const_tree, const_tree) const;
> +  tree apply_step (tree, unsigned int, const wide_int &) const;
>    bool can_elide_p (const_tree) const;
>    void note_representative (tree *, tree);
>
> Index: gcc/tree-vector-builder.c
> ===================================================================
> --- gcc/tree-vector-builder.c   2017-12-06 14:49:00.386854068 +0000
> +++ gcc/tree-vector-builder.c   2017-12-06 14:49:04.289693414 +0000
> @@ -96,6 +96,16 @@ tree_vector_builder::new_binary_operatio
>    return true;
>  }
>
> +/* Return a vector element with the value BASE + FACTOR * STEP.  */
> +
> +tree
> +tree_vector_builder::apply_step (tree base, unsigned int factor,
> +                                const wide_int &step) const
> +{
> +  return wide_int_to_tree (TREE_TYPE (base),
> +                          wi::to_wide (base) + factor * step);
> +}
> +
>  /* Return a VECTOR_CST for the current constant.  */
>
>  tree
> Index: gcc/gimple-fold.h
> ===================================================================
> --- gcc/gimple-fold.h   2017-11-29 11:06:33.003002371 +0000
> +++ gcc/gimple-fold.h   2017-12-06 14:49:04.288693455 +0000
> @@ -135,11 +135,13 @@ gimple_build_vector_from_val (gimple_seq
>    return gimple_build_vector_from_val (seq, UNKNOWN_LOCATION, type, op);
>  }
>
> -extern tree gimple_build_vector (gimple_seq *, location_t, tree, vec<tree>);
> +class tree_vector_builder;
> +extern tree gimple_build_vector (gimple_seq *, location_t,
> +                                tree_vector_builder *);
>  inline tree
> -gimple_build_vector (gimple_seq *seq, tree type, vec<tree> elts)
> +gimple_build_vector (gimple_seq *seq, tree_vector_builder *builder)
>  {
> -  return gimple_build_vector (seq, UNKNOWN_LOCATION, type, elts);
> +  return gimple_build_vector (seq, UNKNOWN_LOCATION, builder);
>  }
>
>  extern bool gimple_stmt_nonnegative_warnv_p (gimple *, bool *, int = 0);
> Index: gcc/gimple-fold.c
> ===================================================================
> --- gcc/gimple-fold.c   2017-12-06 14:48:52.888162175 +0000
> +++ gcc/gimple-fold.c   2017-12-06 14:49:04.288693455 +0000
> @@ -7178,23 +7178,30 @@ gimple_build_vector_from_val (gimple_seq
>    return res;
>  }
>
> -/* Build a vector of type TYPE in which the elements have the values
> -   given by ELTS.  Return a gimple value for the result, appending any
> -   new instructions to SEQ.  */
> +/* Build a vector from BUILDER, handling the case in which some elements
> +   are non-constant.  Return a gimple value for the result, appending any
> +   new instructions to SEQ.
> +
> +   BUILDER must not have a stepped encoding on entry.  This is because
> +   the function is not geared up to handle the arithmetic that would
> +   be needed in the variable case, and any code building a vector that
> +   is known to be constant should use BUILDER->build () directly.  */
>
>  tree
> -gimple_build_vector (gimple_seq *seq, location_t loc, tree type,
> -                    vec<tree> elts)
> +gimple_build_vector (gimple_seq *seq, location_t loc,
> +                    tree_vector_builder *builder)
>  {
> -  unsigned int nelts = elts.length ();
> -  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
> -  for (unsigned int i = 0; i < nelts; ++i)
> -    if (!TREE_CONSTANT (elts[i]))
> +  gcc_assert (builder->nelts_per_pattern () <= 2);
> +  unsigned int encoded_nelts = builder->encoded_nelts ();
> +  for (unsigned int i = 0; i < encoded_nelts; ++i)
> +    if (!TREE_CONSTANT ((*builder)[i]))
>        {
> +       tree type = builder->type ();
> +       unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
>         vec<constructor_elt, va_gc> *v;
>         vec_alloc (v, nelts);
>         for (i = 0; i < nelts; ++i)
> -         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
> +         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
>
>         tree res;
>         if (gimple_in_ssa_p (cfun))
> @@ -7206,7 +7213,7 @@ gimple_build_vector (gimple_seq *seq, lo
>         gimple_seq_add_stmt_without_update (seq, stmt);
>         return res;
>        }
> -  return build_vector (type, elts);
> +  return builder->build ();
>  }
>
>  /* Return true if the result of assignment STMT is known to be non-negative.
> Index: gcc/tree-vect-loop.c
> ===================================================================
> --- gcc/tree-vect-loop.c        2017-12-06 14:48:52.889162134 +0000
> +++ gcc/tree-vect-loop.c        2017-12-06 14:49:04.289693414 +0000
> @@ -3968,11 +3968,9 @@ get_initial_def_for_reduction (gimple *s
>    struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
>    tree scalar_type = TREE_TYPE (init_val);
>    tree vectype = get_vectype_for_scalar_type (scalar_type);
> -  int nunits;
>    enum tree_code code = gimple_assign_rhs_code (stmt);
>    tree def_for_init;
>    tree init_def;
> -  int i;
>    bool nested_in_vect_loop = false;
>    REAL_VALUE_TYPE real_init_val = dconst0;
>    int int_init_val = 0;
> @@ -3980,7 +3978,6 @@ get_initial_def_for_reduction (gimple *s
>    gimple_seq stmts = NULL;
>
>    gcc_assert (vectype);
> -  nunits = TYPE_VECTOR_SUBPARTS (vectype);
>
>    gcc_assert (POINTER_TYPE_P (scalar_type) || INTEGRAL_TYPE_P (scalar_type)
>               || SCALAR_FLOAT_TYPE_P (scalar_type));
> @@ -4053,11 +4050,10 @@ get_initial_def_for_reduction (gimple *s
>         else
>           {
>             /* Option2: the first element is INIT_VAL.  */
> -           auto_vec<tree, 32> elts (nunits);
> +           tree_vector_builder elts (vectype, 1, 2);
>             elts.quick_push (init_val);
> -           for (i = 1; i < nunits; ++i)
> -             elts.quick_push (def_for_init);
> -           init_def = gimple_build_vector (&stmts, vectype, elts);
> +           elts.quick_push (def_for_init);
> +           init_def = gimple_build_vector (&stmts, &elts);
>           }
>        }
>        break;
> @@ -4182,7 +4178,7 @@ get_initial_defs_for_reduction (slp_tree
>    number_of_copies = nunits * number_of_vectors / group_size;
>
>    number_of_places_left_in_vector = nunits;
> -  auto_vec<tree, 32> elts (nunits);
> +  tree_vector_builder elts (vector_type, nunits, 1);
>    elts.quick_grow (nunits);
>    for (j = 0; j < number_of_copies; j++)
>      {
> @@ -4205,12 +4201,14 @@ get_initial_defs_for_reduction (slp_tree
>            if (number_of_places_left_in_vector == 0)
>              {
>               gimple_seq ctor_seq = NULL;
> -             tree init = gimple_build_vector (&ctor_seq, vector_type, elts);
> +             tree init = gimple_build_vector (&ctor_seq, &elts);
>               if (ctor_seq != NULL)
>                 gsi_insert_seq_on_edge_immediate (pe, ctor_seq);
>               voprnds.quick_push (init);
>
>                number_of_places_left_in_vector = nunits;
> +             elts.new_vector (vector_type, nunits, 1);
> +             elts.quick_grow (nunits);
>              }
>          }
>      }
> @@ -6698,7 +6696,7 @@ vectorizable_induction (gimple *phi,
>        unsigned ivn;
>        for (ivn = 0; ivn < nivs; ++ivn)
>         {
> -         auto_vec<tree, 32> elts (nunits);
> +         tree_vector_builder elts (vectype, nunits, 1);
>           stmts = NULL;
>           for (unsigned eltn = 0; eltn < nunits; ++eltn)
>             {
> @@ -6708,7 +6706,7 @@ vectorizable_induction (gimple *phi,
>                                     elt, step_expr);
>               elts.quick_push (elt);
>             }
> -         vec_init = gimple_build_vector (&stmts, vectype, elts);
> +         vec_init = gimple_build_vector (&stmts, &elts);
>           if (stmts)
>             {
>               new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
> @@ -6815,7 +6813,7 @@ vectorizable_induction (gimple *phi,
>        stmts = NULL;
>        new_name = gimple_convert (&stmts, TREE_TYPE (vectype), init_expr);
>
> -      auto_vec<tree, 32> elts (nunits);
> +      tree_vector_builder elts (vectype, nunits, 1);
>        elts.quick_push (new_name);
>        for (i = 1; i < nunits; i++)
>         {
> @@ -6826,7 +6824,7 @@ vectorizable_induction (gimple *phi,
>         }
>        /* Create a vector from [new_name_0, new_name_1, ...,
>          new_name_nunits-1]  */
> -      vec_init = gimple_build_vector (&stmts, vectype, elts);
> +      vec_init = gimple_build_vector (&stmts, &elts);
>        if (stmts)
>         {
>           new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Make build_vector static
  2017-12-06 15:26           ` Make build_vector static Richard Sandiford
@ 2017-12-07 11:09             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:09 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:26 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> After the previous patches, the only remaining uses of build_vector are
> in the selftests in tree.c.  This patch makes it static and moves it to
> the selftest part of the file.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard
>
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * tree.c (build_vector): Delete.
>         * tree.h (build_vector): Make static and move into the self-testing
>         block.
>
> Index: gcc/tree.c
> ===================================================================
> --- gcc/tree.c  2017-12-06 14:48:52.891162052 +0000
> +++ gcc/tree.c  2017-12-06 14:49:10.295445836 +0000
> @@ -1736,18 +1736,6 @@ make_vector (unsigned log2_npatterns,
>  }
>
>  /* Return a new VECTOR_CST node whose type is TYPE and whose values
> -   are given by VALS.  */
> -
> -tree
> -build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
> -{
> -  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
> -  tree_vector_builder builder (type, vals.length (), 1);
> -  builder.splice (vals);
> -  return builder.build ();
> -}
> -
> -/* Return a new VECTOR_CST node whose type is TYPE and whose values
>     are extracted from V, a vector of CONSTRUCTOR_ELT.  */
>
>  tree
> @@ -14066,6 +14054,18 @@ test_labels ()
>    ASSERT_FALSE (FORCED_LABEL (label_decl));
>  }
>
> +/* Return a new VECTOR_CST node whose type is TYPE and whose values
> +   are given by VALS.  */
> +
> +static tree
> +build_vector (tree type, vec<tree> vals MEM_STAT_DECL)
> +{
> +  gcc_assert (vals.length () == TYPE_VECTOR_SUBPARTS (type));
> +  tree_vector_builder builder (type, vals.length (), 1);
> +  builder.splice (vals);
> +  return builder.build ();
> +}
> +
>  /* Check that VECTOR_CST ACTUAL contains the elements in EXPECTED.  */
>
>  static void
> Index: gcc/tree.h
> ===================================================================
> --- gcc/tree.h  2017-12-06 14:46:14.133599820 +0000
> +++ gcc/tree.h  2017-12-06 14:49:10.296445794 +0000
> @@ -4044,7 +4044,6 @@ extern tree build_int_cst (tree, HOST_WI
>  extern tree build_int_cstu (tree type, unsigned HOST_WIDE_INT cst);
>  extern tree build_int_cst_type (tree, HOST_WIDE_INT);
>  extern tree make_vector (unsigned, unsigned CXX_MEM_STAT_INFO);
> -extern tree build_vector (tree, vec<tree> CXX_MEM_STAT_INFO);
>  extern tree build_vector_from_ctor (tree, vec<constructor_elt, va_gc> *);
>  extern tree build_vector_from_val (tree, tree);
>  extern void recompute_constructor_flags (tree);

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: Make more use of VECTOR_CST_ENCODED_ELT
  2017-12-06 15:28           ` Make more use of VECTOR_CST_ENCODED_ELT Richard Sandiford
@ 2017-12-07 11:12             ` Richard Biener
  0 siblings, 0 replies; 24+ messages in thread
From: Richard Biener @ 2017-12-07 11:12 UTC (permalink / raw)
  To: Richard Biener, GCC Patches, Richard Sandiford

On Wed, Dec 6, 2017 at 4:28 PM, Richard Sandiford
<richard.sandiford@linaro.org> wrote:
> This patch makes various bits of code operate directly on the new
> VECTOR_CST encoding, instead of using VECTOR_CST_ELT on all elements
> of the vector.
>
> Previous patches handled operations that produce a new VECTOR_CST,
> while this patch handles things like predicates.  It also makes
> print_node dump the encoding instead of the full vector that
> the encoding represents.
>
> Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu.
> Also spot-checked on sparc64-linux-gnu.  OK to install?

Ok.

Richard.

> Thanks,
> Richard
>
>
> 2017-12-06  Richard Sandiford  <richard.sandiford@linaro.org>
>
> gcc/
>         * tree-vector-builder.h
>         (tree_vector_builder::binary_encoded_nelts): Declare.
>         * tree-vector-builder.c
>         (tree_vector_builder::binary_encoded_nelts): New function.
>         * fold-const.c (negate_expr_p): Likewise.
>         (operand_equal_p, fold_checksum_tree): Likewise.
>         * tree-loop-distribution.c (const_with_all_bytes_same): Likewise.
>         * tree.c (integer_zerop, integer_onep, integer_all_onesp, real_zerop)
>         (real_onep, real_minus_onep, add_expr, initializer_zerop): Likewise.
>         (uniform_vector_p): Likewise.
>         * varasm.c (const_hash_1, compare_constant): Likewise.
>         * tree-ssa-ccp.c: Include tree-vector-builder.h.
>         (valid_lattice_transition): Operate directly on the VECTOR_CST
>         encoding.
>         * ipa-icf.c: Include tree-vector-builder.h.
>         (sem_variable::equals): Operate directly on the VECTOR_CST encoding.
>         * print-tree.c (print_node): Print encoding of VECTOR_CSTs.
>
> Index: gcc/tree-vector-builder.h
> ===================================================================
> --- gcc/tree-vector-builder.h   2017-12-06 14:49:04.289693414 +0000
> +++ gcc/tree-vector-builder.h   2017-12-06 14:50:45.559564436 +0000
> @@ -40,6 +40,8 @@ #define GCC_TREE_VECTOR_BUILDER_H
>    bool new_unary_operation (tree, tree, bool);
>    bool new_binary_operation (tree, tree, tree, bool);
>
> +  static unsigned int binary_encoded_nelts (tree, tree);
> +
>  private:
>    bool equal_p (const_tree, const_tree) const;
>    bool allow_steps_p () const;
> Index: gcc/tree-vector-builder.c
> ===================================================================
> --- gcc/tree-vector-builder.c   2017-12-06 14:49:04.289693414 +0000
> +++ gcc/tree-vector-builder.c   2017-12-06 14:50:45.558564477 +0000
> @@ -96,6 +96,24 @@ tree_vector_builder::new_binary_operatio
>    return true;
>  }
>
> +/* Return the number of elements that the caller needs to operate on in
> +   order to handle a binary operation on VECTOR_CSTs T1 and T2.  This static
> +   function is used instead of new_binary_operation if the result of the
> +   operation is not a VECTOR_CST.  */
> +
> +unsigned int
> +tree_vector_builder::binary_encoded_nelts (tree t1, tree t2)
> +{
> +  unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1));
> +  gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)));
> +  /* See new_binary_operation for details.  */
> +  unsigned int npatterns = least_common_multiple (VECTOR_CST_NPATTERNS (t1),
> +                                                 VECTOR_CST_NPATTERNS (t2));
> +  unsigned int nelts_per_pattern = MAX (VECTOR_CST_NELTS_PER_PATTERN (t1),
> +                                       VECTOR_CST_NELTS_PER_PATTERN (t2));
> +  return MIN (npatterns * nelts_per_pattern, nelts);
> +}
> +
>  /* Return a vector element with the value BASE + FACTOR * STEP.  */
>
>  tree
> Index: gcc/fold-const.c
> ===================================================================
> --- gcc/fold-const.c    2017-12-06 14:49:00.386854068 +0000
> +++ gcc/fold-const.c    2017-12-06 14:50:45.557564518 +0000
> @@ -410,10 +410,10 @@ negate_expr_p (tree t)
>         if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
>           return true;
>
> -       int count = VECTOR_CST_NELTS (t), i;
> -
> -       for (i = 0; i < count; i++)
> -         if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
> +       /* Steps don't prevent negation.  */
> +       unsigned int count = vector_cst_encoded_nelts (t);
> +       for (unsigned int i = 0; i < count; ++i)
> +         if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
>             return false;
>
>         return true;
> @@ -2981,17 +2981,19 @@ operand_equal_p (const_tree arg0, const_
>
>        case VECTOR_CST:
>         {
> -         unsigned i;
> +         if (VECTOR_CST_LOG2_NPATTERNS (arg0)
> +             != VECTOR_CST_LOG2_NPATTERNS (arg1))
> +           return 0;
>
> -         if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
> +         if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
> +             != VECTOR_CST_NELTS_PER_PATTERN (arg1))
>             return 0;
>
> -         for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
> -           {
> -             if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
> -                                   VECTOR_CST_ELT (arg1, i), flags))
> -               return 0;
> -           }
> +         unsigned int count = vector_cst_encoded_nelts (arg0);
> +         for (unsigned int i = 0; i < count; ++i)
> +           if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
> +                                 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
> +             return 0;
>           return 1;
>         }
>
> @@ -11992,8 +11994,9 @@ fold_checksum_tree (const_tree expr, str
>           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
>           break;
>         case VECTOR_CST:
> -         for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
> -           fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
> +         len = vector_cst_encoded_nelts (expr);
> +         for (i = 0; i < len; ++i)
> +           fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
>           break;
>         default:
>           break;
> Index: gcc/tree-loop-distribution.c
> ===================================================================
> --- gcc/tree-loop-distribution.c        2017-11-29 11:06:34.810688336 +0000
> +++ gcc/tree-loop-distribution.c        2017-12-06 14:50:45.558564477 +0000
> @@ -944,13 +944,16 @@ const_with_all_bytes_same (tree val)
>             return 0;
>           break;
>         case VECTOR_CST:
> -         unsigned int j;
> -         for (j = 0; j < VECTOR_CST_NELTS (val); ++j)
> -           if (const_with_all_bytes_same (VECTOR_CST_ELT (val, j)))
> -             break;
> -         if (j == VECTOR_CST_NELTS (val))
> -           return 0;
> -         break;
> +         {
> +           unsigned int count = vector_cst_encoded_nelts (val);
> +           unsigned int j;
> +           for (j = 0; j < count; ++j)
> +             if (const_with_all_bytes_same (VECTOR_CST_ENCODED_ELT (val, j)))
> +               break;
> +           if (j == count)
> +             return 0;
> +           break;
> +         }
>         default:
>           break;
>         }
> Index: gcc/tree.c
> ===================================================================
> --- gcc/tree.c  2017-12-06 14:49:10.295445836 +0000
> +++ gcc/tree.c  2017-12-06 14:50:45.560564396 +0000
> @@ -2338,13 +2338,9 @@ integer_zerop (const_tree expr)
>        return (integer_zerop (TREE_REALPART (expr))
>               && integer_zerop (TREE_IMAGPART (expr)));
>      case VECTOR_CST:
> -      {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -         if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
> -           return false;
> -       return true;
> -      }
> +      return (VECTOR_CST_NPATTERNS (expr) == 1
> +             && VECTOR_CST_DUPLICATE_P (expr)
> +             && integer_zerop (VECTOR_CST_ENCODED_ELT (expr, 0)));
>      default:
>        return false;
>      }
> @@ -2364,13 +2360,9 @@ integer_onep (const_tree expr)
>        return (integer_onep (TREE_REALPART (expr))
>               && integer_zerop (TREE_IMAGPART (expr)));
>      case VECTOR_CST:
> -      {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -         if (!integer_onep (VECTOR_CST_ELT (expr, i)))
> -           return false;
> -       return true;
> -      }
> +      return (VECTOR_CST_NPATTERNS (expr) == 1
> +             && VECTOR_CST_DUPLICATE_P (expr)
> +             && integer_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
>      default:
>        return false;
>      }
> @@ -2401,13 +2393,9 @@ integer_all_onesp (const_tree expr)
>      return 1;
>
>    else if (TREE_CODE (expr) == VECTOR_CST)
> -    {
> -      unsigned i;
> -      for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -       if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
> -         return 0;
> -      return 1;
> -    }
> +    return (VECTOR_CST_NPATTERNS (expr) == 1
> +           && VECTOR_CST_DUPLICATE_P (expr)
> +           && integer_all_onesp (VECTOR_CST_ENCODED_ELT (expr, 0)));
>
>    else if (TREE_CODE (expr) != INTEGER_CST)
>      return 0;
> @@ -2630,9 +2618,11 @@ real_zerop (const_tree expr)
>              && real_zerop (TREE_IMAGPART (expr));
>      case VECTOR_CST:
>        {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -         if (!real_zerop (VECTOR_CST_ELT (expr, i)))
> +       /* Don't simply check for a duplicate because the predicate
> +          accepts both +0.0 and -0.0.  */
> +       unsigned count = vector_cst_encoded_nelts (expr);
> +       for (unsigned int i = 0; i < count; ++i)
> +         if (!real_zerop (VECTOR_CST_ENCODED_ELT (expr, i)))
>             return false;
>         return true;
>        }
> @@ -2657,13 +2647,9 @@ real_onep (const_tree expr)
>        return real_onep (TREE_REALPART (expr))
>              && real_zerop (TREE_IMAGPART (expr));
>      case VECTOR_CST:
> -      {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -         if (!real_onep (VECTOR_CST_ELT (expr, i)))
> -           return false;
> -       return true;
> -      }
> +      return (VECTOR_CST_NPATTERNS (expr) == 1
> +             && VECTOR_CST_DUPLICATE_P (expr)
> +             && real_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
>      default:
>        return false;
>      }
> @@ -2684,13 +2670,9 @@ real_minus_onep (const_tree expr)
>        return real_minus_onep (TREE_REALPART (expr))
>              && real_zerop (TREE_IMAGPART (expr));
>      case VECTOR_CST:
> -      {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
> -         if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
> -           return false;
> -       return true;
> -      }
> +      return (VECTOR_CST_NPATTERNS (expr) == 1
> +             && VECTOR_CST_DUPLICATE_P (expr)
> +             && real_minus_onep (VECTOR_CST_ENCODED_ELT (expr, 0)));
>      default:
>        return false;
>      }
> @@ -7102,9 +7084,11 @@ add_expr (const_tree t, inchash::hash &h
>        return;
>      case VECTOR_CST:
>        {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
> -         inchash::add_expr (VECTOR_CST_ELT (t, i), hstate, flags);
> +       hstate.add_int (VECTOR_CST_NPATTERNS (t));
> +       hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
> +       unsigned int count = vector_cst_encoded_nelts (t);
> +       for (unsigned int i = 0; i < count; ++i)
> +         inchash::add_expr (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
>         return;
>        }
>      case SSA_NAME:
> @@ -10431,13 +10415,9 @@ initializer_zerop (const_tree init)
>             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
>
>      case VECTOR_CST:
> -      {
> -       unsigned i;
> -       for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
> -         if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
> -           return false;
> -       return true;
> -      }
> +      return (VECTOR_CST_NPATTERNS (init) == 1
> +             && VECTOR_CST_DUPLICATE_P (init)
> +             && initializer_zerop (VECTOR_CST_ENCODED_ELT (init, 0)));
>
>      case CONSTRUCTOR:
>        {
> @@ -10486,12 +10466,9 @@ uniform_vector_p (const_tree vec)
>
>    if (TREE_CODE (vec) == VECTOR_CST)
>      {
> -      first = VECTOR_CST_ELT (vec, 0);
> -      for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
> -       if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
> -         return NULL_TREE;
> -
> -      return first;
> +      if (VECTOR_CST_NPATTERNS (vec) == 1 && VECTOR_CST_DUPLICATE_P (vec))
> +       return VECTOR_CST_ENCODED_ELT (vec, 0);
> +      return NULL_TREE;
>      }
>
>    else if (TREE_CODE (vec) == CONSTRUCTOR)
> Index: gcc/varasm.c
> ===================================================================
> --- gcc/varasm.c        2017-12-05 14:24:56.163946903 +0000
> +++ gcc/varasm.c        2017-12-06 14:50:45.560564396 +0000
> @@ -3007,13 +3007,11 @@ const_hash_1 (const tree exp)
>
>      case VECTOR_CST:
>        {
> -       unsigned i;
> -
> -       hi = 7 + VECTOR_CST_NELTS (exp);
> -
> -       for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
> -         hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
> -
> +       hi = 7 + VECTOR_CST_NPATTERNS (exp);
> +       hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
> +       unsigned int count = vector_cst_encoded_nelts (exp);
> +       for (unsigned int i = 0; i < count; ++i)
> +         hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
>         return hi;
>        }
>
> @@ -3151,14 +3149,18 @@ compare_constant (const tree t1, const t
>
>      case VECTOR_CST:
>        {
> -       unsigned i;
> +       if (VECTOR_CST_NPATTERNS (t1)
> +           != VECTOR_CST_NPATTERNS (t2))
> +         return 0;
>
> -        if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
> +       if (VECTOR_CST_NELTS_PER_PATTERN (t1)
> +           != VECTOR_CST_NELTS_PER_PATTERN (t2))
>           return 0;
>
> -       for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
> -         if (!compare_constant (VECTOR_CST_ELT (t1, i),
> -                                VECTOR_CST_ELT (t2, i)))
> +       unsigned int count = vector_cst_encoded_nelts (t1);
> +       for (unsigned int i = 0; i < count; ++i)
> +         if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
> +                                VECTOR_CST_ENCODED_ELT (t2, i)))
>             return 0;
>
>         return 1;
> Index: gcc/tree-ssa-ccp.c
> ===================================================================
> --- gcc/tree-ssa-ccp.c  2017-12-05 14:24:55.073967105 +0000
> +++ gcc/tree-ssa-ccp.c  2017-12-06 14:50:45.558564477 +0000
> @@ -147,6 +147,7 @@ Free Software Foundation; either version
>  #include "diagnostic-core.h"
>  #include "stringpool.h"
>  #include "attribs.h"
> +#include "tree-vector-builder.h"
>
>  /* Possible lattice values.  */
>  typedef enum
> @@ -465,11 +466,14 @@ valid_lattice_transition (ccp_prop_value
>    else if (VECTOR_FLOAT_TYPE_P (type)
>            && !HONOR_NANS (type))
>      {
> -      for (unsigned i = 0; i < VECTOR_CST_NELTS (old_val.value); ++i)
> +      unsigned int count
> +       = tree_vector_builder::binary_encoded_nelts (old_val.value,
> +                                                    new_val.value);
> +      for (unsigned int i = 0; i < count; ++i)
>         if (!REAL_VALUE_ISNAN
> -              (TREE_REAL_CST (VECTOR_CST_ELT (old_val.value, i)))
> -           && !operand_equal_p (VECTOR_CST_ELT (old_val.value, i),
> -                                VECTOR_CST_ELT (new_val.value, i), 0))
> +              (TREE_REAL_CST (VECTOR_CST_ENCODED_ELT (old_val.value, i)))
> +           && !operand_equal_p (VECTOR_CST_ENCODED_ELT (old_val.value, i),
> +                                VECTOR_CST_ENCODED_ELT (new_val.value, i), 0))
>           return false;
>        return true;
>      }
> Index: gcc/ipa-icf.c
> ===================================================================
> --- gcc/ipa-icf.c       2017-11-29 11:06:34.810688336 +0000
> +++ gcc/ipa-icf.c       2017-12-06 14:50:45.557564518 +0000
> @@ -83,6 +83,7 @@ #define INCLUDE_LIST
>  #include "ipa-icf.h"
>  #include "stor-layout.h"
>  #include "dbgcnt.h"
> +#include "tree-vector-builder.h"
>
>  using namespace ipa_icf_gimple;
>
> @@ -2024,17 +2025,17 @@ sem_variable::equals (tree t1, tree t2)
>                                                 &TREE_REAL_CST (t2)));
>      case VECTOR_CST:
>        {
> -       unsigned i;
> -
>          if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
>            return return_false_with_msg ("VECTOR_CST nelts mismatch");
>
> -       for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
> -         if (!sem_variable::equals (VECTOR_CST_ELT (t1, i),
> -                                    VECTOR_CST_ELT (t2, i)))
> -           return 0;
> +       unsigned int count
> +         = tree_vector_builder::binary_encoded_nelts (t1, t2);
> +       for (unsigned int i = 0; i < count; ++i)
> +         if (!sem_variable::equals (VECTOR_CST_ENCODED_ELT (t1, i),
> +                                    VECTOR_CST_ENCODED_ELT (t2, i)))
> +           return false;
>
> -       return 1;
> +       return true;
>        }
>      case ARRAY_REF:
>      case ARRAY_RANGE_REF:
> Index: gcc/print-tree.c
> ===================================================================
> --- gcc/print-tree.c    2017-11-29 11:06:34.810688336 +0000
> +++ gcc/print-tree.c    2017-12-06 14:50:45.558564477 +0000
> @@ -761,24 +761,18 @@ print_node (FILE *file, const char *pref
>
>         case VECTOR_CST:
>           {
> -           /* Big enough for 2 UINT_MAX plus the string below.  */
> +           /* Big enough for UINT_MAX plus the string below.  */
>             char buf[32];
> -           unsigned i;
>
> -           for (i = 0; i < VECTOR_CST_NELTS (node); ++i)
> +           fprintf (file, " npatterns:%u nelts-per-pattern:%u",
> +                    VECTOR_CST_NPATTERNS (node),
> +                    VECTOR_CST_NELTS_PER_PATTERN (node));
> +           unsigned int count = vector_cst_encoded_nelts (node);
> +           for (unsigned int i = 0; i < count; ++i)
>               {
> -               unsigned j;
> -               /* Coalesce the output of identical consecutive elements.  */
> -               for (j = i + 1; j < VECTOR_CST_NELTS (node); j++)
> -                 if (VECTOR_CST_ELT (node, j) != VECTOR_CST_ELT (node, i))
> -                   break;
> -               j--;
> -               if (i == j)
> -                 sprintf (buf, "elt:%u: ", i);
> -               else
> -                 sprintf (buf, "elt:%u...%u: ", i, j);
> -               print_node (file, buf, VECTOR_CST_ELT (node, i), indent + 4);
> -               i = j;
> +               sprintf (buf, "elt:%u: ", i);
> +               print_node (file, buf, VECTOR_CST_ENCODED_ELT (node, i),
> +                           indent + 4);
>               }
>           }
>           break;

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH] selftest: remove "Yoda ordering" in assertions
  2017-12-06 14:40   ` Richard Sandiford
  2017-12-06 15:10     ` ASSERT_* macros (was Re: RFC: Variable-length VECTOR_CSTs) David Malcolm
@ 2018-04-30 15:42     ` David Malcolm
  2018-04-30 22:44       ` Jeff Law
  1 sibling, 1 reply; 24+ messages in thread
From: David Malcolm @ 2018-04-30 15:42 UTC (permalink / raw)
  To: gcc-patches; +Cc: Richard Sandiford, David Malcolm

Our selftest assertions were of the form:

  ASSERT_EQ (expected, actual)

and both Richard Sandiford and I find this "Yoda ordering" confusing.

Our existing tests aren't entirely consistent about this, and it doesn't make
sense for ASSERT_NE and its variants.

The ordering comes from googletest's API, which is what
the earliest version of the selftest code used (before Bernd persuaded
me to stop over-engineering it :) ).

googletest's API now uses just "val1" and "val2" for binary assertion
macros, and their docs now say:

"Historical note: Before February 2016 *_EQ had a convention of calling
it as ASSERT_EQ(expected, actual), so lots of existing code uses this
order. Now *_EQ treats both parameters in the same way."

This seems to have been:
https://github.com/google/googletest/commit/f364e188372e489230ef4e44e1aec6bcb08f3acf
https://github.com/google/googletest/pull/713

This patch renames the params in our selftest API from "expected" and
"actual" to "val1" and "val2".

ASSERT_STREQ (and ASSERT_STREQ_AT) had an asymmetry in error-reporting, where
they did a better job of reporting if the second of the params was NULL; this
patch now handles params equivalently (and both must be non-NULL for a pass).
We aren't able to selftest selftest failures, so I tested the five cases
by hand while developing the patch (4 NULL vs non-NULL cases, with the both
non-NULL case having a pass and fail sub-cases).

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/ChangeLog:
	* selftest.c (assert_streq): Rename "expected" and "actual" to
	"val1" and "val2".  Extend NULL-handling to cover both inputs
	symmetrically, while still requiring both to be non-NULL for a pass.
	* selftest.h (assert_streq): Rename "expected" and "actual" to
	"val1" and "val2".
	(ASSERT_EQ): Likewise.
	(ASSERT_EQ_AT): Likewise.
	(ASSERT_KNOWN_EQ): Likewise.
	(ASSERT_KNOWN_EQ_AT): Likewise.
	(ASSERT_NE): Likewise.
	(ASSERT_MAYBE_NE): Likewise.
	(ASSERT_MAYBE_NE_AT): Likewise.
	(ASSERT_STREQ): Likewise.  Clarify that both must be non-NULL for
	the assertion to pass.
	(ASSERT_STREQ_AT): Likewise.
---
 gcc/selftest.c | 39 ++++++++++++++++++++--------------
 gcc/selftest.h | 66 +++++++++++++++++++++++++++++-----------------------------
 2 files changed, 56 insertions(+), 49 deletions(-)

diff --git a/gcc/selftest.c b/gcc/selftest.c
index 5709110..74adc63 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -63,27 +63,34 @@ fail_formatted (const location &loc, const char *fmt, ...)
 }
 
 /* Implementation detail of ASSERT_STREQ.
-   Compare val_expected and val_actual with strcmp.  They ought
-   to be non-NULL; fail gracefully if either are NULL.  */
+   Compare val1 and val2 with strcmp.  They ought
+   to be non-NULL; fail gracefully if either or both are NULL.  */
 
 void
 assert_streq (const location &loc,
-	      const char *desc_expected, const char *desc_actual,
-	      const char *val_expected, const char *val_actual)
+	      const char *desc_val1, const char *desc_val2,
+	      const char *val1, const char *val2)
 {
-  /* If val_expected is NULL, the test is buggy.  Fail gracefully.  */
-  if (val_expected == NULL)
-    fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=NULL",
-		    desc_expected, desc_actual);
-  /* If val_actual is NULL, fail with a custom error message.  */
-  if (val_actual == NULL)
-    fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=NULL",
-		    desc_expected, desc_actual, val_expected);
-  if (strcmp (val_expected, val_actual) == 0)
-    pass (loc, "ASSERT_STREQ");
+  /* If val1 or val2 are NULL, fail with a custom error message.  */
+  if (val1 == NULL)
+    if (val2 == NULL)
+      fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=NULL",
+		      desc_val1, desc_val2);
+    else
+      fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=NULL val2=\"%s\"",
+		      desc_val1, desc_val2, val2);
   else
-    fail_formatted (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=\"%s\"",
-		    desc_expected, desc_actual, val_expected, val_actual);
+    if (val2 == NULL)
+      fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=NULL",
+		      desc_val1, desc_val2, val1);
+    else
+      {
+	if (strcmp (val1, val2) == 0)
+	  pass (loc, "ASSERT_STREQ");
+	else
+	  fail_formatted (loc, "ASSERT_STREQ (%s, %s) val1=\"%s\" val2=\"%s\"",
+			  desc_val1, desc_val2, val1, val2);
+      }
 }
 
 /* Implementation detail of ASSERT_STR_CONTAINS.
diff --git a/gcc/selftest.h b/gcc/selftest.h
index fbc2bfe..fc47b2c 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -67,8 +67,8 @@ extern void fail_formatted (const location &loc, const char *fmt, ...)
 /* Implementation detail of ASSERT_STREQ.  */
 
 extern void assert_streq (const location &loc,
-			  const char *desc_expected, const char *desc_actual,
-			  const char *val_expected, const char *val_actual);
+			  const char *desc_val1, const char *desc_val2,
+			  const char *val1, const char *val2);
 
 /* Implementation detail of ASSERT_STR_CONTAINS.  */
 
@@ -263,71 +263,71 @@ extern int num_passes;
     ::selftest::pass ((LOC), desc_);				\
   SELFTEST_END_STMT
 
-/* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
+/* Evaluate VAL1 and VAL2 and compare them with ==, calling
    ::selftest::pass if they are equal,
    ::selftest::fail if they are non-equal.  */
 
-#define ASSERT_EQ(EXPECTED, ACTUAL) \
-  ASSERT_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
+#define ASSERT_EQ(VAL1, VAL2) \
+  ASSERT_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
 
 /* Like ASSERT_EQ, but treat LOC as the effective location of the
    selftest.  */
 
-#define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL)		       \
+#define ASSERT_EQ_AT(LOC, VAL1, VAL2)		       \
   SELFTEST_BEGIN_STMT					       \
-  const char *desc_ = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
-  if ((EXPECTED) == (ACTUAL))				       \
+  const char *desc_ = "ASSERT_EQ (" #VAL1 ", " #VAL2 ")"; \
+  if ((VAL1) == (VAL2))				       \
     ::selftest::pass ((LOC), desc_);			       \
   else							       \
     ::selftest::fail ((LOC), desc_);			       \
   SELFTEST_END_STMT
 
-/* Evaluate EXPECTED and ACTUAL and compare them with known_eq, calling
+/* Evaluate VAL1 and VAL2 and compare them with known_eq, calling
    ::selftest::pass if they are always equal,
    ::selftest::fail if they might be non-equal.  */
 
-#define ASSERT_KNOWN_EQ(EXPECTED, ACTUAL) \
-  ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
+#define ASSERT_KNOWN_EQ(VAL1, VAL2) \
+  ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
 
 /* Like ASSERT_KNOWN_EQ, but treat LOC as the effective location of the
    selftest.  */
 
-#define ASSERT_KNOWN_EQ_AT(LOC, EXPECTED, ACTUAL)			\
+#define ASSERT_KNOWN_EQ_AT(LOC, VAL1, VAL2)			\
   SELFTEST_BEGIN_STMT							\
-  const char *desc = "ASSERT_KNOWN_EQ (" #EXPECTED ", " #ACTUAL ")";	\
-  if (known_eq (EXPECTED, ACTUAL))					\
+  const char *desc = "ASSERT_KNOWN_EQ (" #VAL1 ", " #VAL2 ")";	\
+  if (known_eq (VAL1, VAL2))					\
     ::selftest::pass ((LOC), desc);					\
   else									\
     ::selftest::fail ((LOC), desc);					\
   SELFTEST_END_STMT
 
-/* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
+/* Evaluate VAL1 and VAL2 and compare them with !=, calling
    ::selftest::pass if they are non-equal,
    ::selftest::fail if they are equal.  */
 
-#define ASSERT_NE(EXPECTED, ACTUAL)			       \
+#define ASSERT_NE(VAL1, VAL2)			       \
   SELFTEST_BEGIN_STMT					       \
-  const char *desc_ = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
-  if ((EXPECTED) != (ACTUAL))				       \
+  const char *desc_ = "ASSERT_NE (" #VAL1 ", " #VAL2 ")"; \
+  if ((VAL1) != (VAL2))				       \
     ::selftest::pass (SELFTEST_LOCATION, desc_);	       \
   else							       \
     ::selftest::fail (SELFTEST_LOCATION, desc_);	       \
   SELFTEST_END_STMT
 
-/* Evaluate EXPECTED and ACTUAL and compare them with maybe_ne, calling
+/* Evaluate VAL1 and VAL2 and compare them with maybe_ne, calling
    ::selftest::pass if they might be non-equal,
    ::selftest::fail if they are known to be equal.  */
 
-#define ASSERT_MAYBE_NE(EXPECTED, ACTUAL) \
-  ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
+#define ASSERT_MAYBE_NE(VAL1, VAL2) \
+  ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
 
 /* Like ASSERT_MAYBE_NE, but treat LOC as the effective location of the
    selftest.  */
 
-#define ASSERT_MAYBE_NE_AT(LOC, EXPECTED, ACTUAL)			\
+#define ASSERT_MAYBE_NE_AT(LOC, VAL1, VAL2)			\
   SELFTEST_BEGIN_STMT							\
-  const char *desc = "ASSERT_MAYBE_NE (" #EXPECTED ", " #ACTUAL ")";	\
-  if (maybe_ne (EXPECTED, ACTUAL))					\
+  const char *desc = "ASSERT_MAYBE_NE (" #VAL1 ", " #VAL2 ")";	\
+  if (maybe_ne (VAL1, VAL2))					\
     ::selftest::pass ((LOC), desc);					\
   else									\
     ::selftest::fail ((LOC), desc);					\
@@ -371,23 +371,23 @@ extern int num_passes;
     ::selftest::fail ((LOC), desc_);			       \
   SELFTEST_END_STMT
 
-/* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
-   ::selftest::pass if they are equal,
-   ::selftest::fail if they are non-equal.  */
+/* Evaluate VAL1 and VAL2 and compare them with strcmp, calling
+   ::selftest::pass if they are equal (and both are non-NULL),
+   ::selftest::fail if they are non-equal, or are both NULL.  */
 
-#define ASSERT_STREQ(EXPECTED, ACTUAL)				    \
+#define ASSERT_STREQ(VAL1, VAL2)				    \
   SELFTEST_BEGIN_STMT						    \
-  ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
-			    (EXPECTED), (ACTUAL));		    \
+  ::selftest::assert_streq (SELFTEST_LOCATION, #VAL1, #VAL2, \
+			    (VAL1), (VAL2));		    \
   SELFTEST_END_STMT
 
 /* Like ASSERT_STREQ, but treat LOC as the effective location of the
    selftest.  */
 
-#define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL)			    \
+#define ASSERT_STREQ_AT(LOC, VAL1, VAL2)			    \
   SELFTEST_BEGIN_STMT						    \
-  ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL,		    \
-			    (EXPECTED), (ACTUAL));		    \
+  ::selftest::assert_streq ((LOC), #VAL1, #VAL2,		    \
+			    (VAL1), (VAL2));		    \
   SELFTEST_END_STMT
 
 /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
-- 
1.8.5.3

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH] selftest: remove "Yoda ordering" in assertions
  2018-04-30 15:42     ` [PATCH] selftest: remove "Yoda ordering" in assertions David Malcolm
@ 2018-04-30 22:44       ` Jeff Law
  0 siblings, 0 replies; 24+ messages in thread
From: Jeff Law @ 2018-04-30 22:44 UTC (permalink / raw)
  To: David Malcolm, gcc-patches; +Cc: Richard Sandiford

On 04/30/2018 09:48 AM, David Malcolm wrote:
> Our selftest assertions were of the form:
> 
>   ASSERT_EQ (expected, actual)
> 
> and both Richard Sandiford and I find this "Yoda ordering" confusing.
> 
> Our existing tests aren't entirely consistent about this, and it doesn't make
> sense for ASSERT_NE and its variants.
> 
> The ordering comes from googletest's API, which is what
> the earliest version of the selftest code used (before Bernd persuaded
> me to stop over-engineering it :) ).
> 
> googletest's API now uses just "val1" and "val2" for binary assertion
> macros, and their docs now say:
> 
> "Historical note: Before February 2016 *_EQ had a convention of calling
> it as ASSERT_EQ(expected, actual), so lots of existing code uses this
> order. Now *_EQ treats both parameters in the same way."
> 
> This seems to have been:
> https://github.com/google/googletest/commit/f364e188372e489230ef4e44e1aec6bcb08f3acf
> https://github.com/google/googletest/pull/713
> 
> This patch renames the params in our selftest API from "expected" and
> "actual" to "val1" and "val2".
> 
> ASSERT_STREQ (and ASSERT_STREQ_AT) had an asymmetry in error-reporting, where
> they did a better job of reporting if the second of the params was NULL; this
> patch now handles params equivalently (and both must be non-NULL for a pass).
> We aren't able to selftest selftest failures, so I tested the five cases
> by hand while developing the patch (4 NULL vs non-NULL cases, with the both
> non-NULL case having a pass and fail sub-cases).
> 
> Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
> 
> OK for trunk?
> 
> gcc/ChangeLog:
> 	* selftest.c (assert_streq): Rename "expected" and "actual" to
> 	"val1" and "val2".  Extend NULL-handling to cover both inputs
> 	symmetrically, while still requiring both to be non-NULL for a pass.
> 	* selftest.h (assert_streq): Rename "expected" and "actual" to
> 	"val1" and "val2".
> 	(ASSERT_EQ): Likewise.
> 	(ASSERT_EQ_AT): Likewise.
> 	(ASSERT_KNOWN_EQ): Likewise.
> 	(ASSERT_KNOWN_EQ_AT): Likewise.
> 	(ASSERT_NE): Likewise.
> 	(ASSERT_MAYBE_NE): Likewise.
> 	(ASSERT_MAYBE_NE_AT): Likewise.
> 	(ASSERT_STREQ): Likewise.  Clarify that both must be non-NULL for
> 	the assertion to pass.
> 	(ASSERT_STREQ_AT): Likewise.
OK.
jeff

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2018-04-30 22:21 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-29 12:14 RFC: Variable-length VECTOR_CSTs Richard Sandiford
2017-11-29 15:11 ` David Malcolm
2017-12-06 14:40   ` Richard Sandiford
2017-12-06 15:10     ` ASSERT_* macros (was Re: RFC: Variable-length VECTOR_CSTs) David Malcolm
2018-04-30 15:42     ` [PATCH] selftest: remove "Yoda ordering" in assertions David Malcolm
2018-04-30 22:44       ` Jeff Law
2017-11-29 15:56 ` RFC: Variable-length VECTOR_CSTs Richard Biener
2017-11-29 18:24   ` Richard Sandiford
2017-11-30 14:10     ` Richard Sandiford
2017-12-01 11:28       ` Richard Biener
2017-12-06 15:12         ` Richard Sandiford
2017-12-06 15:22           ` Use tree_vector_builder instead of build_vector Richard Sandiford
2017-12-07 11:06             ` Richard Biener
2017-12-06 15:23           ` Use tree_vector_builder::new_unary_operation for folding Richard Sandiford
2017-12-07 11:06             ` Richard Biener
2017-12-06 15:24           ` Use tree_vector_builder::new_binary_operation " Richard Sandiford
2017-12-07 11:07             ` Richard Biener
2017-12-06 15:25           ` Make gimple_build_vector take a tree_vector_builder Richard Sandiford
2017-12-07 11:08             ` Richard Biener
2017-12-06 15:26           ` Make build_vector static Richard Sandiford
2017-12-07 11:09             ` Richard Biener
2017-12-06 15:28           ` Make more use of VECTOR_CST_ENCODED_ELT Richard Sandiford
2017-12-07 11:12             ` Richard Biener
2017-12-07 11:05           ` RFC: Variable-length VECTOR_CSTs Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).