]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/internal-fn.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / internal-fn.c
index 7fb44aa08fec3085764cc8c0a7531295a587af8b..8312d08aab2a9dd1c346aecd8a46ee4856deb322 100644 (file)
@@ -1,5 +1,5 @@
 /* Internal functions.
-   Copyright (C) 2011-2017 Free Software Foundation, Inc.
+   Copyright (C) 2011-2021 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -39,10 +39,22 @@ along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "dojump.h"
 #include "expr.h"
+#include "stringpool.h"
+#include "attribs.h"
+#include "asan.h"
 #include "ubsan.h"
 #include "recog.h"
 #include "builtins.h"
 #include "optabs-tree.h"
+#include "gimple-ssa.h"
+#include "tree-phinodes.h"
+#include "ssa-iterators.h"
+#include "explow.h"
+#include "rtl-iter.h"
+#include "gimple-range.h"
+
+/* For lang_hooks.types.type_for_mode.  */
+#include "langhooks.h"
 
 /* The names of each internal function, indexed by function number.  */
 const char *const internal_fn_name_array[] = {
@@ -58,6 +70,26 @@ const int internal_fn_flags_array[] = {
   0
 };
 
+/* Return the internal function called NAME, or IFN_LAST if there's
+   no such function.  */
+
+internal_fn
+lookup_internal_fn (const char *name)
+{
+  typedef hash_map<nofree_string_hash, internal_fn> name_to_fn_map_type;
+  static name_to_fn_map_type *name_to_fn_map;
+
+  if (!name_to_fn_map)
+    {
+      name_to_fn_map = new name_to_fn_map_type (IFN_LAST);
+      for (unsigned int i = 0; i < IFN_LAST; ++i)
+       name_to_fn_map->put (internal_fn_name (internal_fn (i)),
+                            internal_fn (i));
+    }
+  internal_fn *entry = name_to_fn_map->get (name);
+  return entry ? *entry : IFN_LAST;
+}
+
 /* Fnspec of each internal function, indexed by function number.  */
 const_tree internal_fn_fnspec_array[IFN_LAST + 1];
 
@@ -66,7 +98,7 @@ init_internal_fns ()
 {
 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
   if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \
-    build_string ((int) sizeof (FNSPEC), FNSPEC ? FNSPEC : "");
+    build_string ((int) sizeof (FNSPEC) - 1, FNSPEC ? FNSPEC : "");
 #include "internal-fn.def"
   internal_fn_fnspec_array[IFN_LAST] = 0;
 }
@@ -76,14 +108,34 @@ init_internal_fns ()
 #define not_direct { -2, -2, false }
 #define mask_load_direct { -1, 2, false }
 #define load_lanes_direct { -1, -1, false }
+#define mask_load_lanes_direct { -1, -1, false }
+#define gather_load_direct { 3, 1, false }
+#define len_load_direct { -1, -1, false }
 #define mask_store_direct { 3, 2, false }
 #define store_lanes_direct { 0, 0, false }
+#define mask_store_lanes_direct { 0, 0, false }
+#define vec_cond_mask_direct { 1, 0, false }
+#define vec_cond_direct { 2, 0, false }
+#define scatter_store_direct { 3, 1, false }
+#define len_store_direct { 3, 3, false }
+#define vec_set_direct { 3, 3, false }
 #define unary_direct { 0, 0, true }
 #define binary_direct { 0, 0, true }
+#define ternary_direct { 0, 0, true }
+#define cond_unary_direct { 1, 1, true }
+#define cond_binary_direct { 1, 1, true }
+#define cond_ternary_direct { 1, 1, true }
+#define while_direct { 0, 2, false }
+#define fold_extract_direct { 2, 2, false }
+#define fold_left_direct { 1, 1, false }
+#define mask_fold_left_direct { 1, 1, false }
+#define check_ptrs_direct { 0, 0, false }
 
 const direct_internal_fn_info direct_internal_fn_array[IFN_LAST + 1] = {
 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) not_direct,
 #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) TYPE##_direct,
+#define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
+                                    UNSIGNED_OPTAB, TYPE) TYPE##_direct,
 #include "internal-fn.def"
   not_direct
 };
@@ -109,7 +161,7 @@ get_multi_vector_move (tree array_type, convert_optab optab)
 static void
 expand_load_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
-  struct expand_operand ops[2];
+  class expand_operand ops[2];
   tree type, lhs, rhs;
   rtx target, mem;
 
@@ -126,6 +178,8 @@ expand_load_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
   create_output_operand (&ops[0], target, TYPE_MODE (type));
   create_fixed_operand (&ops[1], mem);
   expand_insn (get_multi_vector_move (type, optab), 2, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* Expand STORE_LANES call STMT using optab OPTAB.  */
@@ -133,7 +187,7 @@ expand_load_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 static void
 expand_store_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
-  struct expand_operand ops[2];
+  class expand_operand ops[2];
   tree type, lhs, rhs;
   rtx target, reg;
 
@@ -166,6 +220,50 @@ expand_GOMP_USE_SIMT (internal_fn, gcall *)
   gcc_unreachable ();
 }
 
+/* This should get expanded in omp_device_lower pass.  */
+
+static void
+expand_GOMP_SIMT_ENTER (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+/* Allocate per-lane storage and begin non-uniform execution region.  */
+
+static void
+expand_GOMP_SIMT_ENTER_ALLOC (internal_fn, gcall *stmt)
+{
+  rtx target;
+  tree lhs = gimple_call_lhs (stmt);
+  if (lhs)
+    target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  else
+    target = gen_reg_rtx (Pmode);
+  rtx size = expand_normal (gimple_call_arg (stmt, 0));
+  rtx align = expand_normal (gimple_call_arg (stmt, 1));
+  class expand_operand ops[3];
+  create_output_operand (&ops[0], target, Pmode);
+  create_input_operand (&ops[1], size, Pmode);
+  create_input_operand (&ops[2], align, Pmode);
+  gcc_assert (targetm.have_omp_simt_enter ());
+  expand_insn (targetm.code_for_omp_simt_enter, 3, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
+}
+
+/* Deallocate per-lane storage and leave non-uniform execution region.  */
+
+static void
+expand_GOMP_SIMT_EXIT (internal_fn, gcall *stmt)
+{
+  gcc_checking_assert (!gimple_call_lhs (stmt));
+  rtx arg = expand_normal (gimple_call_arg (stmt, 0));
+  class expand_operand ops[1];
+  create_input_operand (&ops[0], arg, Pmode);
+  gcc_assert (targetm.have_omp_simt_exit ());
+  expand_insn (targetm.code_for_omp_simt_exit, 1, ops);
+}
+
 /* Lane index on SIMT targets: thread index in the warp on NVPTX.  On targets
    without SIMT execution this should be expanded in omp_device_lower pass.  */
 
@@ -203,11 +301,13 @@ expand_GOMP_SIMT_LAST_LANE (internal_fn, gcall *stmt)
   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   rtx cond = expand_normal (gimple_call_arg (stmt, 0));
   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
-  struct expand_operand ops[2];
+  class expand_operand ops[2];
   create_output_operand (&ops[0], target, mode);
   create_input_operand (&ops[1], cond, mode);
   gcc_assert (targetm.have_omp_simt_last_lane ());
   expand_insn (targetm.code_for_omp_simt_last_lane, 2, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* Non-transparent predicate used in SIMT lowering of OpenMP "ordered".  */
@@ -222,11 +322,13 @@ expand_GOMP_SIMT_ORDERED_PRED (internal_fn, gcall *stmt)
   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   rtx ctr = expand_normal (gimple_call_arg (stmt, 0));
   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
-  struct expand_operand ops[2];
+  class expand_operand ops[2];
   create_output_operand (&ops[0], target, mode);
   create_input_operand (&ops[1], ctr, mode);
   gcc_assert (targetm.have_omp_simt_ordered ());
   expand_insn (targetm.code_for_omp_simt_ordered, 2, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* "Or" boolean reduction across SIMT lanes: return non-zero in all lanes if
@@ -242,11 +344,13 @@ expand_GOMP_SIMT_VOTE_ANY (internal_fn, gcall *stmt)
   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   rtx cond = expand_normal (gimple_call_arg (stmt, 0));
   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
-  struct expand_operand ops[2];
+  class expand_operand ops[2];
   create_output_operand (&ops[0], target, mode);
   create_input_operand (&ops[1], cond, mode);
   gcc_assert (targetm.have_omp_simt_vote_any ());
   expand_insn (targetm.code_for_omp_simt_vote_any, 2, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* Exchange between SIMT lanes with a "butterfly" pattern: source lane index
@@ -263,12 +367,14 @@ expand_GOMP_SIMT_XCHG_BFLY (internal_fn, gcall *stmt)
   rtx src = expand_normal (gimple_call_arg (stmt, 0));
   rtx idx = expand_normal (gimple_call_arg (stmt, 1));
   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
-  struct expand_operand ops[3];
+  class expand_operand ops[3];
   create_output_operand (&ops[0], target, mode);
   create_input_operand (&ops[1], src, mode);
   create_input_operand (&ops[2], idx, SImode);
   gcc_assert (targetm.have_omp_simt_xchg_bfly ());
   expand_insn (targetm.code_for_omp_simt_xchg_bfly, 3, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* Exchange between SIMT lanes according to given source lane index.  */
@@ -284,12 +390,14 @@ expand_GOMP_SIMT_XCHG_IDX (internal_fn, gcall *stmt)
   rtx src = expand_normal (gimple_call_arg (stmt, 0));
   rtx idx = expand_normal (gimple_call_arg (stmt, 1));
   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
-  struct expand_operand ops[3];
+  class expand_operand ops[3];
   create_output_operand (&ops[0], target, mode);
   create_input_operand (&ops[1], src, mode);
   create_input_operand (&ops[2], idx, SImode);
   gcc_assert (targetm.have_omp_simt_xchg_idx ());
   expand_insn (targetm.code_for_omp_simt_xchg_idx, 3, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
 /* This should get expanded in adjust_simduid_builtins.  */
@@ -358,6 +466,14 @@ expand_UBSAN_VPTR (internal_fn, gcall *)
 
 /* This should get expanded in the sanopt pass.  */
 
+static void
+expand_UBSAN_PTR (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+/* This should get expanded in the sanopt pass.  */
+
 static void
 expand_UBSAN_OBJECT_SIZE (internal_fn, gcall *)
 {
@@ -366,6 +482,111 @@ expand_UBSAN_OBJECT_SIZE (internal_fn, gcall *)
 
 /* This should get expanded in the sanopt pass.  */
 
+static void
+expand_HWASAN_CHECK (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+/* For hwasan stack tagging:
+   Clear tags on the dynamically allocated space.
+   For use after an object dynamically allocated on the stack goes out of
+   scope.  */
+static void
+expand_HWASAN_ALLOCA_UNPOISON (internal_fn, gcall *gc)
+{
+  gcc_assert (Pmode == ptr_mode);
+  tree restored_position = gimple_call_arg (gc, 0);
+  rtx restored_rtx = expand_expr (restored_position, NULL_RTX, VOIDmode,
+                                 EXPAND_NORMAL);
+  rtx func = init_one_libfunc ("__hwasan_tag_memory");
+  rtx off = expand_simple_binop (Pmode, MINUS, restored_rtx,
+                                stack_pointer_rtx, NULL_RTX, 0,
+                                OPTAB_WIDEN);
+  emit_library_call_value (func, NULL_RTX, LCT_NORMAL, VOIDmode,
+                          virtual_stack_dynamic_rtx, Pmode,
+                          HWASAN_STACK_BACKGROUND, QImode,
+                          off, Pmode);
+}
+
+/* For hwasan stack tagging:
+   Return a tag to be used for a dynamic allocation.  */
+static void
+expand_HWASAN_CHOOSE_TAG (internal_fn, gcall *gc)
+{
+  tree tag = gimple_call_lhs (gc);
+  rtx target = expand_expr (tag, NULL_RTX, VOIDmode, EXPAND_NORMAL);
+  machine_mode mode = GET_MODE (target);
+  gcc_assert (mode == QImode);
+
+  rtx base_tag = targetm.memtag.extract_tag (hwasan_frame_base (), NULL_RTX);
+  gcc_assert (base_tag);
+  rtx tag_offset = gen_int_mode (hwasan_current_frame_tag (), QImode);
+  rtx chosen_tag = expand_simple_binop (QImode, PLUS, base_tag, tag_offset,
+                                       target, /* unsignedp = */1,
+                                       OPTAB_WIDEN);
+  chosen_tag = hwasan_truncate_to_tag_size (chosen_tag, target);
+
+  /* Really need to put the tag into the `target` RTX.  */
+  if (chosen_tag != target)
+    {
+      rtx temp = chosen_tag;
+      gcc_assert (GET_MODE (chosen_tag) == mode);
+      emit_move_insn (target, temp);
+    }
+
+  hwasan_increment_frame_tag ();
+}
+
+/* For hwasan stack tagging:
+   Tag a region of space in the shadow stack according to the base pointer of
+   an object on the stack.  N.b. the length provided in the internal call is
+   required to be aligned to HWASAN_TAG_GRANULE_SIZE.  */
+static void
+expand_HWASAN_MARK (internal_fn, gcall *gc)
+{
+  gcc_assert (ptr_mode == Pmode);
+  HOST_WIDE_INT flag = tree_to_shwi (gimple_call_arg (gc, 0));
+  bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
+
+  tree base = gimple_call_arg (gc, 1);
+  gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
+  rtx base_rtx = expand_normal (base);
+
+  rtx tag = is_poison ? HWASAN_STACK_BACKGROUND
+    : targetm.memtag.extract_tag (base_rtx, NULL_RTX);
+  rtx address = targetm.memtag.untagged_pointer (base_rtx, NULL_RTX);
+
+  tree len = gimple_call_arg (gc, 2);
+  rtx r_len = expand_normal (len);
+
+  rtx func = init_one_libfunc ("__hwasan_tag_memory");
+  emit_library_call (func, LCT_NORMAL, VOIDmode, address, Pmode,
+                    tag, QImode, r_len, Pmode);
+}
+
+/* For hwasan stack tagging:
+   Store a tag into a pointer.  */
+static void
+expand_HWASAN_SET_TAG (internal_fn, gcall *gc)
+{
+  gcc_assert (ptr_mode == Pmode);
+  tree g_target = gimple_call_lhs (gc);
+  tree g_ptr = gimple_call_arg (gc, 0);
+  tree g_tag = gimple_call_arg (gc, 1);
+
+  rtx ptr = expand_normal (g_ptr);
+  rtx tag = expand_expr (g_tag, NULL_RTX, QImode, EXPAND_NORMAL);
+  rtx target = expand_normal (g_target);
+
+  rtx untagged = targetm.memtag.untagged_pointer (ptr, target);
+  rtx tagged_value = targetm.memtag.set_tag (untagged, tag, target);
+  if (tagged_value != target)
+    emit_move_insn (target, tagged_value);
+}
+
+/* This should get expanded in the sanopt pass.  */
+
 static void
 expand_ASAN_CHECK (internal_fn, gcall *)
 {
@@ -380,6 +601,21 @@ expand_ASAN_MARK (internal_fn, gcall *)
   gcc_unreachable ();
 }
 
+/* This should get expanded in the sanopt pass.  */
+
+static void
+expand_ASAN_POISON (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+/* This should get expanded in the sanopt pass.  */
+
+static void
+expand_ASAN_POISON_USE (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
 
 /* This should get expanded in the tsan pass.  */
 
@@ -398,86 +634,6 @@ expand_FALLTHROUGH (internal_fn, gcall *call)
            "invalid use of attribute %<fallthrough%>");
 }
 
-/* Helper function for expand_addsub_overflow.  Return 1
-   if ARG interpreted as signed in its precision is known to be always
-   positive or 2 if ARG is known to be always negative, or 3 if ARG may
-   be positive or negative.  */
-
-static int
-get_range_pos_neg (tree arg)
-{
-  if (arg == error_mark_node)
-    return 3;
-
-  int prec = TYPE_PRECISION (TREE_TYPE (arg));
-  int cnt = 0;
-  if (TREE_CODE (arg) == INTEGER_CST)
-    {
-      wide_int w = wi::sext (arg, prec);
-      if (wi::neg_p (w))
-       return 2;
-      else
-       return 1;
-    }
-  while (CONVERT_EXPR_P (arg)
-        && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
-        && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
-    {
-      arg = TREE_OPERAND (arg, 0);
-      /* Narrower value zero extended into wider type
-        will always result in positive values.  */
-      if (TYPE_UNSIGNED (TREE_TYPE (arg))
-         && TYPE_PRECISION (TREE_TYPE (arg)) < prec)
-       return 1;
-      prec = TYPE_PRECISION (TREE_TYPE (arg));
-      if (++cnt > 30)
-       return 3;
-    }
-
-  if (TREE_CODE (arg) != SSA_NAME)
-    return 3;
-  wide_int arg_min, arg_max;
-  while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
-    {
-      gimple *g = SSA_NAME_DEF_STMT (arg);
-      if (is_gimple_assign (g)
-         && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
-       {
-         tree t = gimple_assign_rhs1 (g);
-         if (INTEGRAL_TYPE_P (TREE_TYPE (t))
-             && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
-           {
-             if (TYPE_UNSIGNED (TREE_TYPE (t))
-                 && TYPE_PRECISION (TREE_TYPE (t)) < prec)
-               return 1;
-             prec = TYPE_PRECISION (TREE_TYPE (t));
-             arg = t;
-             if (++cnt > 30)
-               return 3;
-             continue;
-           }
-       }
-      return 3;
-    }
-  if (TYPE_UNSIGNED (TREE_TYPE (arg)))
-    {
-      /* For unsigned values, the "positive" range comes
-        below the "negative" range.  */
-      if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED))
-       return 1;
-      if (wi::neg_p (wi::sext (arg_min, prec), SIGNED))
-       return 2;
-    }
-  else
-    {
-      if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED))
-       return 1;
-      if (wi::neg_p (wi::sext (arg_max, prec), SIGNED))
-       return 2;
-    }
-  return 3;
-}
-
 /* Return minimum precision needed to represent all values
    of ARG in SIGNed integral type.  */
 
@@ -497,7 +653,7 @@ get_min_precision (tree arg, signop sign)
          p = wi::min_precision (w, sign);
        }
       else
-       p = wi::min_precision (arg, sign);
+       p = wi::min_precision (wi::to_wide (arg), sign);
       return MIN (p, prec);
     }
   while (CONVERT_EXPR_P (arg)
@@ -516,10 +672,21 @@ get_min_precision (tree arg, signop sign)
       if (++cnt > 30)
        return prec + (orig_sign != sign);
     }
+  if (CONVERT_EXPR_P (arg)
+      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
+      && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) > prec)
+    {
+      /* We have e.g. (unsigned short) y_2 where int y_2 = (int) x_1(D);
+        If y_2's min precision is smaller than prec, return that.  */
+      int oprec = get_min_precision (TREE_OPERAND (arg, 0), sign);
+      if (oprec < prec)
+       return oprec + (orig_sign != sign);
+    }
   if (TREE_CODE (arg) != SSA_NAME)
     return prec + (orig_sign != sign);
-  wide_int arg_min, arg_max;
-  while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE)
+  value_range r;
+  while (!get_global_range_query ()->range_of_expr (r, arg)
+        || r.kind () != VR_RANGE)
     {
       gimple *g = SSA_NAME_DEF_STMT (arg);
       if (is_gimple_assign (g)
@@ -547,14 +714,14 @@ get_min_precision (tree arg, signop sign)
     }
   if (sign == TYPE_SIGN (TREE_TYPE (arg)))
     {
-      int p1 = wi::min_precision (arg_min, sign);
-      int p2 = wi::min_precision (arg_max, sign);
+      int p1 = wi::min_precision (r.lower_bound (), sign);
+      int p2 = wi::min_precision (r.upper_bound (), sign);
       p1 = MAX (p1, p2);
       prec = MIN (prec, p1);
     }
-  else if (sign == UNSIGNED && !wi::neg_p (arg_min, SIGNED))
+  else if (sign == UNSIGNED && !wi::neg_p (r.lower_bound (), SIGNED))
     {
-      int p = wi::min_precision (arg_max, UNSIGNED);
+      int p = wi::min_precision (r.upper_bound (), UNSIGNED);
       prec = MIN (prec, p);
     }
   return prec + (orig_sign != sign);
@@ -580,9 +747,10 @@ expand_arith_set_overflow (tree lhs, rtx target)
 
 static void
 expand_arith_overflow_result_store (tree lhs, rtx target,
-                                   machine_mode mode, rtx res)
+                                   scalar_int_mode mode, rtx res)
 {
-  machine_mode tgtmode = GET_MODE_INNER (GET_MODE (target));
+  scalar_int_mode tgtmode
+    = as_a <scalar_int_mode> (GET_MODE_INNER (GET_MODE (target)));
   rtx lres = res;
   if (tgtmode != mode)
     {
@@ -592,7 +760,7 @@ expand_arith_overflow_result_store (tree lhs, rtx target,
       gcc_assert (GET_MODE_PRECISION (tgtmode) < GET_MODE_PRECISION (mode));
       do_compare_rtx_and_jump (res, convert_modes (mode, tgtmode, lres, uns),
                               EQ, true, mode, NULL_RTX, NULL, done_label,
-                              PROB_VERY_LIKELY);
+                              profile_probability::very_likely ());
       expand_arith_set_overflow (lhs, target);
       emit_label (done_label);
     }
@@ -620,7 +788,7 @@ expand_arith_overflow_result_store (tree lhs, rtx target,
        }
       do_compare_rtx_and_jump (res, lres,
                               EQ, true, tgtmode, NULL_RTX, NULL, done_label,
-                              PROB_VERY_LIKELY);
+                              profile_probability::very_likely ());
       expand_arith_set_overflow (lhs, target);
       emit_label (done_label);
     }
@@ -645,7 +813,7 @@ expand_ubsan_result_store (rtx target, rtx res)
 /* Add sub/add overflow checking to the statement STMT.
    CODE says whether the operation is +, or -.  */
 
-static void
+void
 expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                        tree arg0, tree arg1, bool unsr_p, bool uns0_p,
                        bool uns1_p, bool is_ubsan, tree *datap)
@@ -657,7 +825,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
   do_pending_stack_adjust ();
   rtx op0 = expand_normal (arg0);
   rtx op1 = expand_normal (arg1);
-  machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
   int prec = GET_MODE_PRECISION (mode);
   rtx sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
   bool do_xor = false;
@@ -748,7 +916,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                                        : usubv4_optab, mode);
       if (icode != CODE_FOR_nothing)
        {
-         struct expand_operand ops[4];
+         class expand_operand ops[4];
          rtx_insn *last = get_last_insn ();
 
          res = gen_reg_rtx (mode);
@@ -763,7 +931,8 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                  && JUMP_P (last)
                  && any_condjump_p (last)
                  && !find_reg_note (last, REG_BR_PROB, 0))
-               add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
+               add_reg_br_prob_note (last,
+                                     profile_probability::very_unlikely ());
              emit_jump (done_label);
              goto do_error_label;
            }
@@ -790,7 +959,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
        tem = op1;
       do_compare_rtx_and_jump (res, tem, code == PLUS_EXPR ? GEU : LEU,
                               true, mode, NULL_RTX, NULL, done_label,
-                              PROB_VERY_LIKELY);
+                              profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -805,7 +974,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                              code == PLUS_EXPR ? res : op0, sgn,
                              NULL_RTX, false, OPTAB_LIB_WIDEN);
       do_compare_rtx_and_jump (tem, op1, GEU, true, mode, NULL_RTX, NULL,
-                              done_label, PROB_VERY_LIKELY);
+                              done_label, profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -847,9 +1016,9 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
       else if (pos_neg == 3)
        /* If ARG0 is not known to be always positive, check at runtime.  */
        do_compare_rtx_and_jump (op0, const0_rtx, LT, false, mode, NULL_RTX,
-                                NULL, do_error, PROB_VERY_UNLIKELY);
+                                NULL, do_error, profile_probability::very_unlikely ());
       do_compare_rtx_and_jump (op1, op0, LEU, true, mode, NULL_RTX, NULL,
-                              done_label, PROB_VERY_LIKELY);
+                              done_label, profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -863,7 +1032,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
       rtx tem = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
                              OPTAB_LIB_WIDEN);
       do_compare_rtx_and_jump (op0, tem, LTU, true, mode, NULL_RTX, NULL,
-                              done_label, PROB_VERY_LIKELY);
+                              done_label, profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -875,7 +1044,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
       res = expand_binop (mode, add_optab, op0, op1, NULL_RTX, false,
                          OPTAB_LIB_WIDEN);
       do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
-                              NULL, do_error, PROB_VERY_UNLIKELY);
+                              NULL, do_error, profile_probability::very_unlikely ());
       rtx tem = op1;
       /* The operation is commutative, so we can pick operand to compare
         against.  For prec <= BITS_PER_WORD, I think preferring REG operand
@@ -889,7 +1058,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
          : CONST_SCALAR_INT_P (op0))
        tem = op0;
       do_compare_rtx_and_jump (res, tem, GEU, true, mode, NULL_RTX, NULL,
-                              done_label, PROB_VERY_LIKELY);
+                              done_label, profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -917,7 +1086,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                                    ? and_optab : ior_optab,
                              op0, res, NULL_RTX, false, OPTAB_LIB_WIDEN);
          do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
        }
       else
        {
@@ -925,17 +1094,17 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
          do_compare_rtx_and_jump (op1, const0_rtx,
                                   code == MINUS_EXPR ? GE : LT, false, mode,
                                   NULL_RTX, NULL, do_ior_label,
-                                  PROB_EVEN);
+                                  profile_probability::even ());
          tem = expand_binop (mode, and_optab, op0, res, NULL_RTX, false,
                              OPTAB_LIB_WIDEN);
          do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
          emit_jump (do_error);
          emit_label (do_ior_label);
          tem = expand_binop (mode, ior_optab, op0, res, NULL_RTX, false,
                              OPTAB_LIB_WIDEN);
          do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
        }
       goto do_error_label;
     }
@@ -949,13 +1118,13 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                          OPTAB_LIB_WIDEN);
       rtx_code_label *op0_geu_op1 = gen_label_rtx ();
       do_compare_rtx_and_jump (op0, op1, GEU, true, mode, NULL_RTX, NULL,
-                              op0_geu_op1, PROB_EVEN);
+                              op0_geu_op1, profile_probability::even ());
       do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
-                              NULL, done_label, PROB_VERY_LIKELY);
+                              NULL, done_label, profile_probability::very_likely ());
       emit_jump (do_error);
       emit_label (op0_geu_op1);
       do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
-                              NULL, done_label, PROB_VERY_LIKELY);
+                              NULL, done_label, profile_probability::very_likely ());
       goto do_error_label;
     }
 
@@ -968,7 +1137,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                                     : subv4_optab, mode);
     if (icode != CODE_FOR_nothing)
       {
-       struct expand_operand ops[4];
+       class expand_operand ops[4];
        rtx_insn *last = get_last_insn ();
 
        res = gen_reg_rtx (mode);
@@ -983,7 +1152,8 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
                && JUMP_P (last)
                && any_condjump_p (last)
                && !find_reg_note (last, REG_BR_PROB, 0))
-             add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
+             add_reg_br_prob_note (last, 
+                                   profile_probability::very_unlikely ());
            emit_jump (done_label);
            goto do_error_label;
          }
@@ -1043,7 +1213,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
 
        /* No overflow if the result has bit sign cleared.  */
        do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
-                                NULL, done_label, PROB_VERY_LIKELY);
+                                NULL, done_label, profile_probability::very_likely ());
       }
 
     /* Compare the result of the operation with the first operand.
@@ -1054,7 +1224,7 @@ expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
       do_compare_rtx_and_jump (res, op0,
                               (pos_neg == 1) ^ (code == MINUS_EXPR) ? GE : LE,
                               false, mode, NULL_RTX, NULL, done_label,
-                              PROB_VERY_LIKELY);
+                              profile_probability::very_likely ());
   }
 
  do_error_label:
@@ -1107,7 +1277,7 @@ expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
   do_pending_stack_adjust ();
   op1 = expand_normal (arg1);
 
-  machine_mode mode = TYPE_MODE (TREE_TYPE (arg1));
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg1));
   if (lhs)
     {
       target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
@@ -1118,7 +1288,7 @@ expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
   enum insn_code icode = optab_handler (negv3_optab, mode);
   if (icode != CODE_FOR_nothing)
     {
-      struct expand_operand ops[3];
+      class expand_operand ops[3];
       rtx_insn *last = get_last_insn ();
 
       res = gen_reg_rtx (mode);
@@ -1132,7 +1302,8 @@ expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
              && JUMP_P (last)
              && any_condjump_p (last)
              && !find_reg_note (last, REG_BR_PROB, 0))
-           add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
+           add_reg_br_prob_note (last, 
+                                 profile_probability::very_unlikely ());
          emit_jump (done_label);
         }
       else
@@ -1151,7 +1322,7 @@ expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
       /* Compare the operand with the most negative value.  */
       rtx minv = expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1)));
       do_compare_rtx_and_jump (op1, minv, NE, true, mode, NULL_RTX, NULL,
-                              done_label, PROB_VERY_LIKELY);
+                              done_label, profile_probability::very_likely ());
     }
 
   emit_label (do_error);
@@ -1180,6 +1351,35 @@ expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
     }
 }
 
+/* Return true if UNS WIDEN_MULT_EXPR with result mode WMODE and operand
+   mode MODE can be expanded without using a libcall.  */
+
+static bool
+can_widen_mult_without_libcall (scalar_int_mode wmode, scalar_int_mode mode,
+                               rtx op0, rtx op1, bool uns)
+{
+  if (find_widening_optab_handler (umul_widen_optab, wmode, mode)
+      != CODE_FOR_nothing)
+    return true;
+    
+  if (find_widening_optab_handler (smul_widen_optab, wmode, mode)
+      != CODE_FOR_nothing)
+    return true;
+
+  rtx_insn *last = get_last_insn ();
+  if (CONSTANT_P (op0))
+    op0 = convert_modes (wmode, mode, op0, uns);
+  else
+    op0 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 1);
+  if (CONSTANT_P (op1))
+    op1 = convert_modes (wmode, mode, op1, uns);
+  else
+    op1 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 2);
+  rtx ret = expand_mult (wmode, op0, op1, NULL_RTX, uns, true);
+  delete_insns_since (last);
+  return ret != NULL_RTX;
+} 
+
 /* Add mul overflow checking to the statement STMT.  */
 
 static void
@@ -1201,7 +1401,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
   op0 = expand_normal (arg0);
   op1 = expand_normal (arg1);
 
-  machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
   bool uns = unsr_p;
   if (lhs)
     {
@@ -1284,15 +1484,46 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          ops.location = loc;
          res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
          do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
          goto do_error_label;
        case 3:
+         if (get_min_precision (arg1, UNSIGNED)
+             + get_min_precision (arg0, SIGNED) <= GET_MODE_PRECISION (mode))
+           {
+             /* If the first operand is sign extended from narrower type, the
+                second operand is zero extended from narrower type and
+                the sum of the two precisions is smaller or equal to the
+                result precision: if the first argument is at runtime
+                non-negative, maximum result will be 0x7e81 or 0x7f..fe80..01
+                and there will be no overflow, if the first argument is
+                negative and the second argument zero, the result will be
+                0 and there will be no overflow, if the first argument is
+                negative and the second argument positive, the result when
+                treated as signed will be negative (minimum -0x7f80 or
+                -0x7f..f80..0) there there will be always overflow.  So, do
+                res = (U) (s1 * u2)
+                ovf = (S) res < 0  */
+             struct separate_ops ops;
+             ops.code = MULT_EXPR;
+             ops.type
+               = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
+                                                 1);
+             ops.op0 = make_tree (ops.type, op0);
+             ops.op1 = make_tree (ops.type, op1);
+             ops.op2 = NULL_TREE;
+             ops.location = loc;
+             res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
+             do_compare_rtx_and_jump (res, const0_rtx, GE, false,
+                                      mode, NULL_RTX, NULL, done_label,
+                                      profile_probability::very_likely ());
+             goto do_error_label;
+           }
          rtx_code_label *do_main_label;
          do_main_label = gen_label_rtx ();
          do_compare_rtx_and_jump (op0, const0_rtx, GE, false, mode, NULL_RTX,
-                                  NULL, do_main_label, PROB_VERY_LIKELY);
+                                  NULL, do_main_label, profile_probability::very_likely ());
          do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
-                                  NULL, do_main_label, PROB_VERY_LIKELY);
+                                  NULL, do_main_label, profile_probability::very_likely ());
          expand_arith_set_overflow (lhs, target);
          emit_label (do_main_label);
          goto do_main;
@@ -1304,7 +1535,16 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
   /* u1 * u2 -> sr  */
   if (uns0_p && uns1_p && !unsr_p)
     {
-      uns = true;
+      if ((pos_neg0 | pos_neg1) == 1)
+       {
+         /* If both arguments are zero extended from narrower types,
+            the MSB will be clear on both and so we can pretend it is
+            a normal s1 * s2 -> sr multiplication.  */
+         uns0_p = false;
+         uns1_p = false;
+       }
+      else
+       uns = true;
       /* Rest of handling of this case after res is computed.  */
       goto do_main;
     }
@@ -1329,15 +1569,15 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          ops.location = loc;
          res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
          do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
          do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
-                                  NULL, do_error, PROB_VERY_UNLIKELY);
+                                  NULL, do_error, profile_probability::very_unlikely ());
          int prec;
          prec = GET_MODE_PRECISION (mode);
          rtx sgn;
          sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
          do_compare_rtx_and_jump (op1, sgn, EQ, true, mode, NULL_RTX,
-                                  NULL, done_label, PROB_VERY_LIKELY);
+                                  NULL, done_label, profile_probability::very_likely ());
          goto do_error_label;
        case 3:
          /* Rest of handling of this case after res is computed.  */
@@ -1350,7 +1590,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
   /* s1 * s2 -> ur  */
   if (!uns0_p && !uns1_p && unsr_p)
     {
-      rtx tem, tem2;
+      rtx tem;
       switch (pos_neg0 | pos_neg1)
        {
        case 1: /* Both operands known to be non-negative.  */
@@ -1380,11 +1620,40 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
              ops.op2 = NULL_TREE;
              ops.location = loc;
              res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
-             tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
-                                 OPTAB_LIB_WIDEN);
-             do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode,
-                                      NULL_RTX, NULL, done_label,
-                                      PROB_VERY_LIKELY);
+             do_compare_rtx_and_jump (pos_neg0 == 1 ? op0 : op1, const0_rtx, EQ,
+                                      true, mode, NULL_RTX, NULL, done_label,
+                                      profile_probability::very_likely ());
+             goto do_error_label;
+           }
+         if (get_min_precision (arg0, SIGNED)
+             + get_min_precision (arg1, SIGNED) <= GET_MODE_PRECISION (mode))
+           {
+             /* If both operands are sign extended from narrower types and
+                the sum of the two precisions is smaller or equal to the
+                result precision: if both arguments are at runtime
+                non-negative, maximum result will be 0x3f01 or 0x3f..f0..01
+                and there will be no overflow, if both arguments are negative,
+                maximum result will be 0x40..00 and there will be no overflow
+                either, if one argument is positive and the other argument
+                negative, the result when treated as signed will be negative
+                and there will be always overflow, and if one argument is
+                zero and the other negative the result will be zero and no
+                overflow.  So, do
+                res = (U) (s1 * s2)
+                ovf = (S) res < 0  */
+             struct separate_ops ops;
+             ops.code = MULT_EXPR;
+             ops.type
+               = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
+                                                 1);
+             ops.op0 = make_tree (ops.type, op0);
+             ops.op1 = make_tree (ops.type, op1);
+             ops.op2 = NULL_TREE;
+             ops.location = loc;
+             res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
+             do_compare_rtx_and_jump (res, const0_rtx, GE, false,
+                                      mode, NULL_RTX, NULL, done_label,
+                                      profile_probability::very_likely ());
              goto do_error_label;
            }
          /* The general case, do all the needed comparisons at runtime.  */
@@ -1401,7 +1670,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
                              OPTAB_LIB_WIDEN);
          do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
-                                  NULL, after_negate_label, PROB_VERY_LIKELY);
+                                  NULL, after_negate_label, profile_probability::very_likely ());
          /* Both arguments negative here, negate them and continue with
             normal unsigned overflow checking multiplication.  */
          emit_move_insn (op0, expand_unop (mode, neg_optab, op0,
@@ -1414,16 +1683,23 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          arg1 = error_mark_node;
          emit_jump (do_main_label);
          emit_label (after_negate_label);
-         tem2 = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
-                              OPTAB_LIB_WIDEN);
-         do_compare_rtx_and_jump (tem2, const0_rtx, GE, false, mode, NULL_RTX,
-                                  NULL, do_main_label, PROB_VERY_LIKELY);
+         tem = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
+                             OPTAB_LIB_WIDEN);
+         do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
+                                  NULL, do_main_label,
+                                  profile_probability::very_likely ());
          /* One argument is negative here, the other positive.  This
             overflows always, unless one of the arguments is 0.  But
             if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1
             is, thus we can keep do_main code oring in overflow as is.  */
-         do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, NULL_RTX,
-                                  NULL, do_main_label, PROB_VERY_LIKELY);
+         if (pos_neg0 != 2)
+           do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
+                                    NULL, do_main_label,
+                                    profile_probability::very_unlikely ());
+         if (pos_neg1 != 2)
+           do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
+                                    NULL, do_main_label,
+                                    profile_probability::very_unlikely ());
          expand_arith_set_overflow (lhs, target);
          emit_label (do_main_label);
          goto do_main;
@@ -1436,9 +1712,52 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
   type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), uns);
   sign = uns ? UNSIGNED : SIGNED;
   icode = optab_handler (uns ? umulv4_optab : mulv4_optab, mode);
+  if (uns
+      && (integer_pow2p (arg0) || integer_pow2p (arg1))
+      && (optimize_insn_for_speed_p () || icode == CODE_FOR_nothing))
+    {
+      /* Optimize unsigned multiplication by power of 2 constant
+        using 2 shifts, one for result, one to extract the shifted
+        out bits to see if they are all zero.
+        Don't do this if optimizing for size and we have umulv4_optab,
+        in that case assume multiplication will be shorter.
+        This is heuristics based on the single target that provides
+        umulv4 right now (i?86/x86_64), if further targets add it, this
+        might need to be revisited.
+        Cases where both operands are constant should be folded already
+        during GIMPLE, and cases where one operand is constant but not
+        power of 2 are questionable, either the WIDEN_MULT_EXPR case
+        below can be done without multiplication, just by shifts and adds,
+        or we'd need to divide the result (and hope it actually doesn't
+        really divide nor multiply) and compare the result of the division
+        with the original operand.  */
+      rtx opn0 = op0;
+      rtx opn1 = op1;
+      tree argn0 = arg0;
+      tree argn1 = arg1;
+      if (integer_pow2p (arg0))
+       {
+         std::swap (opn0, opn1);
+         std::swap (argn0, argn1);
+       }
+      int cnt = tree_log2 (argn1);
+      if (cnt >= 0 && cnt < GET_MODE_PRECISION (mode))
+       {
+         rtx upper = const0_rtx;
+         res = expand_shift (LSHIFT_EXPR, mode, opn0, cnt, NULL_RTX, uns);
+         if (cnt != 0)
+           upper = expand_shift (RSHIFT_EXPR, mode, opn0,
+                                 GET_MODE_PRECISION (mode) - cnt,
+                                 NULL_RTX, uns);
+         do_compare_rtx_and_jump (upper, const0_rtx, EQ, true, mode,
+                                  NULL_RTX, NULL, done_label,
+                                  profile_probability::very_likely ());
+         goto do_error_label;
+       }
+    }
   if (icode != CODE_FOR_nothing)
     {
-      struct expand_operand ops[4];
+      class expand_operand ops[4];
       rtx_insn *last = get_last_insn ();
 
       res = gen_reg_rtx (mode);
@@ -1453,7 +1772,8 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
              && JUMP_P (last)
              && any_condjump_p (last)
              && !find_reg_note (last, REG_BR_PROB, 0))
-           add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY);
+           add_reg_br_prob_note (last, 
+                                 profile_probability::very_unlikely ());
          emit_jump (done_label);
         }
       else
@@ -1467,15 +1787,34 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
     {
       struct separate_ops ops;
       int prec = GET_MODE_PRECISION (mode);
-      machine_mode hmode = mode_for_size (prec / 2, MODE_INT, 1);
+      scalar_int_mode hmode, wmode;
       ops.op0 = make_tree (type, op0);
       ops.op1 = make_tree (type, op1);
       ops.op2 = NULL_TREE;
       ops.location = loc;
-      if (GET_MODE_2XWIDER_MODE (mode) != VOIDmode
-         && targetm.scalar_mode_supported_p (GET_MODE_2XWIDER_MODE (mode)))
+
+      /* Optimize unsigned overflow check where we don't use the
+        multiplication result, just whether overflow happened.
+        If we can do MULT_HIGHPART_EXPR, that followed by
+        comparison of the result against zero is cheapest.
+        We'll still compute res, but it should be DCEd later.  */
+      use_operand_p use;
+      gimple *use_stmt;
+      if (!is_ubsan
+         && lhs
+         && uns
+         && !(uns0_p && uns1_p && !unsr_p)
+         && can_mult_highpart_p (mode, uns) == 1
+         && single_imm_use (lhs, &use, &use_stmt)
+         && is_gimple_assign (use_stmt)
+         && gimple_assign_rhs_code (use_stmt) == IMAGPART_EXPR)
+       goto highpart;
+
+      if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
+         && targetm.scalar_mode_supported_p (wmode)
+         && can_widen_mult_without_libcall (wmode, mode, op0, op1, uns))
        {
-         machine_mode wmode = GET_MODE_2XWIDER_MODE (mode);
+       twoxwider:
          ops.code = WIDEN_MULT_EXPR;
          ops.type
            = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode), uns);
@@ -1483,14 +1822,45 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          res = expand_expr_real_2 (&ops, NULL_RTX, wmode, EXPAND_NORMAL);
          rtx hipart = expand_shift (RSHIFT_EXPR, wmode, res, prec,
                                     NULL_RTX, uns);
-         hipart = gen_lowpart (mode, hipart);
-         res = gen_lowpart (mode, res);
+         hipart = convert_modes (mode, wmode, hipart, uns);
+         res = convert_modes (mode, wmode, res, uns);
+         if (uns)
+           /* For the unsigned multiplication, there was overflow if
+              HIPART is non-zero.  */
+           do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
+                                    NULL_RTX, NULL, done_label,
+                                    profile_probability::very_likely ());
+         else
+           {
+             /* RES is used more than once, place it in a pseudo.  */
+             res = force_reg (mode, res);
+
+             rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
+                                         NULL_RTX, 0);
+             /* RES is low half of the double width result, HIPART
+                the high half.  There was overflow if
+                HIPART is different from RES < 0 ? -1 : 0.  */
+             do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
+                                      NULL_RTX, NULL, done_label,
+                                      profile_probability::very_likely ());
+           }
+       }
+      else if (can_mult_highpart_p (mode, uns) == 1)
+       {
+       highpart:
+         ops.code = MULT_HIGHPART_EXPR;
+         ops.type = type;
+
+         rtx hipart = expand_expr_real_2 (&ops, NULL_RTX, mode,
+                                          EXPAND_NORMAL);
+         ops.code = MULT_EXPR;
+         res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
          if (uns)
            /* For the unsigned multiplication, there was overflow if
               HIPART is non-zero.  */
            do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
                                     NULL_RTX, NULL, done_label,
-                                    PROB_VERY_LIKELY);
+                                    profile_probability::very_likely ());
          else
            {
              rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
@@ -1500,10 +1870,12 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
                 HIPART is different from RES < 0 ? -1 : 0.  */
              do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
                                       NULL_RTX, NULL, done_label,
-                                      PROB_VERY_LIKELY);
+                                      profile_probability::very_likely ());
            }
+         
        }
-      else if (hmode != BLKmode && 2 * GET_MODE_PRECISION (hmode) == prec)
+      else if (int_mode_for_size (prec / 2, 1).exists (&hmode)
+              && 2 * GET_MODE_PRECISION (hmode) == prec)
        {
          rtx_code_label *large_op0 = gen_label_rtx ();
          rtx_code_label *small_op0_large_op1 = gen_label_rtx ();
@@ -1517,16 +1889,16 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          unsigned int hprec = GET_MODE_PRECISION (hmode);
          rtx hipart0 = expand_shift (RSHIFT_EXPR, mode, op0, hprec,
                                      NULL_RTX, uns);
-         hipart0 = gen_lowpart (hmode, hipart0);
-         rtx lopart0 = gen_lowpart (hmode, op0);
+         hipart0 = convert_modes (hmode, mode, hipart0, uns);
+         rtx lopart0 = convert_modes (hmode, mode, op0, uns);
          rtx signbit0 = const0_rtx;
          if (!uns)
            signbit0 = expand_shift (RSHIFT_EXPR, hmode, lopart0, hprec - 1,
                                     NULL_RTX, 0);
          rtx hipart1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec,
                                      NULL_RTX, uns);
-         hipart1 = gen_lowpart (hmode, hipart1);
-         rtx lopart1 = gen_lowpart (hmode, op1);
+         hipart1 = convert_modes (hmode, mode, hipart1, uns);
+         rtx lopart1 = convert_modes (hmode, mode, op1, uns);
          rtx signbit1 = const0_rtx;
          if (!uns)
            signbit1 = expand_shift (RSHIFT_EXPR, hmode, lopart1, hprec - 1,
@@ -1593,32 +1965,19 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          if (!op0_small_p)
            do_compare_rtx_and_jump (signbit0, hipart0, NE, true, hmode,
                                     NULL_RTX, NULL, large_op0,
-                                    PROB_UNLIKELY);
+                                    profile_probability::unlikely ());
 
          if (!op1_small_p)
            do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
                                     NULL_RTX, NULL, small_op0_large_op1,
-                                    PROB_UNLIKELY);
+                                    profile_probability::unlikely ());
 
          /* If both op0 and op1 are sign (!uns) or zero (uns) extended from
             hmode to mode, the multiplication will never overflow.  We can
             do just one hmode x hmode => mode widening multiplication.  */
-         rtx lopart0s = lopart0, lopart1s = lopart1;
-         if (GET_CODE (lopart0) == SUBREG)
-           {
-             lopart0s = shallow_copy_rtx (lopart0);
-             SUBREG_PROMOTED_VAR_P (lopart0s) = 1;
-             SUBREG_PROMOTED_SET (lopart0s, uns ? SRP_UNSIGNED : SRP_SIGNED);
-           }
-         if (GET_CODE (lopart1) == SUBREG)
-           {
-             lopart1s = shallow_copy_rtx (lopart1);
-             SUBREG_PROMOTED_VAR_P (lopart1s) = 1;
-             SUBREG_PROMOTED_SET (lopart1s, uns ? SRP_UNSIGNED : SRP_SIGNED);
-           }
          tree halfstype = build_nonstandard_integer_type (hprec, uns);
-         ops.op0 = make_tree (halfstype, lopart0s);
-         ops.op1 = make_tree (halfstype, lopart1s);
+         ops.op0 = make_tree (halfstype, lopart0);
+         ops.op1 = make_tree (halfstype, lopart1);
          ops.code = WIDEN_MULT_EXPR;
          ops.type = type;
          rtx thisres
@@ -1644,7 +2003,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          if (!op1_small_p)
            do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
                                     NULL_RTX, NULL, both_ops_large,
-                                    PROB_UNLIKELY);
+                                    profile_probability::unlikely ());
 
          /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode,
             but op0 is not, prepare larger, hipart and lopart pseudos and
@@ -1681,12 +2040,12 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
              else if (larger_sign != -1)
                do_compare_rtx_and_jump (hipart, const0_rtx, GE, false, hmode,
                                         NULL_RTX, NULL, after_hipart_neg,
-                                        PROB_EVEN);
+                                        profile_probability::even ());
 
              tem = convert_modes (mode, hmode, lopart, 1);
              tem = expand_shift (LSHIFT_EXPR, mode, tem, hprec, NULL_RTX, 1);
              tem = expand_simple_binop (mode, MINUS, loxhi, tem, NULL_RTX,
-                                        1, OPTAB_DIRECT);
+                                        1, OPTAB_WIDEN);
              emit_move_insn (loxhi, tem);
 
              emit_label (after_hipart_neg);
@@ -1697,10 +2056,10 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
              else if (smaller_sign != -1)
                do_compare_rtx_and_jump (lopart, const0_rtx, GE, false, hmode,
                                         NULL_RTX, NULL, after_lopart_neg,
-                                        PROB_EVEN);
+                                        profile_probability::even ());
 
              tem = expand_simple_binop (mode, MINUS, loxhi, larger, NULL_RTX,
-                                        1, OPTAB_DIRECT);
+                                        1, OPTAB_WIDEN);
              emit_move_insn (loxhi, tem);
 
              emit_label (after_lopart_neg);
@@ -1709,7 +2068,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          /* loxhi += (uns) lo0xlo1 >> (bitsize / 2);  */
          tem = expand_shift (RSHIFT_EXPR, mode, lo0xlo1, hprec, NULL_RTX, 1);
          tem = expand_simple_binop (mode, PLUS, loxhi, tem, NULL_RTX,
-                                    1, OPTAB_DIRECT);
+                                    1, OPTAB_WIDEN);
          emit_move_insn (loxhi, tem);
 
          /* if (loxhi >> (bitsize / 2)
@@ -1717,24 +2076,26 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
             if (loxhi >> (bitsize / 2) == 0             (if uns).  */
          rtx hipartloxhi = expand_shift (RSHIFT_EXPR, mode, loxhi, hprec,
                                          NULL_RTX, 0);
-         hipartloxhi = gen_lowpart (hmode, hipartloxhi);
+         hipartloxhi = convert_modes (hmode, mode, hipartloxhi, 0);
          rtx signbitloxhi = const0_rtx;
          if (!uns)
            signbitloxhi = expand_shift (RSHIFT_EXPR, hmode,
-                                        gen_lowpart (hmode, loxhi),
+                                        convert_modes (hmode, mode,
+                                                       loxhi, 0),
                                         hprec - 1, NULL_RTX, 0);
 
          do_compare_rtx_and_jump (signbitloxhi, hipartloxhi, NE, true, hmode,
                                   NULL_RTX, NULL, do_overflow,
-                                  PROB_VERY_UNLIKELY);
+                                  profile_probability::very_unlikely ());
 
          /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1;  */
          rtx loxhishifted = expand_shift (LSHIFT_EXPR, mode, loxhi, hprec,
                                           NULL_RTX, 1);
-         tem = convert_modes (mode, hmode, gen_lowpart (hmode, lo0xlo1), 1);
+         tem = convert_modes (mode, hmode,
+                              convert_modes (hmode, mode, lo0xlo1, 1), 1);
 
          tem = expand_simple_binop (mode, IOR, loxhishifted, tem, res,
-                                    1, OPTAB_DIRECT);
+                                    1, OPTAB_WIDEN);
          if (tem != res)
            emit_move_insn (res, tem);
          emit_jump (done_label);
@@ -1759,41 +2120,41 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
              if (!op0_medium_p)
                {
                  tem = expand_simple_binop (hmode, PLUS, hipart0, const1_rtx,
-                                            NULL_RTX, 1, OPTAB_DIRECT);
+                                            NULL_RTX, 1, OPTAB_WIDEN);
                  do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
                                           NULL_RTX, NULL, do_error,
-                                          PROB_VERY_UNLIKELY);
+                                          profile_probability::very_unlikely ());
                }
 
              if (!op1_medium_p)
                {
                  tem = expand_simple_binop (hmode, PLUS, hipart1, const1_rtx,
-                                            NULL_RTX, 1, OPTAB_DIRECT);
+                                            NULL_RTX, 1, OPTAB_WIDEN);
                  do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
                                           NULL_RTX, NULL, do_error,
-                                          PROB_VERY_UNLIKELY);
+                                          profile_probability::very_unlikely ());
                }
 
              /* At this point hipart{0,1} are both in [-1, 0].  If they are
-                the same, overflow happened if res is negative, if they are
-                different, overflow happened if res is positive.  */
+                the same, overflow happened if res is non-positive, if they
+                are different, overflow happened if res is positive.  */
              if (op0_sign != 1 && op1_sign != 1 && op0_sign != op1_sign)
                emit_jump (hipart_different);
              else if (op0_sign == 1 || op1_sign == 1)
                do_compare_rtx_and_jump (hipart0, hipart1, NE, true, hmode,
                                         NULL_RTX, NULL, hipart_different,
-                                        PROB_EVEN);
+                                        profile_probability::even ());
 
-             do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode,
+             do_compare_rtx_and_jump (res, const0_rtx, LE, false, mode,
                                       NULL_RTX, NULL, do_error,
-                                      PROB_VERY_UNLIKELY);
+                                      profile_probability::very_unlikely ());
              emit_jump (done_label);
 
              emit_label (hipart_different);
 
              do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode,
                                       NULL_RTX, NULL, do_error,
-                                      PROB_VERY_UNLIKELY);
+                                      profile_probability::very_unlikely ());
              emit_jump (done_label);
            }
 
@@ -1805,6 +2166,11 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
          tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
          emit_move_insn (res, tem);
        }
+      else if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
+              && targetm.scalar_mode_supported_p (wmode))
+       /* Even emitting a libcall is better than not detecting overflow
+          at all.  */
+       goto twoxwider;
       else
        {
          gcc_assert (!is_ubsan);
@@ -1838,7 +2204,7 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
     {
       rtx_code_label *all_done_label = gen_label_rtx ();
       do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
-                              NULL, all_done_label, PROB_VERY_LIKELY);
+                              NULL, all_done_label, profile_probability::very_likely ());
       expand_arith_set_overflow (lhs, target);
       emit_label (all_done_label);
     }
@@ -1849,14 +2215,14 @@ expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
       rtx_code_label *all_done_label = gen_label_rtx ();
       rtx_code_label *set_noovf = gen_label_rtx ();
       do_compare_rtx_and_jump (op1, const0_rtx, GE, false, mode, NULL_RTX,
-                              NULL, all_done_label, PROB_VERY_LIKELY);
+                              NULL, all_done_label, profile_probability::very_likely ());
       expand_arith_set_overflow (lhs, target);
       do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
-                              NULL, set_noovf, PROB_VERY_LIKELY);
+                              NULL, set_noovf, profile_probability::very_likely ());
       do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
-                              NULL, all_done_label, PROB_VERY_UNLIKELY);
+                              NULL, all_done_label, profile_probability::very_unlikely ());
       do_compare_rtx_and_jump (op1, res, NE, true, mode, NULL_RTX, NULL,
-                              all_done_label, PROB_VERY_UNLIKELY);
+                              all_done_label, profile_probability::very_unlikely ());
       emit_label (set_noovf);
       write_complex_part (target, const0_rtx, true);
       emit_label (all_done_label);
@@ -1877,7 +2243,7 @@ static void
 expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
                              tree arg0, tree arg1)
 {
-  int cnt = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
+  poly_uint64 cnt = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
   rtx_code_label *loop_lab = NULL;
   rtx cntvar = NULL_RTX;
   tree cntv = NULL_TREE;
@@ -1887,12 +2253,14 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
   tree resv = NULL_TREE;
   rtx lhsr = NULL_RTX;
   rtx resvr = NULL_RTX;
+  unsigned HOST_WIDE_INT const_cnt = 0;
+  bool use_loop_p = (!cnt.is_constant (&const_cnt) || const_cnt > 4);
 
   if (lhs)
     {
       optab op;
       lhsr = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
-      if (GET_MODE (lhsr) == BLKmode
+      if (!VECTOR_MODE_P (GET_MODE (lhsr))
          || (op = optab_for_tree_code (code, TREE_TYPE (arg0),
                                        optab_default)) == unknown_optab
          || (optab_handler (op, TYPE_MODE (TREE_TYPE (arg0)))
@@ -1907,7 +2275,7 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
            }
        }
     }
-  if (cnt > 4)
+  if (use_loop_p)
     {
       do_pending_stack_adjust ();
       loop_lab = gen_label_rtx ();
@@ -1926,18 +2294,26 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
       rtx arg1r = expand_normal (arg1);
       arg1 = make_tree (TREE_TYPE (arg1), arg1r);
     }
-  for (int i = 0; i < (cnt > 4 ? 1 : cnt); i++)
+  for (unsigned int i = 0; i < (use_loop_p ? 1 : const_cnt); i++)
     {
       tree op0, op1, res = NULL_TREE;
-      if (cnt > 4)
+      if (use_loop_p)
        {
          tree atype = build_array_type_nelts (eltype, cnt);
-         op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg0);
-         op0 = build4_loc (loc, ARRAY_REF, eltype, op0, cntv,
-                           NULL_TREE, NULL_TREE);
-         op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg1);
-         op1 = build4_loc (loc, ARRAY_REF, eltype, op1, cntv,
-                           NULL_TREE, NULL_TREE);
+         op0 = uniform_vector_p (arg0);
+         if (op0 == NULL_TREE)
+           {
+             op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg0);
+             op0 = build4_loc (loc, ARRAY_REF, eltype, op0, cntv,
+                               NULL_TREE, NULL_TREE);
+           }
+         op1 = uniform_vector_p (arg1);
+         if (op1 == NULL_TREE)
+           {
+             op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg1);
+             op1 = build4_loc (loc, ARRAY_REF, eltype, op1, cntv,
+                               NULL_TREE, NULL_TREE);
+           }
          if (resv)
            {
              res = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, resv);
@@ -1961,7 +2337,7 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
                                  false, false, false, true, &data);
          break;
        case MINUS_EXPR:
-         if (cnt > 4 ? integer_zerop (arg0) : integer_zerop (op0))
+         if (use_loop_p ? integer_zerop (arg0) : integer_zerop (op0))
            expand_neg_overflow (loc, res, op1, true, &data);
          else
            expand_addsub_overflow (loc, MINUS_EXPR, res, op0, op1,
@@ -1975,7 +2351,7 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
          gcc_unreachable ();
        }
     }
-  if (cnt > 4)
+  if (use_loop_p)
     {
       struct separate_ops ops;
       ops.code = PLUS_EXPR;
@@ -1988,9 +2364,10 @@ expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
                                    EXPAND_NORMAL);
       if (ret != cntvar)
        emit_move_insn (cntvar, ret);
-      do_compare_rtx_and_jump (cntvar, GEN_INT (cnt), NE, false,
+      rtx cntrtx = gen_int_mode (cnt, TYPE_MODE (sizetype));
+      do_compare_rtx_and_jump (cntvar, cntrtx, NE, false,
                               TYPE_MODE (sizetype), NULL_RTX, NULL, loop_lab,
-                              PROB_VERY_LIKELY);
+                              profile_probability::very_likely ());
     }
   if (lhs && resv == NULL_TREE)
     {
@@ -2117,7 +2494,7 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
          /* The infinity precision result will always fit into result.  */
          rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
          write_complex_part (target, const0_rtx, true);
-         enum machine_mode mode = TYPE_MODE (type);
+         scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
          struct separate_ops ops;
          ops.code = code;
          ops.type = type;
@@ -2170,7 +2547,7 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
       if (orig_precres == precres && precop <= BITS_PER_WORD)
        {
          int p = MAX (min_precision, precop);
-         enum machine_mode m = smallest_mode_for_size (p, MODE_INT);
+         scalar_int_mode m = smallest_int_mode_for_size (p);
          tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
                                                        uns0_p && uns1_p
                                                        && unsr_p);
@@ -2212,7 +2589,7 @@ expand_arith_overflow (enum tree_code code, gimple *stmt)
       if (orig_precres == precres)
        {
          int p = MAX (prec0, prec1);
-         enum machine_mode m = smallest_mode_for_size (p, MODE_INT);
+         scalar_int_mode m = smallest_int_mode_for_size (p);
          tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
                                                        uns0_p && uns1_p
                                                        && unsr_p);
@@ -2263,26 +2640,81 @@ expand_LOOP_VECTORIZED (internal_fn, gcall *)
   gcc_unreachable ();
 }
 
-/* Expand MASK_LOAD call STMT using optab OPTAB.  */
+/* This should get folded in tree-vectorizer.c.  */
+
+static void
+expand_LOOP_DIST_ALIAS (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+/* Return a memory reference of type TYPE for argument INDEX of STMT.
+   Use argument INDEX + 1 to derive the second (TBAA) operand.  */
+
+static tree
+expand_call_mem_ref (tree type, gcall *stmt, int index)
+{
+  tree addr = gimple_call_arg (stmt, index);
+  tree alias_ptr_type = TREE_TYPE (gimple_call_arg (stmt, index + 1));
+  unsigned int align = tree_to_shwi (gimple_call_arg (stmt, index + 1));
+  if (TYPE_ALIGN (type) != align)
+    type = build_aligned_type (type, align);
+
+  tree tmp = addr;
+  if (TREE_CODE (tmp) == SSA_NAME)
+    {
+      gimple *def = SSA_NAME_DEF_STMT (tmp);
+      if (gimple_assign_single_p (def))
+       tmp = gimple_assign_rhs1 (def);
+    }
+
+  if (TREE_CODE (tmp) == ADDR_EXPR)
+    {
+      tree mem = TREE_OPERAND (tmp, 0);
+      if (TREE_CODE (mem) == TARGET_MEM_REF
+         && types_compatible_p (TREE_TYPE (mem), type))
+       {
+         tree offset = TMR_OFFSET (mem);
+         if (type != TREE_TYPE (mem)
+             || alias_ptr_type != TREE_TYPE (offset)
+             || !integer_zerop (offset))
+           {
+             mem = copy_node (mem);
+             TMR_OFFSET (mem) = wide_int_to_tree (alias_ptr_type,
+                                                  wi::to_poly_wide (offset));
+             TREE_TYPE (mem) = type;
+           }
+         return mem;
+       }
+    }
+
+  return fold_build2 (MEM_REF, type, addr, build_int_cst (alias_ptr_type, 0));
+}
+
+/* Expand MASK_LOAD{,_LANES} or LEN_LOAD call STMT using optab OPTAB.  */
 
 static void
-expand_mask_load_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
+expand_partial_load_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
-  struct expand_operand ops[3];
-  tree type, lhs, rhs, maskt, ptr;
+  class expand_operand ops[3];
+  tree type, lhs, rhs, maskt;
   rtx mem, target, mask;
-  unsigned align;
+  insn_code icode;
 
   maskt = gimple_call_arg (stmt, 2);
   lhs = gimple_call_lhs (stmt);
   if (lhs == NULL_TREE)
     return;
   type = TREE_TYPE (lhs);
-  ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
-  align = tree_to_shwi (gimple_call_arg (stmt, 1));
-  if (TYPE_ALIGN (type) != align)
-    type = build_aligned_type (type, align);
-  rhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0), ptr);
+  rhs = expand_call_mem_ref (type, stmt, 0);
+
+  if (optab == vec_mask_load_lanes_optab)
+    icode = get_multi_vector_move (type, optab);
+  else if (optab == len_load_optab)
+    icode = direct_optab_handler (optab, TYPE_MODE (type));
+  else
+    icode = convert_optab_handler (optab, TYPE_MODE (type),
+                                  TYPE_MODE (TREE_TYPE (maskt)));
 
   mem = expand_expr (rhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   gcc_assert (MEM_P (mem));
@@ -2290,30 +2722,42 @@ expand_mask_load_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
   target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   create_output_operand (&ops[0], target, TYPE_MODE (type));
   create_fixed_operand (&ops[1], mem);
-  create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
-  expand_insn (convert_optab_handler (optab, TYPE_MODE (type),
-                                     TYPE_MODE (TREE_TYPE (maskt))),
-              3, ops);
+  if (optab == len_load_optab)
+    create_convert_operand_from (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)),
+                                TYPE_UNSIGNED (TREE_TYPE (maskt)));
+  else
+    create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
+  expand_insn (icode, 3, ops);
+  if (!rtx_equal_p (target, ops[0].value))
+    emit_move_insn (target, ops[0].value);
 }
 
-/* Expand MASK_STORE call STMT using optab OPTAB.  */
+#define expand_mask_load_optab_fn expand_partial_load_optab_fn
+#define expand_mask_load_lanes_optab_fn expand_mask_load_optab_fn
+#define expand_len_load_optab_fn expand_partial_load_optab_fn
+
+/* Expand MASK_STORE{,_LANES} or LEN_STORE call STMT using optab OPTAB.  */
 
 static void
-expand_mask_store_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
+expand_partial_store_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
-  struct expand_operand ops[3];
-  tree type, lhs, rhs, maskt, ptr;
+  class expand_operand ops[3];
+  tree type, lhs, rhs, maskt;
   rtx mem, reg, mask;
-  unsigned align;
+  insn_code icode;
 
   maskt = gimple_call_arg (stmt, 2);
   rhs = gimple_call_arg (stmt, 3);
   type = TREE_TYPE (rhs);
-  ptr = build_int_cst (TREE_TYPE (gimple_call_arg (stmt, 1)), 0);
-  align = tree_to_shwi (gimple_call_arg (stmt, 1));
-  if (TYPE_ALIGN (type) != align)
-    type = build_aligned_type (type, align);
-  lhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0), ptr);
+  lhs = expand_call_mem_ref (type, stmt, 0);
+
+  if (optab == vec_mask_store_lanes_optab)
+    icode = get_multi_vector_move (type, optab);
+  else if (optab == len_store_optab)
+    icode = direct_optab_handler (optab, TYPE_MODE (type));
+  else
+    icode = convert_optab_handler (optab, TYPE_MODE (type),
+                                  TYPE_MODE (TREE_TYPE (maskt)));
 
   mem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   gcc_assert (MEM_P (mem));
@@ -2321,24 +2765,147 @@ expand_mask_store_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
   reg = expand_normal (rhs);
   create_fixed_operand (&ops[0], mem);
   create_input_operand (&ops[1], reg, TYPE_MODE (type));
-  create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
-  expand_insn (convert_optab_handler (optab, TYPE_MODE (type),
-                                     TYPE_MODE (TREE_TYPE (maskt))),
-              3, ops);
+  if (optab == len_store_optab)
+    create_convert_operand_from (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)),
+                                TYPE_UNSIGNED (TREE_TYPE (maskt)));
+  else
+    create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt)));
+  expand_insn (icode, 3, ops);
 }
 
+#define expand_mask_store_optab_fn expand_partial_store_optab_fn
+#define expand_mask_store_lanes_optab_fn expand_mask_store_optab_fn
+#define expand_len_store_optab_fn expand_partial_store_optab_fn
+
+/* Expand VCOND, VCONDU and VCONDEQ optab internal functions.
+   The expansion of STMT happens based on OPTAB table associated.  */
+
 static void
-expand_ABNORMAL_DISPATCHER (internal_fn, gcall *)
+expand_vec_cond_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
+  class expand_operand ops[6];
+  insn_code icode;
+  tree lhs = gimple_call_lhs (stmt);
+  tree op0a = gimple_call_arg (stmt, 0);
+  tree op0b = gimple_call_arg (stmt, 1);
+  tree op1 = gimple_call_arg (stmt, 2);
+  tree op2 = gimple_call_arg (stmt, 3);
+  enum tree_code tcode = (tree_code) int_cst_value (gimple_call_arg (stmt, 4));
+
+  tree vec_cond_type = TREE_TYPE (lhs);
+  tree op_mode = TREE_TYPE (op0a);
+  bool unsignedp = TYPE_UNSIGNED (op_mode);
+
+  machine_mode mode = TYPE_MODE (vec_cond_type);
+  machine_mode cmp_op_mode = TYPE_MODE (op_mode);
+
+  icode = convert_optab_handler (optab, mode, cmp_op_mode);
+  rtx comparison
+    = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp, icode, 4);
+  rtx rtx_op1 = expand_normal (op1);
+  rtx rtx_op2 = expand_normal (op2);
+
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  create_output_operand (&ops[0], target, mode);
+  create_input_operand (&ops[1], rtx_op1, mode);
+  create_input_operand (&ops[2], rtx_op2, mode);
+  create_fixed_operand (&ops[3], comparison);
+  create_fixed_operand (&ops[4], XEXP (comparison, 0));
+  create_fixed_operand (&ops[5], XEXP (comparison, 1));
+  expand_insn (icode, 6, ops);
+  if (!rtx_equal_p (ops[0].value, target))
+    emit_move_insn (target, ops[0].value);
 }
 
+/* Expand VCOND_MASK optab internal function.
+   The expansion of STMT happens based on OPTAB table associated.  */
+
 static void
-expand_BUILTIN_EXPECT (internal_fn, gcall *stmt)
+expand_vec_cond_mask_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
 {
-  /* When guessing was done, the hints should be already stripped away.  */
-  gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ());
+  class expand_operand ops[4];
 
-  rtx target;
+  tree lhs = gimple_call_lhs (stmt);
+  tree op0 = gimple_call_arg (stmt, 0);
+  tree op1 = gimple_call_arg (stmt, 1);
+  tree op2 = gimple_call_arg (stmt, 2);
+  tree vec_cond_type = TREE_TYPE (lhs);
+
+  machine_mode mode = TYPE_MODE (vec_cond_type);
+  machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
+  enum insn_code icode = convert_optab_handler (optab, mode, mask_mode);
+  rtx mask, rtx_op1, rtx_op2;
+
+  gcc_assert (icode != CODE_FOR_nothing);
+
+  mask = expand_normal (op0);
+  rtx_op1 = expand_normal (op1);
+  rtx_op2 = expand_normal (op2);
+
+  mask = force_reg (mask_mode, mask);
+  rtx_op1 = force_reg (mode, rtx_op1);
+
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  create_output_operand (&ops[0], target, mode);
+  create_input_operand (&ops[1], rtx_op1, mode);
+  create_input_operand (&ops[2], rtx_op2, mode);
+  create_input_operand (&ops[3], mask, mask_mode);
+  expand_insn (icode, 4, ops);
+  if (!rtx_equal_p (ops[0].value, target))
+    emit_move_insn (target, ops[0].value);
+}
+
+/* Expand VEC_SET internal functions.  */
+
+static void
+expand_vec_set_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  tree op0 = gimple_call_arg (stmt, 0);
+  tree op1 = gimple_call_arg (stmt, 1);
+  tree op2 = gimple_call_arg (stmt, 2);
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  rtx src = expand_normal (op0);
+
+  machine_mode outermode = TYPE_MODE (TREE_TYPE (op0));
+  scalar_mode innermode = GET_MODE_INNER (outermode);
+
+  rtx value = expand_normal (op1);
+  rtx pos = expand_normal (op2);
+
+  class expand_operand ops[3];
+  enum insn_code icode = optab_handler (optab, outermode);
+
+  if (icode != CODE_FOR_nothing)
+    {
+      rtx temp = gen_reg_rtx (outermode);
+      emit_move_insn (temp, src);
+
+      create_fixed_operand (&ops[0], temp);
+      create_input_operand (&ops[1], value, innermode);
+      create_convert_operand_from (&ops[2], pos, TYPE_MODE (TREE_TYPE (op2)),
+                                  true);
+      if (maybe_expand_insn (icode, 3, ops))
+       {
+         emit_move_insn (target, temp);
+         return;
+       }
+    }
+  gcc_unreachable ();
+}
+
+static void
+expand_ABNORMAL_DISPATCHER (internal_fn, gcall *)
+{
+}
+
+static void
+expand_BUILTIN_EXPECT (internal_fn, gcall *stmt)
+{
+  /* When guessing was done, the hints should be already stripped away.  */
+  gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ());
+
+  rtx target;
   tree lhs = gimple_call_lhs (stmt);
   if (lhs)
     target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
@@ -2358,6 +2925,15 @@ expand_VA_ARG (internal_fn, gcall *)
   gcc_unreachable ();
 }
 
+/* IFN_VEC_CONVERT is supposed to be expanded at pass_lower_vector.  So this
+   dummy function should never be called.  */
+
+static void
+expand_VEC_CONVERT (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
 /* Expand the IFN_UNIQUE function according to its first argument.  */
 
 static void
@@ -2404,6 +2980,89 @@ expand_UNIQUE (internal_fn, gcall *stmt)
     emit_insn (pattern);
 }
 
+/* Expand the IFN_DEFERRED_INIT function:
+   LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, IS_VLA);
+
+   if IS_VLA is false, the LHS is the DECL itself,
+   if IS_VLA is true, the LHS is a MEM_REF whose address is the pointer
+   to this DECL.
+
+   Initialize the LHS with zero/pattern according to its second argument
+   INIT_TYPE:
+   if INIT_TYPE is AUTO_INIT_ZERO, use zeroes to initialize;
+   if INIT_TYPE is AUTO_INIT_PATTERN, use 0xFE byte-repeatable pattern
+     to initialize;
+   The LHS variable is initialized including paddings.
+   The reasons to choose 0xFE for pattern initialization are:
+     1. It is a non-canonical virtual address on x86_64, and at the
+       high end of the i386 kernel address space.
+     2. It is a very large float value (-1.694739530317379e+38).
+     3. It is also an unusual number for integers.  */
+#define INIT_PATTERN_VALUE  0xFE
+static void
+expand_DEFERRED_INIT (internal_fn, gcall *stmt)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  tree var_size = gimple_call_arg (stmt, 0);
+  enum auto_init_type init_type
+    = (enum auto_init_type) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
+  bool reg_lhs = true;
+
+  tree var_type = TREE_TYPE (lhs);
+  gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
+
+  if (TREE_CODE (lhs) == SSA_NAME)
+    reg_lhs = true;
+  else
+    {
+      rtx tem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+      reg_lhs = !MEM_P (tem);
+    }
+
+  if (!reg_lhs)
+    {
+      /* If this is a VLA or the variable is not in register,
+        expand to a memset to initialize it.  */
+      mark_addressable (lhs);
+      tree var_addr = build_fold_addr_expr (lhs);
+
+      tree value = (init_type == AUTO_INIT_PATTERN) ?
+                   build_int_cst (integer_type_node,
+                                  INIT_PATTERN_VALUE) :
+                   integer_zero_node;
+      tree m_call = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMSET),
+                                    3, var_addr, value, var_size);
+      /* Expand this memset call.  */
+      expand_builtin_memset (m_call, NULL_RTX, TYPE_MODE (var_type));
+    }
+  else
+    {
+      /* If this variable is in a register, use expand_assignment might
+        generate better code.  */
+      tree init = build_zero_cst (var_type);
+      unsigned HOST_WIDE_INT total_bytes
+       = tree_to_uhwi (TYPE_SIZE_UNIT (var_type));
+
+      if (init_type == AUTO_INIT_PATTERN)
+       {
+         unsigned char *buf = (unsigned char *) xmalloc (total_bytes);
+         memset (buf, INIT_PATTERN_VALUE, total_bytes);
+         if (can_native_interpret_type_p (var_type))
+           init = native_interpret_expr (var_type, buf, total_bytes);
+         else
+           {
+             tree itype = build_nonstandard_integer_type
+                            (total_bytes * BITS_PER_UNIT, 1);
+             wide_int w = wi::from_buffer (buf, total_bytes);
+             init = build1 (VIEW_CONVERT_EXPR, var_type,
+                            wide_int_to_tree (itype, w));
+           }
+       }
+
+      expand_assignment (lhs, init, false);
+    }
+}
+
 /* The size of an OpenACC compute dimension.  */
 
 static void
@@ -2462,6 +3121,14 @@ expand_GOACC_REDUCTION (internal_fn, gcall *)
   gcc_unreachable ();
 }
 
+/* This is expanded by oacc_device_lower pass.  */
+
+static void
+expand_GOACC_TILE (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
 /* Set errno to EDOM.  */
 
 static void
@@ -2525,6 +3192,104 @@ expand_LAUNDER (internal_fn, gcall *call)
   expand_assignment (lhs, gimple_call_arg (call, 0), false);
 }
 
+/* Expand {MASK_,}SCATTER_STORE{S,U} call CALL using optab OPTAB.  */
+
+static void
+expand_scatter_store_optab_fn (internal_fn, gcall *stmt, direct_optab optab)
+{
+  internal_fn ifn = gimple_call_internal_fn (stmt);
+  int rhs_index = internal_fn_stored_value_index (ifn);
+  int mask_index = internal_fn_mask_index (ifn);
+  tree base = gimple_call_arg (stmt, 0);
+  tree offset = gimple_call_arg (stmt, 1);
+  tree scale = gimple_call_arg (stmt, 2);
+  tree rhs = gimple_call_arg (stmt, rhs_index);
+
+  rtx base_rtx = expand_normal (base);
+  rtx offset_rtx = expand_normal (offset);
+  HOST_WIDE_INT scale_int = tree_to_shwi (scale);
+  rtx rhs_rtx = expand_normal (rhs);
+
+  class expand_operand ops[6];
+  int i = 0;
+  create_address_operand (&ops[i++], base_rtx);
+  create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
+  create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
+  create_integer_operand (&ops[i++], scale_int);
+  create_input_operand (&ops[i++], rhs_rtx, TYPE_MODE (TREE_TYPE (rhs)));
+  if (mask_index >= 0)
+    {
+      tree mask = gimple_call_arg (stmt, mask_index);
+      rtx mask_rtx = expand_normal (mask);
+      create_input_operand (&ops[i++], mask_rtx, TYPE_MODE (TREE_TYPE (mask)));
+    }
+
+  insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (rhs)),
+                                          TYPE_MODE (TREE_TYPE (offset)));
+  expand_insn (icode, i, ops);
+}
+
+/* Expand {MASK_,}GATHER_LOAD call CALL using optab OPTAB.  */
+
+static void
+expand_gather_load_optab_fn (internal_fn, gcall *stmt, direct_optab optab)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  tree base = gimple_call_arg (stmt, 0);
+  tree offset = gimple_call_arg (stmt, 1);
+  tree scale = gimple_call_arg (stmt, 2);
+
+  rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  rtx base_rtx = expand_normal (base);
+  rtx offset_rtx = expand_normal (offset);
+  HOST_WIDE_INT scale_int = tree_to_shwi (scale);
+
+  int i = 0;
+  class expand_operand ops[6];
+  create_output_operand (&ops[i++], lhs_rtx, TYPE_MODE (TREE_TYPE (lhs)));
+  create_address_operand (&ops[i++], base_rtx);
+  create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
+  create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
+  create_integer_operand (&ops[i++], scale_int);
+  if (optab == mask_gather_load_optab)
+    {
+      tree mask = gimple_call_arg (stmt, 4);
+      rtx mask_rtx = expand_normal (mask);
+      create_input_operand (&ops[i++], mask_rtx, TYPE_MODE (TREE_TYPE (mask)));
+    }
+  insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)),
+                                          TYPE_MODE (TREE_TYPE (offset)));
+  expand_insn (icode, i, ops);
+  if (!rtx_equal_p (lhs_rtx, ops[0].value))
+    emit_move_insn (lhs_rtx, ops[0].value);
+}
+
+/* Helper for expand_DIVMOD.  Return true if the sequence starting with
+   INSN contains any call insns or insns with {,U}{DIV,MOD} rtxes.  */
+
+static bool
+contains_call_div_mod (rtx_insn *insn)
+{
+  subrtx_iterator::array_type array;
+  for (; insn; insn = NEXT_INSN (insn))
+    if (CALL_P (insn))
+      return true;
+    else if (INSN_P (insn))
+      FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
+       switch (GET_CODE (*iter))
+         {
+         case CALL:
+         case DIV:
+         case UDIV:
+         case MOD:
+         case UMOD:
+           return true;
+         default:
+           break;
+         }
+  return false;
+ }
+
 /* Expand DIVMOD() using:
  a) optab handler for udivmod/sdivmod if it is available.
  b) If optab_handler doesn't exist, generate call to
@@ -2547,10 +3312,85 @@ expand_DIVMOD (internal_fn, gcall *call_stmt)
   rtx op1 = expand_normal (arg1);
   rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
 
-  rtx quotient, remainder, libfunc;
+  rtx quotient = NULL_RTX, remainder = NULL_RTX;
+  rtx_insn *insns = NULL;
+
+  if (TREE_CODE (arg1) == INTEGER_CST)
+    {
+      /* For DIVMOD by integral constants, there could be efficient code
+        expanded inline e.g. using shifts and plus/minus.  Try to expand
+        the division and modulo and if it emits any library calls or any
+        {,U}{DIV,MOD} rtxes throw it away and use a divmod optab or
+        divmod libcall.  */
+      scalar_int_mode int_mode;
+      if (remainder == NULL_RTX
+         && optimize
+         && CONST_INT_P (op1)
+         && !pow2p_hwi (INTVAL (op1))
+         && is_int_mode (TYPE_MODE (type), &int_mode)
+         && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
+         && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
+         && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
+         && optimize_insn_for_speed_p ())
+       {
+         rtx_insn *last = get_last_insn ();
+         remainder = NULL_RTX;
+         quotient = expand_doubleword_divmod (int_mode, op0, op1, &remainder,
+                                              TYPE_UNSIGNED (type));
+         if (quotient != NULL_RTX)
+           {
+             if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
+               {
+                 rtx_insn *move = emit_move_insn (quotient, quotient);
+                 set_dst_reg_note (move, REG_EQUAL,
+                                   gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
+                                                   ? UDIV : DIV, int_mode,
+                                                   copy_rtx (op0), op1),
+                                   quotient);
+                 move = emit_move_insn (remainder, remainder);
+                 set_dst_reg_note (move, REG_EQUAL,
+                                   gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
+                                                   ? UMOD : MOD, int_mode,
+                                                   copy_rtx (op0), op1),
+                                   quotient);
+               }
+           }
+         else
+           delete_insns_since (last);
+       }
+
+      if (remainder == NULL_RTX)
+       {
+         struct separate_ops ops;
+         ops.code = TRUNC_DIV_EXPR;
+         ops.type = type;
+         ops.op0 = make_tree (ops.type, op0);
+         ops.op1 = arg1;
+         ops.op2 = NULL_TREE;
+         ops.location = gimple_location (call_stmt);
+         start_sequence ();
+         quotient = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
+         if (contains_call_div_mod (get_insns ()))
+           quotient = NULL_RTX;
+         else
+           {
+             ops.code = TRUNC_MOD_EXPR;
+             remainder = expand_expr_real_2 (&ops, NULL_RTX, mode,
+                                             EXPAND_NORMAL);
+             if (contains_call_div_mod (get_insns ()))
+               remainder = NULL_RTX;
+           }
+         if (remainder)
+           insns = get_insns ();
+         end_sequence ();
+       }
+    }
+
+  if (remainder)
+    emit_insn (insns);
 
   /* Check if optab_handler exists for divmod_optab for given mode.  */
-  if (optab_handler (tab, mode) != CODE_FOR_nothing)
+  else if (optab_handler (tab, mode) != CODE_FOR_nothing)
     {
       quotient = gen_reg_rtx (mode);
       remainder = gen_reg_rtx (mode);
@@ -2558,7 +3398,7 @@ expand_DIVMOD (internal_fn, gcall *call_stmt)
     }
 
   /* Generate call to divmod libfunc if it exists.  */
-  else if ((libfunc = optab_libfunc (tab, mode)) != NULL_RTX)
+  else if (rtx libfunc = optab_libfunc (tab, mode))
     targetm.expand_divmod_libfunc (libfunc, mode, op0, op1,
                                   &quotient, &remainder);
 
@@ -2569,7 +3409,41 @@ expand_DIVMOD (internal_fn, gcall *call_stmt)
   expand_expr (build2 (COMPLEX_EXPR, TREE_TYPE (lhs),
                       make_tree (TREE_TYPE (arg0), quotient),
                       make_tree (TREE_TYPE (arg1), remainder)),
-             target, VOIDmode, EXPAND_NORMAL);
+              target, VOIDmode, EXPAND_NORMAL);
+}
+
+/* Expand a NOP.  */
+
+static void
+expand_NOP (internal_fn, gcall *)
+{
+  /* Nothing.  But it shouldn't really prevail.  */
+}
+
+/* Coroutines, all should have been processed at this stage.  */
+
+static void
+expand_CO_FRAME (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_YIELD (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_SUSPN (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_ACTOR (internal_fn, gcall *)
+{
+  gcc_unreachable ();
 }
 
 /* Expand a call to FN using the operands in STMT.  FN has a single
@@ -2583,11 +3457,21 @@ expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
 
   tree_pair types = direct_internal_fn_types (fn, stmt);
   insn_code icode = direct_optab_handler (optab, TYPE_MODE (types.first));
+  gcc_assert (icode != CODE_FOR_nothing);
 
   tree lhs = gimple_call_lhs (stmt);
-  tree lhs_type = TREE_TYPE (lhs);
-  rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
-  create_output_operand (&ops[0], lhs_rtx, insn_data[icode].operand[0].mode);
+  rtx lhs_rtx = NULL_RTX;
+  if (lhs)
+    lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+
+  /* Do not assign directly to a promoted subreg, since there is no
+     guarantee that the instruction will leave the upper bits of the
+     register in the state required by SUBREG_PROMOTED_SIGN.  */
+  rtx dest = lhs_rtx;
+  if (dest && GET_CODE (dest) == SUBREG && SUBREG_PROMOTED_VAR_P (dest))
+    dest = NULL_RTX;
+
+  create_output_operand (&ops[0], dest, insn_data[icode].operand[0].mode);
 
   for (unsigned int i = 0; i < nargs; ++i)
     {
@@ -2603,7 +3487,7 @@ expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
     }
 
   expand_insn (icode, nargs + 1, ops);
-  if (!rtx_equal_p (lhs_rtx, ops[0].value))
+  if (lhs_rtx && !rtx_equal_p (lhs_rtx, ops[0].value))
     {
       /* If the return value has an integral type, convert the instruction
         result to that type.  This is useful for things that return an
@@ -2617,7 +3501,7 @@ expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
          /* If this is a scalar in a register that is stored in a wider
             mode than the declared mode, compute the result into its
             declared mode and then convert to the wider mode.  */
-         gcc_checking_assert (INTEGRAL_TYPE_P (lhs_type));
+         gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
          rtx tmp = convert_to_mode (GET_MODE (lhs_rtx), ops[0].value, 0);
          convert_move (SUBREG_REG (lhs_rtx), tmp,
                        SUBREG_PROMOTED_SIGN (lhs_rtx));
@@ -2626,12 +3510,41 @@ expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
        emit_move_insn (lhs_rtx, ops[0].value);
       else
        {
-         gcc_checking_assert (INTEGRAL_TYPE_P (lhs_type));
+         gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
          convert_move (lhs_rtx, ops[0].value, 0);
        }
     }
 }
 
+/* Expand WHILE_ULT call STMT using optab OPTAB.  */
+
+static void
+expand_while_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
+{
+  expand_operand ops[3];
+  tree rhs_type[2];
+
+  tree lhs = gimple_call_lhs (stmt);
+  tree lhs_type = TREE_TYPE (lhs);
+  rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  create_output_operand (&ops[0], lhs_rtx, TYPE_MODE (lhs_type));
+
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      tree rhs = gimple_call_arg (stmt, i);
+      rhs_type[i] = TREE_TYPE (rhs);
+      rtx rhs_rtx = expand_normal (rhs);
+      create_input_operand (&ops[i + 1], rhs_rtx, TYPE_MODE (rhs_type[i]));
+    }
+
+  insn_code icode = convert_optab_handler (optab, TYPE_MODE (rhs_type[0]),
+                                          TYPE_MODE (lhs_type));
+
+  expand_insn (icode, 3, ops);
+  if (!rtx_equal_p (lhs_rtx, ops[0].value))
+    emit_move_insn (lhs_rtx, ops[0].value);
+}
+
 /* Expanders for optabs that can use expand_direct_optab_fn.  */
 
 #define expand_unary_optab_fn(FN, STMT, OPTAB) \
@@ -2640,6 +3553,30 @@ expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
 #define expand_binary_optab_fn(FN, STMT, OPTAB) \
   expand_direct_optab_fn (FN, STMT, OPTAB, 2)
 
+#define expand_ternary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 3)
+
+#define expand_cond_unary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 3)
+
+#define expand_cond_binary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 4)
+
+#define expand_cond_ternary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 5)
+
+#define expand_fold_extract_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 3)
+
+#define expand_fold_left_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 2)
+
+#define expand_mask_fold_left_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 3)
+
+#define expand_check_ptrs_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 4)
+
 /* RETURN_TYPE and ARGS are a return type and argument list that are
    in principle compatible with FN (which satisfies direct_internal_fn_p).
    Return the types that should be used to determine whether the
@@ -2684,6 +3621,19 @@ direct_optab_supported_p (direct_optab optab, tree_pair types,
   return direct_optab_handler (optab, mode, opt_type) != CODE_FOR_nothing;
 }
 
+/* Return true if OPTAB is supported for TYPES, where the first type
+   is the destination and the second type is the source.  Used for
+   convert optabs.  */
+
+static bool
+convert_optab_supported_p (convert_optab optab, tree_pair types,
+                          optimization_type opt_type)
+{
+  return (convert_optab_handler (optab, TYPE_MODE (types.first),
+                                TYPE_MODE (types.second), opt_type)
+         != CODE_FOR_nothing);
+}
+
 /* Return true if load/store lanes optab OPTAB is supported for
    array type TYPES.first when the optimization type is OPT_TYPE.  */
 
@@ -2700,10 +3650,71 @@ multi_vector_optab_supported_p (convert_optab optab, tree_pair types,
 
 #define direct_unary_optab_supported_p direct_optab_supported_p
 #define direct_binary_optab_supported_p direct_optab_supported_p
-#define direct_mask_load_optab_supported_p direct_optab_supported_p
+#define direct_ternary_optab_supported_p direct_optab_supported_p
+#define direct_cond_unary_optab_supported_p direct_optab_supported_p
+#define direct_cond_binary_optab_supported_p direct_optab_supported_p
+#define direct_cond_ternary_optab_supported_p direct_optab_supported_p
+#define direct_mask_load_optab_supported_p convert_optab_supported_p
 #define direct_load_lanes_optab_supported_p multi_vector_optab_supported_p
-#define direct_mask_store_optab_supported_p direct_optab_supported_p
+#define direct_mask_load_lanes_optab_supported_p multi_vector_optab_supported_p
+#define direct_gather_load_optab_supported_p convert_optab_supported_p
+#define direct_len_load_optab_supported_p direct_optab_supported_p
+#define direct_mask_store_optab_supported_p convert_optab_supported_p
 #define direct_store_lanes_optab_supported_p multi_vector_optab_supported_p
+#define direct_mask_store_lanes_optab_supported_p multi_vector_optab_supported_p
+#define direct_vec_cond_mask_optab_supported_p convert_optab_supported_p
+#define direct_vec_cond_optab_supported_p convert_optab_supported_p
+#define direct_scatter_store_optab_supported_p convert_optab_supported_p
+#define direct_len_store_optab_supported_p direct_optab_supported_p
+#define direct_while_optab_supported_p convert_optab_supported_p
+#define direct_fold_extract_optab_supported_p direct_optab_supported_p
+#define direct_fold_left_optab_supported_p direct_optab_supported_p
+#define direct_mask_fold_left_optab_supported_p direct_optab_supported_p
+#define direct_check_ptrs_optab_supported_p direct_optab_supported_p
+#define direct_vec_set_optab_supported_p direct_optab_supported_p
+
+/* Return the optab used by internal function FN.  */
+
+static optab
+direct_internal_fn_optab (internal_fn fn, tree_pair types)
+{
+  switch (fn)
+    {
+#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
+    case IFN_##CODE: break;
+#define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
+    case IFN_##CODE: return OPTAB##_optab;
+#define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
+                                    UNSIGNED_OPTAB, TYPE)              \
+    case IFN_##CODE: return (TYPE_UNSIGNED (types.SELECTOR)            \
+                            ? UNSIGNED_OPTAB ## _optab                 \
+                            : SIGNED_OPTAB ## _optab);
+#include "internal-fn.def"
+
+    case IFN_LAST:
+      break;
+    }
+  gcc_unreachable ();
+}
+
+/* Return the optab used by internal function FN.  */
+
+static optab
+direct_internal_fn_optab (internal_fn fn)
+{
+  switch (fn)
+    {
+#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
+    case IFN_##CODE: break;
+#define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
+    case IFN_##CODE: return OPTAB##_optab;
+#include "internal-fn.def"
+
+    case IFN_LAST:
+      break;
+    }
+  gcc_unreachable ();
+}
 
 /* Return true if FN is supported for the types in TYPES when the
    optimization type is OPT_TYPE.  The types are those associated with
@@ -2722,6 +3733,16 @@ direct_internal_fn_supported_p (internal_fn fn, tree_pair types,
     case IFN_##CODE: \
       return direct_##TYPE##_optab_supported_p (OPTAB##_optab, types, \
                                                opt_type);
+#define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
+                                    UNSIGNED_OPTAB, TYPE)              \
+    case IFN_##CODE:                                                   \
+      {                                                                        \
+       optab which_optab = (TYPE_UNSIGNED (types.SELECTOR)             \
+                            ? UNSIGNED_OPTAB ## _optab                 \
+                            : SIGNED_OPTAB ## _optab);                 \
+       return direct_##TYPE##_optab_supported_p (which_optab, types,   \
+                                                 opt_type);            \
+      }
 #include "internal-fn.def"
 
     case IFN_LAST:
@@ -2743,6 +3764,56 @@ direct_internal_fn_supported_p (internal_fn fn, tree type,
   return direct_internal_fn_supported_p (fn, tree_pair (type, type), opt_type);
 }
 
+/* Return true if the STMT is supported when the optimization type is OPT_TYPE,
+   given that STMT is a call to a direct internal function.  */
+
+bool
+direct_internal_fn_supported_p (gcall *stmt, optimization_type opt_type)
+{
+  internal_fn fn = gimple_call_internal_fn (stmt);
+  tree_pair types = direct_internal_fn_types (fn, stmt);
+  return direct_internal_fn_supported_p (fn, types, opt_type);
+}
+
+/* If FN is commutative in two consecutive arguments, return the
+   index of the first, otherwise return -1.  */
+
+int
+first_commutative_argument (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_FMA:
+    case IFN_FMS:
+    case IFN_FNMA:
+    case IFN_FNMS:
+    case IFN_AVG_FLOOR:
+    case IFN_AVG_CEIL:
+    case IFN_MULH:
+    case IFN_MULHS:
+    case IFN_MULHRS:
+    case IFN_FMIN:
+    case IFN_FMAX:
+      return 0;
+
+    case IFN_COND_ADD:
+    case IFN_COND_MUL:
+    case IFN_COND_MIN:
+    case IFN_COND_MAX:
+    case IFN_COND_AND:
+    case IFN_COND_IOR:
+    case IFN_COND_XOR:
+    case IFN_COND_FMA:
+    case IFN_COND_FMS:
+    case IFN_COND_FNMA:
+    case IFN_COND_FNMS:
+      return 1;
+
+    default:
+      return -1;
+    }
+}
+
 /* Return true if IFN_SET_EDOM is supported.  */
 
 bool
@@ -2761,6 +3832,15 @@ set_edom_supported_p (void)
   {                                                    \
     expand_##TYPE##_optab_fn (fn, stmt, OPTAB##_optab);        \
   }
+#define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
+                                    UNSIGNED_OPTAB, TYPE)              \
+  static void                                                          \
+  expand_##CODE (internal_fn fn, gcall *stmt)                          \
+  {                                                                    \
+    tree_pair types = direct_internal_fn_types (fn, stmt);             \
+    optab which_optab = direct_internal_fn_optab (fn, types);          \
+    expand_##TYPE##_optab_fn (fn, stmt, which_optab);                  \
+  }
 #include "internal-fn.def"
 
 /* Routines to expand each internal function, indexed by function number.
@@ -2775,6 +3855,323 @@ static void (*const internal_fn_expanders[]) (internal_fn, gcall *) = {
   0
 };
 
+/* Invoke T(CODE, IFN) for each conditional function IFN that maps to a
+   tree code CODE.  */
+#define FOR_EACH_CODE_MAPPING(T) \
+  T (PLUS_EXPR, IFN_COND_ADD) \
+  T (MINUS_EXPR, IFN_COND_SUB) \
+  T (MULT_EXPR, IFN_COND_MUL) \
+  T (TRUNC_DIV_EXPR, IFN_COND_DIV) \
+  T (TRUNC_MOD_EXPR, IFN_COND_MOD) \
+  T (RDIV_EXPR, IFN_COND_RDIV) \
+  T (MIN_EXPR, IFN_COND_MIN) \
+  T (MAX_EXPR, IFN_COND_MAX) \
+  T (BIT_AND_EXPR, IFN_COND_AND) \
+  T (BIT_IOR_EXPR, IFN_COND_IOR) \
+  T (BIT_XOR_EXPR, IFN_COND_XOR) \
+  T (LSHIFT_EXPR, IFN_COND_SHL) \
+  T (RSHIFT_EXPR, IFN_COND_SHR)
+
+/* Return a function that only performs CODE when a certain condition is met
+   and that uses a given fallback value otherwise.  For example, if CODE is
+   a binary operation associated with conditional function FN:
+
+     LHS = FN (COND, A, B, ELSE)
+
+   is equivalent to the C expression:
+
+     LHS = COND ? A CODE B : ELSE;
+
+   operating elementwise if the operands are vectors.
+
+   Return IFN_LAST if no such function exists.  */
+
+internal_fn
+get_conditional_internal_fn (tree_code code)
+{
+  switch (code)
+    {
+#define CASE(CODE, IFN) case CODE: return IFN;
+      FOR_EACH_CODE_MAPPING(CASE)
+#undef CASE
+    default:
+      return IFN_LAST;
+    }
+}
+
+/* If IFN implements the conditional form of a tree code, return that
+   tree code, otherwise return ERROR_MARK.  */
+
+tree_code
+conditional_internal_fn_code (internal_fn ifn)
+{
+  switch (ifn)
+    {
+#define CASE(CODE, IFN) case IFN: return CODE;
+      FOR_EACH_CODE_MAPPING(CASE)
+#undef CASE
+    default:
+      return ERROR_MARK;
+    }
+}
+
+/* Invoke T(IFN) for each internal function IFN that also has an
+   IFN_COND_* form.  */
+#define FOR_EACH_COND_FN_PAIR(T) \
+  T (FMA) \
+  T (FMS) \
+  T (FNMA) \
+  T (FNMS)
+
+/* Return a function that only performs internal function FN when a
+   certain condition is met and that uses a given fallback value otherwise.
+   In other words, the returned function FN' is such that:
+
+     LHS = FN' (COND, A1, ... An, ELSE)
+
+   is equivalent to the C expression:
+
+     LHS = COND ? FN (A1, ..., An) : ELSE;
+
+   operating elementwise if the operands are vectors.
+
+   Return IFN_LAST if no such function exists.  */
+
+internal_fn
+get_conditional_internal_fn (internal_fn fn)
+{
+  switch (fn)
+    {
+#define CASE(NAME) case IFN_##NAME: return IFN_COND_##NAME;
+      FOR_EACH_COND_FN_PAIR(CASE)
+#undef CASE
+    default:
+      return IFN_LAST;
+    }
+}
+
+/* If IFN implements the conditional form of an unconditional internal
+   function, return that unconditional function, otherwise return IFN_LAST.  */
+
+internal_fn
+get_unconditional_internal_fn (internal_fn ifn)
+{
+  switch (ifn)
+    {
+#define CASE(NAME) case IFN_COND_##NAME: return IFN_##NAME;
+      FOR_EACH_COND_FN_PAIR(CASE)
+#undef CASE
+    default:
+      return IFN_LAST;
+    }
+}
+
+/* Return true if STMT can be interpreted as a conditional tree code
+   operation of the form:
+
+     LHS = COND ? OP (RHS1, ...) : ELSE;
+
+   operating elementwise if the operands are vectors.  This includes
+   the case of an all-true COND, so that the operation always happens.
+
+   When returning true, set:
+
+   - *COND_OUT to the condition COND, or to NULL_TREE if the condition
+     is known to be all-true
+   - *CODE_OUT to the tree code
+   - OPS[I] to operand I of *CODE_OUT
+   - *ELSE_OUT to the fallback value ELSE, or to NULL_TREE if the
+     condition is known to be all true.  */
+
+bool
+can_interpret_as_conditional_op_p (gimple *stmt, tree *cond_out,
+                                  tree_code *code_out,
+                                  tree (&ops)[3], tree *else_out)
+{
+  if (gassign *assign = dyn_cast <gassign *> (stmt))
+    {
+      *cond_out = NULL_TREE;
+      *code_out = gimple_assign_rhs_code (assign);
+      ops[0] = gimple_assign_rhs1 (assign);
+      ops[1] = gimple_assign_rhs2 (assign);
+      ops[2] = gimple_assign_rhs3 (assign);
+      *else_out = NULL_TREE;
+      return true;
+    }
+  if (gcall *call = dyn_cast <gcall *> (stmt))
+    if (gimple_call_internal_p (call))
+      {
+       internal_fn ifn = gimple_call_internal_fn (call);
+       tree_code code = conditional_internal_fn_code (ifn);
+       if (code != ERROR_MARK)
+         {
+           *cond_out = gimple_call_arg (call, 0);
+           *code_out = code;
+           unsigned int nops = gimple_call_num_args (call) - 2;
+           for (unsigned int i = 0; i < 3; ++i)
+             ops[i] = i < nops ? gimple_call_arg (call, i + 1) : NULL_TREE;
+           *else_out = gimple_call_arg (call, nops + 1);
+           if (integer_truep (*cond_out))
+             {
+               *cond_out = NULL_TREE;
+               *else_out = NULL_TREE;
+             }
+           return true;
+         }
+      }
+  return false;
+}
+
+/* Return true if IFN is some form of load from memory.  */
+
+bool
+internal_load_fn_p (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_MASK_LOAD:
+    case IFN_LOAD_LANES:
+    case IFN_MASK_LOAD_LANES:
+    case IFN_GATHER_LOAD:
+    case IFN_MASK_GATHER_LOAD:
+    case IFN_LEN_LOAD:
+      return true;
+
+    default:
+      return false;
+    }
+}
+
+/* Return true if IFN is some form of store to memory.  */
+
+bool
+internal_store_fn_p (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_MASK_STORE:
+    case IFN_STORE_LANES:
+    case IFN_MASK_STORE_LANES:
+    case IFN_SCATTER_STORE:
+    case IFN_MASK_SCATTER_STORE:
+    case IFN_LEN_STORE:
+      return true;
+
+    default:
+      return false;
+    }
+}
+
+/* Return true if IFN is some form of gather load or scatter store.  */
+
+bool
+internal_gather_scatter_fn_p (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_GATHER_LOAD:
+    case IFN_MASK_GATHER_LOAD:
+    case IFN_SCATTER_STORE:
+    case IFN_MASK_SCATTER_STORE:
+      return true;
+
+    default:
+      return false;
+    }
+}
+
+/* If FN takes a vector mask argument, return the index of that argument,
+   otherwise return -1.  */
+
+int
+internal_fn_mask_index (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_MASK_LOAD:
+    case IFN_MASK_LOAD_LANES:
+    case IFN_MASK_STORE:
+    case IFN_MASK_STORE_LANES:
+      return 2;
+
+    case IFN_MASK_GATHER_LOAD:
+    case IFN_MASK_SCATTER_STORE:
+      return 4;
+
+    default:
+      return (conditional_internal_fn_code (fn) != ERROR_MARK
+             || get_unconditional_internal_fn (fn) != IFN_LAST ? 0 : -1);
+    }
+}
+
+/* If FN takes a value that should be stored to memory, return the index
+   of that argument, otherwise return -1.  */
+
+int
+internal_fn_stored_value_index (internal_fn fn)
+{
+  switch (fn)
+    {
+    case IFN_MASK_STORE:
+    case IFN_MASK_STORE_LANES:
+    case IFN_SCATTER_STORE:
+    case IFN_MASK_SCATTER_STORE:
+    case IFN_LEN_STORE:
+      return 3;
+
+    default:
+      return -1;
+    }
+}
+
+/* Return true if the target supports gather load or scatter store function
+   IFN.  For loads, VECTOR_TYPE is the vector type of the load result,
+   while for stores it is the vector type of the stored data argument.
+   MEMORY_ELEMENT_TYPE is the type of the memory elements being loaded
+   or stored.  OFFSET_VECTOR_TYPE is the vector type that holds the
+   offset from the shared base address of each loaded or stored element.
+   SCALE is the amount by which these offsets should be multiplied
+   *after* they have been extended to address width.  */
+
+bool
+internal_gather_scatter_fn_supported_p (internal_fn ifn, tree vector_type,
+                                       tree memory_element_type,
+                                       tree offset_vector_type, int scale)
+{
+  if (!tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vector_type)),
+                          TYPE_SIZE (memory_element_type)))
+    return false;
+  if (maybe_ne (TYPE_VECTOR_SUBPARTS (vector_type),
+               TYPE_VECTOR_SUBPARTS (offset_vector_type)))
+    return false;
+  optab optab = direct_internal_fn_optab (ifn);
+  insn_code icode = convert_optab_handler (optab, TYPE_MODE (vector_type),
+                                          TYPE_MODE (offset_vector_type));
+  int output_ops = internal_load_fn_p (ifn) ? 1 : 0;
+  bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (offset_vector_type));
+  return (icode != CODE_FOR_nothing
+         && insn_operand_matches (icode, 2 + output_ops, GEN_INT (unsigned_p))
+         && insn_operand_matches (icode, 3 + output_ops, GEN_INT (scale)));
+}
+
+/* Return true if the target supports IFN_CHECK_{RAW,WAR}_PTRS function IFN
+   for pointers of type TYPE when the accesses have LENGTH bytes and their
+   common byte alignment is ALIGN.  */
+
+bool
+internal_check_ptrs_fn_supported_p (internal_fn ifn, tree type,
+                                   poly_uint64 length, unsigned int align)
+{
+  machine_mode mode = TYPE_MODE (type);
+  optab optab = direct_internal_fn_optab (ifn);
+  insn_code icode = direct_optab_handler (optab, mode);
+  if (icode == CODE_FOR_nothing)
+    return false;
+  rtx length_rtx = immed_wide_int_const (length, mode);
+  return (insn_operand_matches (icode, 3, length_rtx)
+         && insn_operand_matches (icode, 4, GEN_INT (align)));
+}
+
 /* Expand STMT as though it were a call to internal function FN.  */
 
 void
@@ -2791,6 +4188,48 @@ expand_internal_call (gcall *stmt)
   expand_internal_call (gimple_call_internal_fn (stmt), stmt);
 }
 
+/* If TYPE is a vector type, return true if IFN is a direct internal
+   function that is supported for that type.  If TYPE is a scalar type,
+   return true if IFN is a direct internal function that is supported for
+   the target's preferred vector version of TYPE.  */
+
+bool
+vectorized_internal_fn_supported_p (internal_fn ifn, tree type)
+{
+  if (VECTOR_MODE_P (TYPE_MODE (type)))
+    return direct_internal_fn_supported_p (ifn, type, OPTIMIZE_FOR_SPEED);
+
+  scalar_mode smode;
+  if (!is_a <scalar_mode> (TYPE_MODE (type), &smode))
+    return false;
+
+  machine_mode vmode = targetm.vectorize.preferred_simd_mode (smode);
+  if (VECTOR_MODE_P (vmode))
+    {
+      tree vectype = build_vector_type_for_mode (type, vmode);
+      if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
+       return true;
+    }
+
+  auto_vector_modes vector_modes;
+  targetm.vectorize.autovectorize_vector_modes (&vector_modes, true);
+  for (machine_mode base_mode : vector_modes)
+    if (related_vector_mode (base_mode, smode).exists (&vmode))
+      {
+       tree vectype = build_vector_type_for_mode (type, vmode);
+       if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
+         return true;
+      }
+
+  return false;
+}
+
+void
+expand_SHUFFLEVECTOR (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
 void
 expand_PHI (internal_fn, gcall *)
 {