Index: gcc/vec-perm-indices.h
===================================================================
--- gcc/vec-perm-indices.h	2018-01-02 18:27:07.346639775 +0000
+++ gcc/vec-perm-indices.h	2018-01-03 08:46:30.347663443 +0000
@@ -25,7 +25,7 @@ #define GCC_VEC_PERN_INDICES_H 1
 /* A vector_builder for building constant permutation vectors.
    The elements do not need to be clamped to a particular range
    of input elements.  */
-typedef int_vector_builder<HOST_WIDE_INT> vec_perm_builder;
+typedef int_vector_builder<poly_int64> vec_perm_builder;
 
 /* This class represents a constant permutation vector, such as that used
    as the final operand to a VEC_PERM_EXPR.
@@ -49,7 +49,7 @@ typedef int_vector_builder<HOST_WIDE_INT
    different numbers of elements.  */
 class vec_perm_indices
 {
-  typedef HOST_WIDE_INT element_type;
+  typedef poly_int64 element_type;
 
 public:
   vec_perm_indices ();
@@ -118,13 +118,17 @@ vec_perm_indices::vec_perm_indices (cons
 inline vec_perm_indices::element_type
 vec_perm_indices::clamp (element_type elt) const
 {
-  element_type limit = input_nelts ();
-  elt %= limit;
+  element_type limit = input_nelts (), elem_within_input;
+  int input;
+  if (!can_div_trunc_p (elt, limit, &input, &elem_within_input))
+    return elt;
+
   /* Treat negative elements as counting from the end.  This only matters
      if the vector size is not a power of 2.  */
-  if (elt < 0)
-    elt += limit;
-  return elt;
+  if (known_lt (elem_within_input, 0))
+    return elem_within_input + limit;
+
+  return elem_within_input;
 }
 
 /* Return the value of vector element I, which might or might not be
Index: gcc/vec-perm-indices.c
===================================================================
--- gcc/vec-perm-indices.c	2018-01-02 18:27:52.375765255 +0000
+++ gcc/vec-perm-indices.c	2018-01-03 08:46:30.347663443 +0000
@@ -95,7 +95,7 @@ vec_perm_indices::series_p (unsigned int
 			    element_type in_base, element_type in_step) const
 {
   /* Check the base value.  */
-  if (clamp (m_encoding.elt (out_base)) != clamp (in_base))
+  if (maybe_ne (clamp (m_encoding.elt (out_base)), clamp (in_base)))
     return false;
 
   unsigned int full_nelts = m_encoding.full_nelts ();
@@ -127,7 +127,7 @@ vec_perm_indices::series_p (unsigned int
 
       element_type v0 = m_encoding.elt (out_base - out_step);
       element_type v1 = m_encoding.elt (out_base);
-      if (clamp (v1 - v0) != in_step)
+      if (maybe_ne (clamp (v1 - v0), in_step))
 	return false;
 
       out_base += out_step;
@@ -146,7 +146,7 @@ vec_perm_indices::all_in_range_p (elemen
   unsigned int nelts_per_pattern = m_encoding.nelts_per_pattern ();
   unsigned int base_nelts = npatterns * MIN (nelts_per_pattern, 2);
   for (unsigned int i = 0; i < base_nelts; ++i)
-    if (m_encoding[i] < start || (m_encoding[i] - start) >= size)
+    if (!known_in_range_p (m_encoding[i], start, size))
       return false;
 
   /* For stepped encodings, check the full range of the series.  */
@@ -174,8 +174,11 @@ vec_perm_indices::all_in_range_p (elemen
 	     wide enough for overflow not to be a problem.  */
 	  element_type headroom_down = base1 - start;
 	  element_type headroom_up = size - headroom_down - 1;
-	  if (headroom_up < step * step_nelts
-	      && headroom_down < (limit - step) * step_nelts)
+	  HOST_WIDE_INT diff;
+	  if ((!step.is_constant (&diff)
+	       || maybe_lt (headroom_up, diff * step_nelts))
+	      && (!(limit - step).is_constant (&diff)
+		  || maybe_lt (headroom_down, diff * step_nelts)))
 	    return false;
 	}
     }
@@ -191,14 +194,14 @@ tree_to_vec_perm_builder (vec_perm_build
 {
   unsigned int encoded_nelts = vector_cst_encoded_nelts (cst);
   for (unsigned int i = 0; i < encoded_nelts; ++i)
-    if (!tree_fits_shwi_p (VECTOR_CST_ENCODED_ELT (cst, i)))
+    if (!tree_fits_poly_int64_p (VECTOR_CST_ENCODED_ELT (cst, i)))
       return false;
 
   builder->new_vector (TYPE_VECTOR_SUBPARTS (TREE_TYPE (cst)),
 		       VECTOR_CST_NPATTERNS (cst),
 		       VECTOR_CST_NELTS_PER_PATTERN (cst));
   for (unsigned int i = 0; i < encoded_nelts; ++i)
-    builder->quick_push (tree_to_shwi (VECTOR_CST_ENCODED_ELT (cst, i)));
+    builder->quick_push (tree_to_poly_int64 (VECTOR_CST_ENCODED_ELT (cst, i)));
   return true;
 }
 
Index: gcc/vector-builder.h
===================================================================
--- gcc/vector-builder.h	2018-01-02 18:27:07.346639775 +0000
+++ gcc/vector-builder.h	2018-01-03 08:46:30.347663443 +0000
@@ -284,7 +284,8 @@ vector_builder<T, Derived>::stepped_sequ
 	  || !derived ()->integral_p (elt3))
 	return false;
 
-      if (derived ()->step (elt1, elt2) != derived ()->step (elt2, elt3))
+      if (maybe_ne (derived ()->step (elt1, elt2),
+		    derived ()->step (elt2, elt3)))
 	return false;
 
       if (!derived ()->can_elide_p (elt3))
Index: gcc/int-vector-builder.h
===================================================================
--- gcc/int-vector-builder.h	2018-01-02 18:26:37.619823164 +0000
+++ gcc/int-vector-builder.h	2018-01-03 08:46:30.345663760 +0000
@@ -66,7 +66,7 @@ int_vector_builder<T>::int_vector_builde
 inline bool
 int_vector_builder<T>::equal_p (T elt1, T elt2) const
 {
-  return elt1 == elt2;
+  return known_eq (elt1, elt2);
 }
 
 /* Return the value of element ELT2 minus the value of element ELT1.  */
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	2018-01-03 07:17:07.401983352 +0000
+++ gcc/fold-const.c	2018-01-03 08:46:30.345663760 +0000
@@ -8945,9 +8945,12 @@ fold_vec_perm (tree type, tree arg0, tre
   tree_vector_builder out_elts (type, nelts, 1);
   for (i = 0; i < nelts; i++)
     {
-      if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
+      HOST_WIDE_INT index;
+      if (!sel[i].is_constant (&index))
+	return NULL_TREE;
+      if (!CONSTANT_CLASS_P (in_elts[index]))
 	need_ctor = true;
-      out_elts.quick_push (unshare_expr (in_elts[sel[i]]));
+      out_elts.quick_push (unshare_expr (in_elts[index]));
     }
 
   if (need_ctor)
Index: gcc/optabs.c
===================================================================
--- gcc/optabs.c	2018-01-02 18:27:52.375765255 +0000
+++ gcc/optabs.c	2018-01-03 08:46:30.346663602 +0000
@@ -5398,16 +5398,18 @@ shift_amt_for_vec_perm_mask (machine_mod
 {
   unsigned int nelt = GET_MODE_NUNITS (mode);
   unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
-  unsigned int first = sel[0];
-  if (first >= nelt)
+  poly_int64 first = sel[0];
+  if (maybe_ge (sel[0], nelt))
     return NULL_RTX;
 
   if (!sel.series_p (0, 1, first, 1))
     for (unsigned int i = 1; i < nelt; i++)
       {
-	unsigned int expected = i + first;
+	poly_int64 expected = i + first;
 	/* Indices into the second vector are all equivalent.  */
-	if (MIN (nelt, sel[i]) != MIN (nelt, expected))
+	if (maybe_lt (sel[i], nelt)
+	    ? maybe_ne (sel[i], expected)
+	    : maybe_lt (expected, nelt))
 	  return NULL_RTX;
       }
 
Index: gcc/tree-vect-generic.c
===================================================================
--- gcc/tree-vect-generic.c	2018-01-03 07:16:49.037017897 +0000
+++ gcc/tree-vect-generic.c	2018-01-03 08:46:30.346663602 +0000
@@ -1337,18 +1337,19 @@ lower_vec_perm (gimple_stmt_iterator *gs
 	  != CODE_FOR_nothing
 	  && TREE_CODE (vec1) == VECTOR_CST
 	  && initializer_zerop (vec1)
-	  && indices[0]
-	  && indices[0] < elements)
+	  && maybe_ne (indices[0], 0)
+	  && known_lt (indices[0], elements))
 	{
 	  bool ok_p = indices.series_p (0, 1, indices[0], 1);
 	  if (!ok_p)
 	    {
 	      for (i = 1; i < elements; ++i)
 		{
-		  unsigned int expected = i + indices[0];
+		  poly_int64 expected = i + indices[0];
 		  /* Indices into the second vector are all equivalent.  */
-		  if (MIN (elements, (unsigned) indices[i])
-		      != MIN (elements, expected))
+		  if (maybe_lt (indices[i], elements)
+		      ? maybe_ne (indices[i], expected)
+		      : maybe_lt (expected, elements))
 		    break;
 		}
 	      ok_p = i == elements;
Index: gcc/tree-vect-slp.c
===================================================================
--- gcc/tree-vect-slp.c	2018-01-03 07:16:36.566760459 +0000
+++ gcc/tree-vect-slp.c	2018-01-03 08:46:30.347663443 +0000
@@ -3727,8 +3727,10 @@ vect_transform_slp_perm_load (slp_tree n
 				       vect_location, 
 				       "unsupported vect permute { ");
 		      for (i = 0; i < nunits; ++i)
-			dump_printf (MSG_MISSED_OPTIMIZATION,
-				     HOST_WIDE_INT_PRINT_DEC " ", mask[i]);
+			{
+			  dump_dec (MSG_MISSED_OPTIMIZATION, mask[i]);
+			  dump_printf (MSG_MISSED_OPTIMIZATION, " ");
+			}
 		      dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
 		    }
 		  gcc_assert (analyze_only);
Index: gcc/config/aarch64/aarch64.c
===================================================================
--- gcc/config/aarch64/aarch64.c	2018-01-03 07:14:58.947066762 +0000
+++ gcc/config/aarch64/aarch64.c	2018-01-03 08:46:30.343664078 +0000
@@ -13625,7 +13625,7 @@ aarch64_evpc_tbl (struct expand_vec_perm
 	 mode on NEON.  Reverse the index within each word but not the word
 	 itself.  */
       rperm[i] = GEN_INT (BYTES_BIG_ENDIAN ? d->perm[i] ^ (nunits - 1)
-					   : d->perm[i]);
+					   : (HOST_WIDE_INT) d->perm[i]);
     }
   sel = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (nelt, rperm));
   sel = force_reg (vmode, sel);
