]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/tree-ssa-forwprop.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / tree-ssa-forwprop.c
index fe55ca958b49b986f79a9a710d92b5d906959105..759baf568978b3201ef1f744fa4d446d3daf12fb 100644 (file)
@@ -1,5 +1,5 @@
 /* Forward propagation of expressions for single use variables.
-   Copyright (C) 2004-2019 Free Software Foundation, Inc.
+   Copyright (C) 2004-2020 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -48,6 +48,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "optabs-tree.h"
 #include "tree-vector-builder.h"
 #include "vec-perm-indices.h"
+#include "internal-fn.h"
+#include "cgraph.h"
+#include "tree-ssa.h"
 
 /* This pass propagates the RHS of assignment statements into use
    sites of the LHS of the assignment.  It's basically a specialized
@@ -730,16 +733,15 @@ forward_propagate_addr_expr_1 (tree name, tree def_rhs,
       if (TREE_CODE (new_def_rhs) == MEM_REF
          && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
        return false;
-      new_def_rhs = build_fold_addr_expr_with_type (new_def_rhs,
-                                                   TREE_TYPE (rhs));
+      new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
 
       /* Recurse.  If we could propagate into all uses of lhs do not
         bother to replace into the current use but just pretend we did.  */
-      if (TREE_CODE (new_def_rhs) == ADDR_EXPR
-         && forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
+      if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
        return true;
 
-      if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_def_rhs)))
+      if (useless_type_conversion_p (TREE_TYPE (lhs),
+                                    TREE_TYPE (new_def_rhs)))
        gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
                                        new_def_rhs);
       else if (is_gimple_min_invariant (new_def_rhs))
@@ -1317,6 +1319,7 @@ simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
                  || !tree_fits_shwi_p (src1))
                break;
              ptr1 = build_fold_addr_expr (ptr1);
+             STRIP_USELESS_TYPE_CONVERSION (ptr1);
              callee1 = NULL_TREE;
              len1 = size_one_node;
              lhs1 = NULL_TREE;
@@ -1559,14 +1562,14 @@ simplify_rotate (gimple_stmt_iterator *gsi)
   for (i = 0; i < 2; i++)
     defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
 
-  /* Look through narrowing conversions.  */
+  /* Look through narrowing (or same precision) conversions.  */
   if (CONVERT_EXPR_CODE_P (def_code[0])
       && CONVERT_EXPR_CODE_P (def_code[1])
       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
       && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
       && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
         == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
-      && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) > TYPE_PRECISION (rtype)
+      && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
       && has_single_use (arg[0])
       && has_single_use (arg[1]))
     {
@@ -1576,6 +1579,21 @@ simplify_rotate (gimple_stmt_iterator *gsi)
          defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
        }
     }
+  else
+    {
+      /* Handle signed rotate; the RSHIFT_EXPR has to be done
+        in unsigned type but LSHIFT_EXPR could be signed.  */
+      i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
+      if (CONVERT_EXPR_CODE_P (def_code[i])
+         && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
+         && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
+         && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
+         && has_single_use (arg[i]))
+       {
+         arg[i] = def_arg1[i];
+         defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
+       }
+    }
 
   /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR.  */
   for (i = 0; i < 2; i++)
@@ -1605,8 +1623,33 @@ simplify_rotate (gimple_stmt_iterator *gsi)
   if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
       || !types_compatible_p (TREE_TYPE (def_arg1[0]),
                              TREE_TYPE (def_arg1[1])))
-    return false;
-  if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
+    {
+      if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
+          != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
+         || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
+             == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
+       return false;
+
+      /* Handle signed rotate; the RSHIFT_EXPR has to be done
+        in unsigned type but LSHIFT_EXPR could be signed.  */
+      i = def_code[0] != RSHIFT_EXPR;
+      if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
+       return false;
+
+      tree tem;
+      enum tree_code code;
+      defcodefor_name (def_arg1[i], &code, &tem, NULL);
+      if (!CONVERT_EXPR_CODE_P (code)
+         || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
+         || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
+       return false;
+      def_arg1[i] = tem;
+      if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
+         || !types_compatible_p (TREE_TYPE (def_arg1[0]),
+                                 TREE_TYPE (def_arg1[1])))
+       return false;
+    }
+  else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
     return false;
 
   /* CNT1 + CNT2 == B case above.  */
@@ -1778,6 +1821,198 @@ simplify_rotate (gimple_stmt_iterator *gsi)
   return true;
 }
 
+
+/* Check whether an array contains a valid ctz table.  */
+static bool
+check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc,
+                HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
+{
+  tree elt, idx;
+  unsigned HOST_WIDE_INT i, mask;
+  unsigned matched = 0;
+
+  mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
+
+  zero_val = 0;
+
+  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
+    {
+      if (TREE_CODE (idx) != INTEGER_CST || TREE_CODE (elt) != INTEGER_CST)
+       return false;
+      if (i > bits * 2)
+       return false;
+
+      unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
+      HOST_WIDE_INT val = tree_to_shwi (elt);
+
+      if (index == 0)
+       {
+         zero_val = val;
+         matched++;
+       }
+
+      if (val >= 0 && val < bits && (((mulc << val) & mask) >> shift) == index)
+       matched++;
+
+      if (matched > bits)
+       return true;
+    }
+
+  return false;
+}
+
+/* Check whether a string contains a valid ctz table.  */
+static bool
+check_ctz_string (tree string, unsigned HOST_WIDE_INT mulc,
+                 HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits)
+{
+  unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
+  unsigned HOST_WIDE_INT mask;
+  unsigned matched = 0;
+  const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
+
+  if (len < bits || len > bits * 2)
+    return false;
+
+  mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift;
+
+  zero_val = p[0];
+
+  for (unsigned i = 0; i < len; i++)
+    if (p[i] < bits && (((mulc << p[i]) & mask) >> shift) == i)
+      matched++;
+
+  return matched == bits;
+}
+
+/* Recognize count trailing zeroes idiom.
+   The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
+   constant which when multiplied by a power of 2 creates a unique value
+   in the top 5 or 6 bits.  This is then indexed into a table which maps it
+   to the number of trailing zeroes.  Array[0] is returned so the caller can
+   emit an appropriate sequence depending on whether ctz (0) is defined on
+   the target.  */
+static bool
+optimize_count_trailing_zeroes (tree array_ref, tree x, tree mulc,
+                               tree tshift, HOST_WIDE_INT &zero_val)
+{
+  tree type = TREE_TYPE (array_ref);
+  tree array = TREE_OPERAND (array_ref, 0);
+
+  gcc_assert (TREE_CODE (mulc) == INTEGER_CST);
+  gcc_assert (TREE_CODE (tshift) == INTEGER_CST);
+
+  tree input_type = TREE_TYPE (x);
+  unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
+
+  /* Check the array element type is not wider than 32 bits and the input is
+     an unsigned 32-bit or 64-bit type.  */
+  if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
+    return false;
+  if (input_bits != 32 && input_bits != 64)
+    return false;
+
+  if (!direct_internal_fn_supported_p (IFN_CTZ, input_type, OPTIMIZE_FOR_BOTH))
+    return false;
+
+  /* Check the lower bound of the array is zero.  */
+  tree low = array_ref_low_bound (array_ref);
+  if (!low || !integer_zerop (low))
+    return false;
+
+  unsigned shiftval = tree_to_shwi (tshift);
+
+  /* Check the shift extracts the top 5..7 bits.  */
+  if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
+    return false;
+
+  tree ctor = ctor_for_folding (array);
+  if (!ctor)
+    return false;
+
+  unsigned HOST_WIDE_INT val = tree_to_uhwi (mulc);
+
+  if (TREE_CODE (ctor) == CONSTRUCTOR)
+    return check_ctz_array (ctor, val, zero_val, shiftval, input_bits);
+
+  if (TREE_CODE (ctor) == STRING_CST
+      && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
+    return check_ctz_string (ctor, val, zero_val, shiftval, input_bits);
+
+  return false;
+}
+
+/* Match.pd function to match the ctz expression.  */
+extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
+
+static bool
+simplify_count_trailing_zeroes (gimple_stmt_iterator *gsi)
+{
+  gimple *stmt = gsi_stmt (*gsi);
+  tree array_ref = gimple_assign_rhs1 (stmt);
+  tree res_ops[3];
+  HOST_WIDE_INT zero_val;
+
+  gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
+
+  if (!gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
+    return false;
+
+  if (optimize_count_trailing_zeroes (array_ref, res_ops[0],
+                                     res_ops[1], res_ops[2], zero_val))
+    {
+      tree type = TREE_TYPE (res_ops[0]);
+      HOST_WIDE_INT ctz_val = 0;
+      HOST_WIDE_INT type_size = tree_to_shwi (TYPE_SIZE (type));
+      bool zero_ok
+       = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type), ctz_val) == 2;
+
+      /* If the input value can't be zero, don't special case ctz (0).  */
+      if (tree_expr_nonzero_p (res_ops[0]))
+       {
+         zero_ok = true;
+         zero_val = 0;
+         ctz_val = 0;
+       }
+
+      /* Skip if there is no value defined at zero, or if we can't easily
+        return the correct value for zero.  */
+      if (!zero_ok)
+       return false;
+      if (zero_val != ctz_val && !(zero_val == 0 && ctz_val == type_size))
+       return false;
+
+      gimple_seq seq = NULL;
+      gimple *g;
+      gcall *call = gimple_build_call_internal (IFN_CTZ, 1, res_ops[0]);
+      gimple_set_location (call, gimple_location (stmt));
+      gimple_set_lhs (call, make_ssa_name (integer_type_node));
+      gimple_seq_add_stmt (&seq, call);
+
+      tree prev_lhs = gimple_call_lhs (call);
+
+      /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0.  */
+      if (zero_val == 0 && ctz_val == type_size)
+       {
+         g = gimple_build_assign (make_ssa_name (integer_type_node),
+                                  BIT_AND_EXPR, prev_lhs,
+                                  build_int_cst (integer_type_node,
+                                                 type_size - 1));
+         gimple_set_location (g, gimple_location (stmt));
+         gimple_seq_add_stmt (&seq, g);
+         prev_lhs = gimple_assign_lhs (g);
+       }
+
+      g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
+      gimple_seq_add_stmt (&seq, g);
+      gsi_replace_with_seq (gsi, seq, true);
+      return true;
+    }
+
+  return false;
+}
+
+
 /* Combine an element access with a shuffle.  Returns true if there were
    any changes made, else it returns false.  */
  
@@ -1786,7 +2021,7 @@ simplify_bitfield_ref (gimple_stmt_iterator *gsi)
 {
   gimple *stmt = gsi_stmt (*gsi);
   gimple *def_stmt;
-  tree op, op0, op1, op2;
+  tree op, op0, op1;
   tree elem_type;
   unsigned idx, size;
   enum tree_code code;
@@ -1804,20 +2039,7 @@ simplify_bitfield_ref (gimple_stmt_iterator *gsi)
     return false;
 
   op1 = TREE_OPERAND (op, 1);
-  op2 = TREE_OPERAND (op, 2);
   code = gimple_assign_rhs_code (def_stmt);
-
-  if (code == CONSTRUCTOR)
-    {
-      tree tem = fold_ternary (BIT_FIELD_REF, TREE_TYPE (op),
-                              gimple_assign_rhs1 (def_stmt), op1, op2);
-      if (!tem || !valid_gimple_rhs_p (tem))
-       return false;
-      gimple_assign_set_rhs_from_tree (gsi, tem);
-      update_stmt (gsi_stmt (*gsi));
-      return true;
-    }
-
   elem_type = TREE_TYPE (TREE_TYPE (op0));
   if (TREE_TYPE (op) != elem_type)
     return false;
@@ -2017,16 +2239,12 @@ get_bit_field_ref_def (tree val, enum tree_code &conv_code)
     return NULL_TREE;
   enum tree_code code = gimple_assign_rhs_code (def_stmt);
   if (code == FLOAT_EXPR
-      || code == FIX_TRUNC_EXPR)
+      || code == FIX_TRUNC_EXPR
+      || CONVERT_EXPR_CODE_P (code))
     {
       tree op1 = gimple_assign_rhs1 (def_stmt);
       if (conv_code == ERROR_MARK)
-       {
-         if (maybe_ne (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (val))),
-                       GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))))
-           return NULL_TREE;
-         conv_code = code;
-       }
+       conv_code = code;
       else if (conv_code != code)
        return NULL_TREE;
       if (TREE_CODE (op1) != SSA_NAME)
@@ -2047,33 +2265,33 @@ static bool
 simplify_vector_constructor (gimple_stmt_iterator *gsi)
 {
   gimple *stmt = gsi_stmt (*gsi);
-  tree op, op2, orig[2], type, elem_type;
+  tree op, orig[2], type, elem_type;
   unsigned elem_size, i;
   unsigned HOST_WIDE_INT nelts;
+  unsigned HOST_WIDE_INT refnelts;
   enum tree_code conv_code;
   constructor_elt *elt;
-  bool maybe_ident;
-
-  gcc_checking_assert (gimple_assign_rhs_code (stmt) == CONSTRUCTOR);
 
   op = gimple_assign_rhs1 (stmt);
   type = TREE_TYPE (op);
-  gcc_checking_assert (TREE_CODE (type) == VECTOR_TYPE);
+  gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
+                      && TREE_CODE (type) == VECTOR_TYPE);
 
   if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
     return false;
   elem_type = TREE_TYPE (type);
   elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
 
-  vec_perm_builder sel (nelts, nelts, 1);
   orig[0] = NULL;
   orig[1] = NULL;
   conv_code = ERROR_MARK;
-  maybe_ident = true;
+  bool maybe_ident = true;
+  bool maybe_blend[2] = { true, true };
   tree one_constant = NULL_TREE;
   tree one_nonconstant = NULL_TREE;
   auto_vec<tree> constants;
   constants.safe_grow_cleared (nelts);
+  auto_vec<std::pair<unsigned, unsigned>, 64> elts;
   FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
     {
       tree ref, op1;
@@ -2090,9 +2308,9 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          && VECTOR_TYPE_P (TREE_TYPE (ref))
          && useless_type_conversion_p (TREE_TYPE (op1),
                                        TREE_TYPE (TREE_TYPE (ref)))
-         && known_eq (bit_field_size (op1), elem_size)
          && constant_multiple_p (bit_field_offset (op1),
-                                 elem_size, &elem))
+                                 bit_field_size (op1), &elem)
+         && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
        {
          unsigned int j;
          for (j = 0; j < 2; ++j)
@@ -2111,11 +2329,11 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          if (j < 2)
            {
              orig[j] = ref;
-             if (j)
-               elem += nelts;
-             if (elem != i)
+             if (elem != i || j != 0)
                maybe_ident = false;
-             sel.quick_push (elem);
+             if (elem != i)
+               maybe_blend[j] = false;
+             elts.safe_push (std::make_pair (j, elem));
              continue;
            }
          /* Else fallthru.  */
@@ -2144,95 +2362,270 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
          else if (!operand_equal_p (one_nonconstant, elt->value, 0))
            return false;
        }
-      sel.quick_push (i + nelts);
+      elts.safe_push (std::make_pair (1, i));
       maybe_ident = false;
     }
   if (i < nelts)
     return false;
 
   if (! orig[0]
-      || ! VECTOR_TYPE_P (TREE_TYPE (orig[0]))
-      || maybe_ne (TYPE_VECTOR_SUBPARTS (type),
-                  TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0]))))
+      || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
     return false;
-
-  tree tem;
-  if (conv_code != ERROR_MARK
-      && (! supportable_convert_operation (conv_code, type,
-                                          TREE_TYPE (orig[0]),
-                                          &tem, &conv_code)
-         || conv_code == CALL_EXPR))
+  refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
+  /* We currently do not handle larger destination vectors.  */
+  if (refnelts < nelts)
     return false;
 
   if (maybe_ident)
     {
+      tree conv_src_type
+       = (nelts != refnelts
+          ? (conv_code != ERROR_MARK
+             ? build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])), nelts)
+             : type)
+          : TREE_TYPE (orig[0]));
+      if (conv_code != ERROR_MARK
+         && !supportable_convert_operation (conv_code, type, conv_src_type,
+                                            &conv_code))
+       {
+         /* Only few targets implement direct conversion patterns so try
+            some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR.  */
+         optab optab;
+         tree halfvectype, dblvectype;
+         if (CONVERT_EXPR_CODE_P (conv_code)
+             && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
+                 == TYPE_PRECISION (TREE_TYPE (type)))
+             && mode_for_vector (as_a <scalar_mode>
+                                 (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
+                                 nelts * 2).exists ()
+             && (dblvectype
+                 = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
+                                      nelts * 2))
+             && (optab = optab_for_tree_code (FLOAT_TYPE_P (TREE_TYPE (type))
+                                              ? VEC_UNPACK_FLOAT_LO_EXPR
+                                              : VEC_UNPACK_LO_EXPR,
+                                              dblvectype,
+                                              optab_default))
+             && (optab_handler (optab, TYPE_MODE (dblvectype))
+                 != CODE_FOR_nothing))
+           {
+             gimple_seq stmts = NULL;
+             tree dbl;
+             if (refnelts == nelts)
+               {
+                 /* ???  Paradoxical subregs don't exist, so insert into
+                    the lower half of a wider zero vector.  */
+                 dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
+                                     build_zero_cst (dblvectype), orig[0],
+                                     bitsize_zero_node);
+               }
+             else if (refnelts == 2 * nelts)
+               dbl = orig[0];
+             else
+               dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
+                                   orig[0], TYPE_SIZE (dblvectype),
+                                   bitsize_zero_node);
+             gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
+             gimple_assign_set_rhs_with_ops (gsi,
+                                             FLOAT_TYPE_P (TREE_TYPE (type))
+                                             ? VEC_UNPACK_FLOAT_LO_EXPR
+                                             : VEC_UNPACK_LO_EXPR,
+                                             dbl);
+           }
+         else if (CONVERT_EXPR_CODE_P (conv_code)
+                  && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
+                      == 2 * TYPE_PRECISION (TREE_TYPE (type)))
+                  && mode_for_vector (as_a <scalar_mode>
+                                        (TYPE_MODE
+                                          (TREE_TYPE (TREE_TYPE (orig[0])))),
+                                      nelts / 2).exists ()
+                  && (halfvectype
+                        = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
+                                             nelts / 2))
+                  && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
+                                                   halfvectype,
+                                                   optab_default))
+                  && (optab_handler (optab, TYPE_MODE (halfvectype))
+                      != CODE_FOR_nothing))
+           {
+             gimple_seq stmts = NULL;
+             tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
+                                      orig[0], TYPE_SIZE (halfvectype),
+                                      bitsize_zero_node);
+             tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
+                                      orig[0], TYPE_SIZE (halfvectype),
+                                      TYPE_SIZE (halfvectype));
+             gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
+             gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
+                                             low, hig);
+           }
+         else
+           return false;
+         update_stmt (gsi_stmt (*gsi));
+         return true;
+       }
+      if (nelts != refnelts)
+       {
+         gassign *lowpart
+           = gimple_build_assign (make_ssa_name (conv_src_type),
+                                  build3 (BIT_FIELD_REF, conv_src_type,
+                                          orig[0], TYPE_SIZE (conv_src_type),
+                                          bitsize_zero_node));
+         gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
+         orig[0] = gimple_assign_lhs (lowpart);
+       }
       if (conv_code == ERROR_MARK)
-       gimple_assign_set_rhs_from_tree (gsi, orig[0]);
+       {
+         tree src_type = TREE_TYPE (orig[0]);
+         if (!useless_type_conversion_p (type, src_type))
+           {
+             gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
+                                   TYPE_VECTOR_SUBPARTS (src_type))
+                         && useless_type_conversion_p (TREE_TYPE (type),
+                                                       TREE_TYPE (src_type)));
+             tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
+             orig[0] = make_ssa_name (type);
+             gassign *assign = gimple_build_assign (orig[0], rhs);
+             gsi_insert_before (gsi, assign, GSI_SAME_STMT);
+           }
+         gimple_assign_set_rhs_from_tree (gsi, orig[0]);
+       }
       else
        gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
                                        NULL_TREE, NULL_TREE);
     }
   else
     {
-      tree mask_type;
+      /* If we combine a vector with a non-vector avoid cases where
+        we'll obviously end up with more GIMPLE stmts which is when
+        we'll later not fold this to a single insert into the vector
+        and we had a single extract originally.  See PR92819.  */
+      if (nelts == 2
+         && refnelts > 2
+         && orig[1] == error_mark_node
+         && !maybe_blend[0])
+       return false;
+      tree mask_type, perm_type, conv_src_type;
+      perm_type = TREE_TYPE (orig[0]);
+      conv_src_type = (nelts == refnelts
+                      ? perm_type
+                      : build_vector_type (TREE_TYPE (perm_type), nelts));
+      if (conv_code != ERROR_MARK
+         && !supportable_convert_operation (conv_code, type, conv_src_type,
+                                            &conv_code))
+       return false;
 
-      vec_perm_indices indices (sel, orig[1] ? 2 : 1, nelts);
-      if (!can_vec_perm_const_p (TYPE_MODE (type), indices))
+      /* Now that we know the number of elements of the source build the
+        permute vector.
+        ???  When the second vector has constant values we can shuffle
+        it and its source indexes to make the permutation supported.
+        For now it mimics a blend.  */
+      vec_perm_builder sel (refnelts, refnelts, 1);
+      bool all_same_p = true;
+      for (i = 0; i < elts.length (); ++i)
+       {
+         sel.quick_push (elts[i].second + elts[i].first * refnelts);
+         all_same_p &= known_eq (sel[i], sel[0]);
+       }
+      /* And fill the tail with "something".  It's really don't care,
+         and ideally we'd allow VEC_PERM to have a smaller destination
+        vector.  As a heuristic:
+
+        (a) if what we have so far duplicates a single element, make the
+            tail do the same
+
+        (b) otherwise preserve a uniform orig[0].  This facilitates
+            later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR.  */
+      for (; i < refnelts; ++i)
+       sel.quick_push (all_same_p
+                       ? sel[0]
+                       : (elts[0].second == 0 && elts[0].first == 0
+                          ? 0 : refnelts) + i);
+      vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
+      if (!can_vec_perm_const_p (TYPE_MODE (perm_type), indices))
        return false;
       mask_type
        = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
-                            nelts);
+                            refnelts);
       if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
          || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
-                      GET_MODE_SIZE (TYPE_MODE (type))))
+                      GET_MODE_SIZE (TYPE_MODE (perm_type))))
        return false;
-      op2 = vec_perm_indices_to_tree (mask_type, indices);
-      bool convert_orig0 = false;
+      tree op2 = vec_perm_indices_to_tree (mask_type, indices);
+      bool converted_orig1 = false;
+      gimple_seq stmts = NULL;
       if (!orig[1])
        orig[1] = orig[0];
       else if (orig[1] == error_mark_node
               && one_nonconstant)
        {
-         gimple_seq seq = NULL;
-         orig[1] = gimple_build_vector_from_val (&seq, UNKNOWN_LOCATION,
-                                                 type, one_nonconstant);
-         gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
-         convert_orig0 = true;
+         /* ???  We can see if we can safely convert to the original
+            element type.  */
+         converted_orig1 = conv_code != ERROR_MARK;
+         orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
+                                                 converted_orig1
+                                                 ? type : perm_type,
+                                                 one_nonconstant);
        }
       else if (orig[1] == error_mark_node)
        {
-         tree_vector_builder vec (type, nelts, 1);
-         for (unsigned i = 0; i < nelts; ++i)
-           if (constants[i])
+         /* ???  See if we can convert the vector to the original type.  */
+         converted_orig1 = conv_code != ERROR_MARK;
+         unsigned n = converted_orig1 ? nelts : refnelts;
+         tree_vector_builder vec (converted_orig1
+                                  ? type : perm_type, n, 1);
+         for (unsigned i = 0; i < n; ++i)
+           if (i < nelts && constants[i])
              vec.quick_push (constants[i]);
            else
              /* ??? Push a don't-care value.  */
              vec.quick_push (one_constant);
          orig[1] = vec.build ();
-         convert_orig0 = true;
        }
-      if (conv_code == ERROR_MARK)
-       gimple_assign_set_rhs_with_ops (gsi, VEC_PERM_EXPR, orig[0],
-                                       orig[1], op2);
-      else if (convert_orig0)
+      tree blend_op2 = NULL_TREE;
+      if (converted_orig1)
        {
-         gimple *conv
-           = gimple_build_assign (make_ssa_name (type), conv_code, orig[0]);
-         orig[0] = gimple_assign_lhs (conv);
-         gsi_insert_before (gsi, conv, GSI_SAME_STMT);
-         gimple_assign_set_rhs_with_ops (gsi, VEC_PERM_EXPR,
-                                         orig[0], orig[1], op2);
+         /* Make sure we can do a blend in the target type.  */
+         vec_perm_builder sel (nelts, nelts, 1);
+         for (i = 0; i < elts.length (); ++i)
+           sel.quick_push (elts[i].first
+                           ? elts[i].second + nelts : i);
+         vec_perm_indices indices (sel, 2, nelts);
+         if (!can_vec_perm_const_p (TYPE_MODE (type), indices))
+           return false;
+         mask_type
+           = build_vector_type (build_nonstandard_integer_type (elem_size, 1),
+                                nelts);
+         if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT
+             || maybe_ne (GET_MODE_SIZE (TYPE_MODE (mask_type)),
+                          GET_MODE_SIZE (TYPE_MODE (type))))
+           return false;
+         blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
        }
-      else
+      tree orig1_for_perm
+       = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
+      tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
+                              orig[0], orig1_for_perm, op2);
+      if (nelts != refnelts)
+       res = gimple_build (&stmts, BIT_FIELD_REF,
+                           conv_code != ERROR_MARK ? conv_src_type : type,
+                           res, TYPE_SIZE (type), bitsize_zero_node);
+      if (conv_code != ERROR_MARK)
+       res = gimple_build (&stmts, conv_code, type, res);
+      else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
        {
-         gimple *perm
-           = gimple_build_assign (make_ssa_name (TREE_TYPE (orig[0])),
-                                  VEC_PERM_EXPR, orig[0], orig[1], op2);
-         orig[0] = gimple_assign_lhs (perm);
-         gsi_insert_before (gsi, perm, GSI_SAME_STMT);
-         gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
-                                         NULL_TREE, NULL_TREE);
+         gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
+                               TYPE_VECTOR_SUBPARTS (perm_type))
+                     && useless_type_conversion_p (TREE_TYPE (type),
+                                                   TREE_TYPE (perm_type)));
+         res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
        }
+      /* Blend in the actual constant.  */
+      if (converted_orig1)
+       res = gimple_build (&stmts, VEC_PERM_EXPR, type,
+                           res, orig[1], blend_op2);
+      gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
+      gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
     }
   update_stmt (gsi_stmt (*gsi));
   return true;
@@ -2370,12 +2763,13 @@ pass_forwprop::execute (function *fun)
 
          /* If this statement sets an SSA_NAME to an address,
             try to propagate the address into the uses of the SSA_NAME.  */
-         if (code == ADDR_EXPR
-             /* Handle pointer conversions on invariant addresses
-                as well, as this is valid gimple.  */
-             || (CONVERT_EXPR_CODE_P (code)
-                 && TREE_CODE (rhs) == ADDR_EXPR
-                 && POINTER_TYPE_P (TREE_TYPE (lhs))))
+         if ((code == ADDR_EXPR
+              /* Handle pointer conversions on invariant addresses
+                 as well, as this is valid gimple.  */
+              || (CONVERT_EXPR_CODE_P (code)
+                  && TREE_CODE (rhs) == ADDR_EXPR
+                  && POINTER_TYPE_P (TREE_TYPE (lhs))))
+             && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
            {
              tree base = get_base_address (TREE_OPERAND (rhs, 0));
              if ((!base
@@ -2443,7 +2837,8 @@ pass_forwprop::execute (function *fun)
                    continue;
                  if (!is_gimple_assign (use_stmt)
                      || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
-                         && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR))
+                         && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
+                     || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
                    {
                      rewrite = false;
                      break;
@@ -2505,7 +2900,8 @@ pass_forwprop::execute (function *fun)
                  if (is_gimple_debug (use_stmt))
                    continue;
                  if (!is_gimple_assign (use_stmt)
-                     || gimple_assign_rhs_code (use_stmt) != BIT_FIELD_REF)
+                     || gimple_assign_rhs_code (use_stmt) != BIT_FIELD_REF
+                     || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
                    {
                      rewrite = false;
                      break;
@@ -2566,6 +2962,8 @@ pass_forwprop::execute (function *fun)
                      != TARGET_MEM_REF))
                {
                  tree use_lhs = gimple_assign_lhs (use_stmt);
+                 if (auto_var_p (use_lhs))
+                   DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
                  tree new_lhs = build1 (REALPART_EXPR,
                                         TREE_TYPE (TREE_TYPE (use_lhs)),
                                         unshare_expr (use_lhs));
@@ -2617,6 +3015,9 @@ pass_forwprop::execute (function *fun)
                    = tree_to_uhwi (TYPE_SIZE (elt_t));
                  unsigned HOST_WIDE_INT n
                    = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
+                 tree use_lhs = gimple_assign_lhs (use_stmt);
+                 if (auto_var_p (use_lhs))
+                   DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
                  for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
                    {
                      unsigned HOST_WIDE_INT ci = bi / elt_w;
@@ -2625,7 +3026,6 @@ pass_forwprop::execute (function *fun)
                        new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
                      else
                        new_rhs = build_zero_cst (elt_t);
-                     tree use_lhs = gimple_assign_lhs (use_stmt);
                      tree new_lhs = build3 (BIT_FIELD_REF,
                                             elt_t,
                                             unshare_expr (use_lhs),
@@ -2759,6 +3159,8 @@ pass_forwprop::execute (function *fun)
                    else if (code == CONSTRUCTOR
                             && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
                      changed = simplify_vector_constructor (&gsi);
+                   else if (code == ARRAY_REF)
+                     changed = simplify_count_trailing_zeroes (&gsi);
                    break;
                  }